-
Notifications
You must be signed in to change notification settings - Fork 423
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
Fix null-termination bug w/ string
->int
cast
#21146
Fix null-termination bug w/ string
->int
cast
#21146
Conversation
… failure mode. Signed-off-by: Jeremiah Corrado <jeremiah.corrado@hpe.com>
I was expecting that this PR would adjust IO.readLine to add a null terminator there somewhere but it does not. Why not? |
Offline, @vasslitvinov suggested modifying I suppose adding a null terminator to readLine would be more correct tho? Maybe to protect against similar bugs on other casts? |
Signed-off-by: Jeremiah Corrado <jeremiah.corrado@hpe.com>
@@ -1426,6 +1426,9 @@ module BytesStringCommon { | |||
} | |||
} | |||
} | |||
|
|||
// ensure that there is a null byte at the end of the buffer | |||
x.buff[x.buffLen] = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend if x.buffLen > 0
here around this, unless you know that x
cannot be the empty string at this point.
Signed-off-by: Jeremiah Corrado <jeremiah.corrado@hpe.com>
Thanks so much for picking this up quickly @jeremiah-corrado! Wish we'd discovered it prior to AoC 2022! |
Sure thing, at least we got it early! |
True! What if it hadn't been until day 10! :O |
During AoC activities, we identified a bug involving a missing null-terminator in strings created by
IO.readLine
. The bug caused leftover bytes from previousreadLine
operations to show up in casts fromstring
toint
.This PR modifies
IO.readStringBytesData
to properly terminate strings modified byreadLine(ref s: string)
andreadLine(ref b: bytes)
It also ensures that string buffers are properly terminated before casts to integral types, by modifying
_cleanupForNumericCast
.An additional test from AoC activities was added to confirm the validity of the fix.