Skip to content

Commit

Permalink
Detect invalid length (thanks to Ivan Krylov).
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@86594 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
kalibera committed May 23, 2024
1 parent 66ce19d commit 7c34dbb
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/main/serialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1985,6 +1985,8 @@ static SEXP ReadItem_Recursive (int flags, SEXP ref_table, R_inpstream_t stream)
{
/* These are all short strings */
length = InInteger(stream);
if (length < 0)
error(_("invalid length"));
R_CheckStack2(length+1);
char cbuf[length+1];
InString(stream, cbuf, length);
Expand All @@ -2000,7 +2002,9 @@ static SEXP ReadItem_Recursive (int flags, SEXP ref_table, R_inpstream_t stream)
case CHARSXP:
/* these are currently limited to 2^31 -1 bytes */
length = InInteger(stream);
if (length == -1)
if (length < -1)
error(_("invalid length"));
else if (length == -1)
PROTECT(s = NA_STRING);
else if (length < 1000) {
char cbuf[length+1];
Expand Down Expand Up @@ -2247,7 +2251,7 @@ SEXP R_Unserialize(R_inpstream_t stream)
case 3:
{
int nelen = InInteger(stream);
if (nelen > R_CODESET_MAX)
if (nelen > R_CODESET_MAX || nelen < 0)
error(_("invalid length of encoding name"));
InString(stream, stream->native_encoding, nelen);
stream->native_encoding[nelen] = '\0';
Expand Down Expand Up @@ -2338,7 +2342,7 @@ SEXP R_SerializeInfo(R_inpstream_t stream)
if (version == 3) {
SET_STRING_ELT(names, 4, mkChar("native_encoding"));
int nelen = InInteger(stream);
if (nelen > R_CODESET_MAX)
if (nelen > R_CODESET_MAX || nelen < 0)
error(_("invalid length of encoding name"));
char nbuf[nelen + 1];
InString(stream, nbuf, nelen);
Expand Down

0 comments on commit 7c34dbb

Please sign in to comment.