added tmpstring overrun guards
authorPhilip Kovacs <pkovacs@users.sourceforge.net>
Tue, 29 Nov 2005 17:35:21 +0000 (17:35 +0000)
committerPhilip Kovacs <pkovacs@users.sourceforge.net>
Tue, 29 Nov 2005 17:35:21 +0000 (17:35 +0000)
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky@434 7f574dfc-610e-0410-a909-a81674777703

src/conky.c
src/conky.h

index ae8fc7f..d97cd72 100644 (file)
@@ -3462,7 +3462,9 @@ static void draw_string(const char *s)
                printf("%s\n", s);
        }
        /* daemon_run(s);  the daemon can be called here, but we need to have a buffer in daemon_run() and we need to tell it when everything is ready to be sent */
-       strcpy(tmpstring1, s);
+       memset(tmpstring1,0,TEXT_BUFFER_SIZE);
+       memset(tmpstring2,0,TEXT_BUFFER_SIZE);
+       strncpy(tmpstring1, s, TEXT_BUFFER_SIZE-1);
        pos = 0;
        added = 0;
        char space[2];
@@ -3483,13 +3485,21 @@ static void draw_string(const char *s)
                        for (i2 = 0;
                             i2 < (8 - (1 + pos) % 8) && added <= max;
                             i2++) {
-                               tmpstring2[pos + i2] = ' ';
+                               /*
+                               if ( pos + i2 > TEXT_BUFFER_SIZE-1 )
+                                       fprintf(stderr,"buffer overrun detected\n");
+                               */
+                               tmpstring2[ MIN(pos + i2, TEXT_BUFFER_SIZE-1) ] = ' '; /* guard against overrun */
                                added++;
                        }
                        pos += i2;
                } else {
                        if (tmpstring1[i] != 9) {
-                               tmpstring2[pos] = tmpstring1[i];
+                               /*
+                               if ( pos > TEXT_BUFFER_SIZE-1 )
+                                        fprintf(stderr,"buffer overrun detected\n");
+                               */
+                               tmpstring2[ MIN(pos, TEXT_BUFFER_SIZE-1) ] = tmpstring1[i]; /* guard against overrun */
                                pos++;
                        }
                }
index 3ab87f3..a5e1492 100644 (file)
@@ -55,6 +55,9 @@ fprintf(stderr, "Conky: " s "\n", ##varargs)
 #define CRIT_ERR(s, varargs...) \
 { fprintf(stderr, "Conky: " s "\n", ##varargs);  exit(EXIT_FAILURE); }
 
+#define MIN(a,b) (a>b ? b : a)
+#define MAX(a,b) (a<b ? b : a)
+
 struct i8k_struct {
        char *version;
        char *bios;