1
0
mirror of https://github.com/gnosygnu/xowa.git synced 2026-03-02 03:49:30 +00:00

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

This commit is contained in:
gnosygnu
2019-09-02 22:30:08 -04:00
parent 8f029f479b
commit cda21d987f
6 changed files with 23 additions and 8 deletions

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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);