Browse Source

string: fix a potential memory corruption with caca_printf() calls done

outside the canvas.
tags/v0.99.beta19
Sam Hocevar sam 12 years ago
parent
commit
ab882df7b1
1 changed files with 9 additions and 6 deletions
  1. +9
    -6
      caca/string.c

+ 9
- 6
caca/string.c View File

@@ -315,7 +315,7 @@ int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
* *
* Format a string at the given coordinates, using the default foreground * Format a string at the given coordinates, using the default foreground
* and background values. The coordinates may be outside the canvas * and background values. The coordinates may be outside the canvas
* boundaries (eg. a negative Y coordinate) and the string will be cropped
* boundaries (eg. a negative X coordinate) and the string will be cropped
* accordingly if it is too long. The syntax of the format string is the * accordingly if it is too long. The syntax of the format string is the
* same as for the C vprintf() function. * same as for the C vprintf() function.
* *
@@ -337,19 +337,22 @@ int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
{ {
char tmp[BUFSIZ]; char tmp[BUFSIZ];
char *buf = tmp; char *buf = tmp;
int ret;
int bufsize = BUFSIZ, ret;


if(cv->width - x + 1 > BUFSIZ) if(cv->width - x + 1 > BUFSIZ)
buf = malloc(cv->width - x + 1);
{
bufsize = cv->width - x + 1;
buf = malloc(bufsize);
}


#if defined(HAVE_VSNPRINTF_S) #if defined(HAVE_VSNPRINTF_S)
vsnprintf_s(buf, cv->width - x + 1, _TRUNCATE, format, args);
vsnprintf_s(buf, bufsize, _TRUNCATE, format, args);
#elif defined(HAVE_VSNPRINTF) #elif defined(HAVE_VSNPRINTF)
vsnprintf(buf, cv->width - x + 1, format, args);
vsnprintf(buf, bufsize, format, args);
#else #else
vsprintf(buf, format, args); vsprintf(buf, format, args);
#endif #endif
buf[cv->width - x] = '\0';
buf[bufsize - 1] = '\0';


ret = caca_put_str(cv, x, y, buf); ret = caca_put_str(cv, x, y, buf);




Loading…
Cancel
Save