Skip to content

Commit

Permalink
fwrite() race condition could lead to huge memmove / crash. Fixed, th…
Browse files Browse the repository at this point in the history
…anks to sam-itt for pointing it out (GitHub issue #10).

git-svn-id: svn://rootdirectory.ddns.net/pdclib/trunk@863 bcf39385-58cc-4174-9fcf-14f50f90dd47
  • Loading branch information
solar authored and thrimbor committed Mar 6, 2020
1 parent ecbbfe7 commit b770ec6
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions functions/stdio/fwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, str
_PDCLIB_UNLOCK( stream->mtx );
return nmemb_i;
}
offset = 0;
/* lineend = false; */
}
}
Expand All @@ -72,18 +73,19 @@ size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, str
}
break;
case _IOLBF:
if ( offset > 0 )
{
size_t bufidx = stream->bufidx;
stream->bufidx = offset;
if ( _PDCLIB_flushbuffer( stream ) == EOF )
{
/* See comment above. */
stream->bufidx = bufidx;
_PDCLIB_UNLOCK( stream->mtx );
return nmemb_i - 1;
}
stream->bufidx = bufidx - offset;
memmove( stream->buffer, stream->buffer + offset, stream->bufidx );
size_t bufidx = stream->bufidx;
stream->bufidx = offset;
if ( _PDCLIB_flushbuffer( stream ) == EOF )
{
/* See comment above. */
stream->bufidx = bufidx;
_PDCLIB_UNLOCK( stream->mtx );
return nmemb_i - 1;
}
stream->bufidx = bufidx - offset;
memmove( stream->buffer, stream->buffer + offset, stream->bufidx );
}
}

Expand Down

0 comments on commit b770ec6

Please sign in to comment.