Skip to content

Commit

Permalink
Fix bug in u_read()
Browse files Browse the repository at this point in the history
It was possible to overrun the strings region leading to corruption of memory.
The new code checks for overrun and allocates a new buffer if it would occur.
  • Loading branch information
Don-Ward committed Oct 27, 2024
1 parent 628a09a commit 1842481
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/runtime/rposix.r
Original file line number Diff line number Diff line change
Expand Up @@ -2445,9 +2445,21 @@ dptr u_read(dptr f, int n, int fstatus, dptr d)
/* Something is available: allocate another chunk */
if (i == 0)
StrLoc(*d) = alcstr(NULL, bufsize);
else
else {
/* Extend the string */
/* We must guard against running over the end of the current string region.
* In that case, allocate a whole new buffer (which will result in a GC)
* and copy the existing buffer into it. Don't use alcstr() to do the copy
* because that might involve accessing potentially non-existent memory after
* the end of the (old) string region.
*/
if (DiffPtrs(strend,strfree) < bufsize) {
char *newb = alcstr(NULL, StrLen(*d) + bufsize); /* a GC will occur */
memcpy(newb, StrLoc(*d), StrLen(*d));
StrLoc(*d) = newb;
} else
(void) alcstr(NULL, bufsize);
}
tryagain:
if (fstatus & Fs_Socket) {
Expand Down

0 comments on commit 1842481

Please sign in to comment.