# HG changeset patch # User ib # Date 1299617811 0 # Node ID fbaae7fe1a1311bdb2b5c0cef6a23afd34aeec2a # Parent dc905c693b7a836aeb93a9711a382c96c804e24b Fix several issues with Translate(). 1. The "Unsafe!" comment has been removed, because the strings passed to the function are strcpy'd. 2. The needless memsets (one of which with wrong size) have been removed in favor of a sufficiently simple initialization of trbuf. 3. The array indices are unsigned now, and the manual optimization of having strlen() outside the for loop has been removed in favor of optimization performed by the compiler. 4. There is a check now to prevent an out-of-bounds array access. diff -r dc905c693b7a -r fbaae7fe1a13 gui/mplayer/gui_common.c --- a/gui/mplayer/gui_common.c Tue Mar 08 19:07:55 2011 +0000 +++ b/gui/mplayer/gui_common.c Tue Mar 08 20:56:51 2011 +0000 @@ -107,24 +107,22 @@ } } -/* Unsafe! Pass only null-terminated strings as (char *)str. */ char *Translate(char *str) { static char trbuf[512]; char tmp[512]; - int i, c; + unsigned int i, c; int t; - int strsize = 0; mixer_t *mixer; - memset(trbuf, 0, 512); - memset(tmp, 0, 128); - strsize = strlen(str); + *trbuf = 0; - for (c = 0, i = 0; i < strsize; i++) { + for (c = 0, i = 0; i < strlen(str); i++) { if (str[i] != '$') { + if (c + 1 < sizeof(trbuf)) { trbuf[c++] = str[i]; trbuf[c] = 0; + } } else { switch (str[++i]) { case 't':