We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3531 - internal SDL_vsnprintf implementation access memory outside given buffer ranges
Summary: internal SDL_vsnprintf implementation access memory outside given buffer ranges
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: *don't know* (show other bugs)
Version: 2.0.5
Hardware: All Windows 10
: P2 normal
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-12-28 20:08 UTC by Tristan
Modified: 2017-01-01 00:15 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tristan 2016-12-28 20:08:25 UTC
The internal SDL_vsnprintf implementation accesses memory outside buffer. The bug existed also inside the format (%) processing, which was fixed with Bug 3441.

But there is still an invalid access, if we do not have any format inside the source string and the destination string is shorter than the format string. You can use any string for this test, as long it is longer than the buffer.

Example:

va_list argList;
char buffer[4];
SDL_vsnprintf(buffer, sizeof(buffer), "Testing", argList);

The bug is located on the 'else' branch of the format char test:

while (*fmt) {
  if (*fmt == '%') {
    ...
  } else {
    if (left > 1) {
      *text = *fmt;
      --left;
    }
    ++fmt;
    ++text;
  }
}
if (left > 0) {
  *text = '\0';
}

As you can see that text is always incremented, even when left is already one. When then on the last lines, *text is assigned the NULL char, the pointer is located outside bounds.

Sorry for my perhaps ambiguous english, it is not my native language.
Comment 1 Sam Lantinga 2017-01-01 00:15:09 UTC
Good catch. This is fixed, thanks!
https://hg.libsdl.org/SDL/rev/5f8d2376d5b2