tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

string-compare-char-out-of-bounds.js (673B)


      1 // |str.char(idx) == "b"| is compiled as |str.charCodeAt(idx) == 0x62|.
      2 
      3 const strings = [
      4  "",
      5  "a", "b", "c",
      6  "a-", "b-", "c-",
      7 ];
      8 
      9 for (let i = 0; i < 1000; ++i) {
     10  let str = strings[i % strings.length];
     11 
     12  // |j <= str.length| to test out-of-bounds access, too.
     13  for (let j = 0; j <= str.length; ++j) {
     14    let ch = str.charAt(j);
     15    let code = j < str.length ? str.charCodeAt(j) : -1;
     16 
     17    assertEq(ch == "", code == -1);
     18 
     19    assertEq(ch == "b", code == 0x62);
     20    assertEq(ch != "b", code != 0x62);
     21 
     22    assertEq(ch < "b", code < 0x62);
     23    assertEq(ch <= "b", code <= 0x62);
     24 
     25    assertEq(ch > "b", code > 0x62);
     26    assertEq(ch >= "b", code >= 0x62);
     27  }
     28 }