fix array index crash
commit5153d6eb2cca2e25d6b033a507c5abe899b0fc4f
authorSteve Cao <shiqicao@fb.com>
Wed, 30 Oct 2019 04:57:43 +0000 (29 21:57 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 30 Oct 2019 04:59:37 +0000 (29 21:59 -0700)
tree27a0e5d704cd60b7949b388d5debdbf338d20054
parentda44f5d6da388df36da2944eff997c0024233d33
fix array index crash

Summary:
I didn't realize Rust does runtime check for over flow, so the the trick won't work. In the previous implementation, I tried to depend on the "wrap around" behavior of over flowed subtraction operation.

Given usize is return by `len()` and `i >= 0` is always `true`, let `i` be the current length seems the easiest implementation.

```
fn rfind(s: &[u8], needle: u8) -> Option<usize> {
    let mut i = s.len();
    while i > 0 {
        i -= 1;
        if s[i] == needle {
            return Some(i);
        }
    }
    None
}
```

Reviewed By: losvald

Differential Revision: D18219014

fbshipit-source-id: 0d0ce8ea744e2ed30c2702c40a67ad7fd6270e2f
hphp/hack/src/parser/lowerer.rs
hphp/hack/src/utils/escaper.rs