changeset 32972:fbaae7fe1a13

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.
author ib
date Tue, 08 Mar 2011 20:56:51 +0000
parents dc905c693b7a
children 9346b986c721
files gui/mplayer/gui_common.c
diffstat 1 files changed, 5 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- 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':