-
Notifications
You must be signed in to change notification settings - Fork 101
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Executing some regular expressions on long strings may cause pointer out of bounds #195
Comments
C is arcane magic... The safety requirements for this "m must equal the size of s, and n must be the size currently used." ... so why have the 64 byte s to begin with if it's dynamic? ... wait... how can it be dynamic? ... C is arcane magic... sb = js_realloc(J, sb, (sb->m *= 2) + soffsetof(js_Buffer, s)); This is saying "Let's re-allocate a new memory region of size (2 x sb->m) + the offset of s (to leave space for the new n and m) while reassigning sb->m at the same time." So at the beginning js_Buffer is 64 + 4 + 4, or 72 bytes. Once the buffer is filled to 64, the else branch will allocate 128 + 4 + 4, or 136 bytes, and copy over the 72 bytes of the previous js_Buffer to the beginning of that 136 byte allocation. ....... so even though s is supposedly fixed size at 64, the reallocation logic is ensuring that indices beyond 64 should always be valid. This is not an uncommon pattern in C. C is arcane magic... lol |
Thank you very much for your answer @junderw |
@ahaoboy Since I saw your project on r/rust I will let you know that this short string optimization can be accomplished by the compact_str crate. However, the allocator is obviously not keeping track of what js_State probably is, so it's likely not a 1:1 comparison. |
You are a nice person, learned a lot, thank you, the principles of compact_str and js_Buffer seem to be very similar |
I am not familiar with C language, if this is expected, please close this issue
The sb->n++ here may be greater than 64, resulting in access to memory beyond the array
If you add the following code and execute the js file, you will see a lot of output, which seems to appear only in the regular expression related code
The text was updated successfully, but these errors were encountered: