Parser: Do not reuse byte array when trimming bfr [#562]

pull/620/head
gnosygnu 5 years ago
parent 8f029f479b
commit cda21d987f

@ -438,9 +438,9 @@ public class Bry_ {
return rv;
}
public static final byte[] Trim_ary_ws = mask_(256, Byte_ascii.Tab, Byte_ascii.Nl, Byte_ascii.Cr, Byte_ascii.Space);
public static byte[] Trim(byte[] src) {return Trim(src, 0, src.length, true, true, Trim_ary_ws);}
public static byte[] Trim(byte[] src, int bgn, int end) {return Trim(src, bgn, end, true, true, Trim_ary_ws);}
public static byte[] Trim(byte[] src, int bgn, int end, boolean trim_bgn, boolean trim_end, byte[] trim_ary) {
public static byte[] Trim(byte[] src) {return Trim(src, 0, src.length, true, true, Trim_ary_ws, true);}
public static byte[] Trim(byte[] src, int bgn, int end) {return Trim(src, bgn, end, true, true, Trim_ary_ws, true);}
public static byte[] Trim(byte[] src, int bgn, int end, boolean trim_bgn, boolean trim_end, byte[] trim_ary, boolean reuse_bry_if_noop) {
int txt_bgn = bgn, txt_end = end;
boolean all_ws = true;
if (trim_bgn) {
@ -466,7 +466,8 @@ public class Bry_ {
if (all_ws) return Bry_.Empty;
}
if ( bgn == 0 && end == src.length // Trim is called on entire bry, not subset
if ( reuse_bry_if_noop
&& bgn == 0 && end == src.length // Trim is called on entire bry, not subset
&& bgn == txt_bgn && end == txt_end // Trim hasn't trimmed anything
) {
return src;

@ -588,7 +588,7 @@ public class Bry_bfr {
public byte[] To_bry() {return bfr_len == 0 ? Bry_.Empty : Bry_.Mid(bfr, 0, bfr_len);}
public byte[] To_bry_and_clear_and_trim() {return To_bry_and_clear_and_trim(true, true, Bry_.Trim_ary_ws);}
public byte[] To_bry_and_clear_and_trim(boolean trim_bgn, boolean trim_end, byte[] trim_bry) {
byte[] rv = Bry_.Trim(bfr, 0, bfr_len, trim_bgn, trim_end, trim_bry);
byte[] rv = Bry_.Trim(bfr, 0, bfr_len, trim_bgn, trim_end, trim_bry, false); // NOTE: must not reuse bry; ISSUE#:562; DATE:2019-09-02
this.Clear();
return rv;
}

@ -219,6 +219,20 @@ public class Bry_bfr_tst {
fxt.Test__to_bry_ary_and_clear("a\nb\nc" , "a", "b", "c"); // lines=n
fxt.Test__to_bry_ary_and_clear("a\n" , "a"); // nl at end
}
@Test public void To_bry_ary_and_clear_and_trim__memory_reference_bug() {// PURPOSE:test that bry isn't reused; ISSUE#:562; DATE:2019-09-02
String str_a = "aaaaaaaaaaaaaaaa" // NOTE: length is 16 b/c bry_bfr init's to 16 len
, str_b = "bbbbbbbbbbbbbbbb";
Bry_bfr bfr = Bry_bfr_.New();
bfr.Add_str_a7(str_a);
byte[] bry_a = bfr.To_bry_and_clear_and_trim();
Gftest.Eq__str(str_a, String_.new_u8(bry_a));
bfr.Add_str_a7(str_b);
byte[] bry_b = bfr.To_bry_and_clear_and_trim();
Gftest.Eq__str(str_b, String_.new_u8(bry_b));
Gftest.Eq__str(str_a, String_.new_u8(bry_a)); // fais if bry_b
}
}
class ByteAryBfr_fxt {
private final Bry_bfr bfr = Bry_bfr_.Reset(16);

@ -90,7 +90,7 @@ class Xoh_toc_wkr__txt {
// add any text before tag
if (pos < txt_end) {
byte[] anch_bry = amp_mgr.Decode_as_bry(Bry_.Trim(src, pos, txt_end, Bool_.Y, Bool_.Y, Trim__anch)); // trim \n else ".0a"; PAGE:en.w:List_of_U-boats_never_deployed DATE:2016-08-13
byte[] anch_bry = amp_mgr.Decode_as_bry(Bry_.Trim(src, pos, txt_end, Bool_.Y, Bool_.Y, Trim__anch, Bool_.Y)); // trim \n else ".0a"; PAGE:en.w:List_of_U-boats_never_deployed DATE:2016-08-13
anch_encoder.Encode(anch_bfr, anch_bry);
text_bfr.Add_mid(src, pos, txt_end);
}

@ -154,7 +154,7 @@ public class Xog_tab_itm_edit_mgr {
byte[] rv = tab.Html_itm().Get_elem_value_for_edit_box_as_bry();
if (orig != null) // orig == null for Preview
rv = tab.Wiki().Parser_mgr().Hdr__section_editable__mgr().Merge_section(tab.Page().Url(), rv, orig);
rv = Bry_.Trim(rv, 0, rv.length, false, true, Bry_.Trim_ary_ws); // MW: trim all trailing ws; REF:EditPage.php!safeUnicodeInput; DATE:2014-04-25
rv = Bry_.Trim(rv, 0, rv.length, false, true, Bry_.Trim_ary_ws, true); // MW: trim all trailing ws; REF:EditPage.php!safeUnicodeInput; DATE:2014-04-25
return rv;
}
}

@ -21,7 +21,7 @@ public class Wbase_repo_linker {
private byte[] articlePath;
// private byte[] scriptPath;
public Wbase_repo_linker(byte[] baseUrl, byte[] articlePath, byte[] scriptPath) {
this.baseUrl = Bry_.Trim(baseUrl, 0, baseUrl.length, false, true, Bry_.mask_(256, Byte_ascii.Slash_bry)); // getBaseUrl
this.baseUrl = Bry_.Trim(baseUrl, 0, baseUrl.length, false, true, Bry_.mask_(256, Byte_ascii.Slash_bry), true); // getBaseUrl
this.articlePath = articlePath;
// this.scriptPath = scriptPath;
}

Loading…
Cancel
Save