gcalctool r2028 - in trunk: . gcalctool



Author: rancell
Date: Wed Mar 12 02:05:33 2008
New Revision: 2028
URL: http://svn.gnome.org/viewvc/gcalctool?rev=2028&view=rev

Log:
Bug #520769 - Remove usage of sprintf() and strcpy() and replace with snprintf() and strncpy().


Modified:
   trunk/ChangeLog
   trunk/gcalctool/calctool.c
   trunk/gcalctool/calctool.h
   trunk/gcalctool/display.c
   trunk/gcalctool/display.h
   trunk/gcalctool/functions.c
   trunk/gcalctool/functions.h
   trunk/gcalctool/get.c
   trunk/gcalctool/get.h
   trunk/gcalctool/gtk.c
   trunk/gcalctool/mp.c

Modified: trunk/gcalctool/calctool.c
==============================================================================
--- trunk/gcalctool/calctool.c	(original)
+++ trunk/gcalctool/calctool.c	Wed Mar 12 02:05:33 2008
@@ -625,7 +625,6 @@
     exit(1);
 }
 
-
 #define INC { argc--; argv++; }
 
 void
@@ -656,15 +655,7 @@
                         v->accuracy = DEFAULT_ACCURACY;
                     }
                     break;
-                
-                case 'n' :
-                    if (strcmp(argv[0], "-name") == 0)
-                    {
-                        INC;
-                        read_str(&v->appname, argv[0]);
-                    }
-                    break;
-                
+
                 case 'u':
                     unittest();
                     break;
@@ -751,11 +742,10 @@
     textdomain(GETTEXT_PACKAGE);
 
     v->progname = argv[0];     /* Save programs name. */
-    v->appname  = NULL;
     if ((ptr = strrchr(argv[0], '/')) != NULL) {
-        read_str(&v->appname, ptr+1);
+        v->appname = strdup(ptr+1);
     } else {
-        read_str(&v->appname, argv[0]);
+        v->appname = strdup(argv[0]);
     }
     
     init_state();

Modified: trunk/gcalctool/calctool.h
==============================================================================
--- trunk/gcalctool/calctool.h	(original)
+++ trunk/gcalctool/calctool.h	Wed Mar 12 02:05:33 2008
@@ -42,10 +42,8 @@
 #define MKSTEMP      (void) mkstemp
 #define REWIND       (void) rewind
 #define SNPRINTF     (void) snprintf
-#define SPRINTF      (void) sprintf
 #define SSCANF       (void) sscanf
 #define STRCAT       (void) strcat
-#define STRCPY       (void) strcpy
 #define STRNCPY      (void) strncpy
 #define STRNCAT      (void) strncat
 #define UNLINK       (void) unlink

Modified: trunk/gcalctool/display.c
==============================================================================
--- trunk/gcalctool/display.c	(original)
+++ trunk/gcalctool/display.c	Wed Mar 12 02:05:33 2008
@@ -46,7 +46,7 @@
  */
 
 void
-localize_number(char *dest, const char *src)
+localize_number(char *dest, const char *src, int dest_length)
 {
     char tnum[MAX_LOCALIZED], *dstp;
 
@@ -88,7 +88,7 @@
         }
         *dstp++ = '\0';
     } else {
-        STRCPY(dest, src);
+        STRNCPY(dest, src, dest_length - 1);
     }
     dstp = strchr(dest, '.');
     if (dstp != NULL) {
@@ -168,7 +168,7 @@
 char *
 make_fixed(int *MPnumber, char *str, int base, int cmax, int toclear)
 {
-    char half[4], *optr;
+    char half[MAXLINE], *optr;
     int MP1base[MP_SIZE], MP1[MP_SIZE], MP2[MP_SIZE], MPval[MP_SIZE];
     int ndig;                   /* Total number of digits to generate. */
     int ddig;                   /* Number of digits to left of decimal sep. */
@@ -186,7 +186,7 @@
 
     mppwr(MP1base, &v->accuracy, MP1);
     /* FIXME: string const. if MPstr_to_num can get it */
-    SPRINTF(half, "0.5");
+    SNPRINTF(half, MAXLINE, "0.5");
     MPstr_to_num(half, DEC, MP2);
     mpdiv(MP2, MP1, MP1);
     mpadd(MPval, MP1, MPval);
@@ -276,7 +276,7 @@
 static char *
 make_eng_sci(int *MPnumber, int base)
 {
-    char half[4], fixed[MAX_DIGITS], *optr;
+    char half[MAXLINE], fixed[MAX_DIGITS], *optr;
     int MP1[MP_SIZE], MPatmp[MP_SIZE], MPval[MP_SIZE];
     int MP1base[MP_SIZE], MP3base[MP_SIZE], MP10base[MP_SIZE];
     int i, dval, len, n;
@@ -335,7 +335,7 @@
         }
     }
  
-    STRCPY(fixed, make_fixed(MPmant, v->fnum, base, MAX_DIGITS-6, TRUE));
+    STRNCPY(fixed, make_fixed(MPmant, v->fnum, base, MAX_DIGITS-6, TRUE), MAX_DIGITS - 1);
     len = strlen(fixed);
     for (i = 0; i < len; i++) {
         *optr++ = fixed[i];
@@ -350,7 +350,7 @@
         *optr++ = '+';
     }
  
-    SPRINTF(half, "0.5");
+    SNPRINTF(half, MAXLINE, "0.5");
     MPstr_to_num(half, DEC, MP1);
     mpaddi(MP1, &exp, MPval);
     n = 1;
@@ -570,7 +570,7 @@
 refresh_display(int cursor)
 {
     int i, MP_reg[MP_SIZE];
-    char localized[MAX_LOCALIZED], *str, *ans, reg[3];
+    char localized[MAX_LOCALIZED], *str, *ans, reg[3], *t;
     struct exprm_state *e;
 
     switch (v->syntax) {
@@ -588,14 +588,18 @@
             }
             ans = make_number(e->ans, v->base, TRUE);
 
-            localize_number(localized, ans);
-            str_replace(&str, "Ans", localized);
+            localize_number(localized, ans, MAX_LOCALIZED);
+            t = str_replace(str, "Ans", localized);
+            free(str);
+            str = t;
 
             /* Replace registers with values. */
             for (i = 0; i < 10; i++) {
                 SNPRINTF(reg, 3, "R%d", i);
                 do_rcl_reg(i, MP_reg);
-                str_replace(&str, reg, make_number(MP_reg, v->base, FALSE));
+                t = str_replace(str, reg, make_number(MP_reg, v->base, FALSE));
+                free(str);
+                str = t;
             }
 
             ui_set_display(str, cursor);

Modified: trunk/gcalctool/display.h
==============================================================================
--- trunk/gcalctool/display.h	(original)
+++ trunk/gcalctool/display.h	Wed Mar 12 02:05:33 2008
@@ -25,7 +25,7 @@
 #include "calctool.h"
 
 void initialise();
-void localize_number(char *, const char *);
+void localize_number(char *, const char *, int);
 char *make_fixed(int *, char *, int, int, int);
 char *make_number(int *, int, int);
 void clear_display(int);

Modified: trunk/gcalctool/functions.c
==============================================================================
--- trunk/gcalctool/functions.c	(original)
+++ trunk/gcalctool/functions.c	Wed Mar 12 02:05:33 2008
@@ -337,7 +337,10 @@
     if (cursor < 0) {
         if (exp_has_postfix(e->expression, "Ans")) {
             char *ans = make_number(e->ans, v->base, FALSE);
-            str_replace(&e->expression, "Ans", ans);
+            char *t;
+            t = str_replace(&e->expression, "Ans", ans);
+            free(e->expression);
+            e->expression = t;
         } else {
             for (i = 0; i < 10; i++) {
                 SNPRINTF(buf, MAXLINE, "R%d", i);
@@ -428,44 +431,30 @@
 }
 
 
-void
-str_replace(char **str, char *from, char *to)
+char *
+str_replace(char *str, char *from, char *to)
 {
-    int i, flen, len;
-
-    assert(str);
-    assert(from);
-    assert(to);
-
-    if (!*str) {
-        return;
-    }
-
-    i = 0;
-    len = strlen(*str);
-    flen = strlen(from);
-
-    for (i = 0; len-i >= flen; i++) {
-        if (!strncasecmp(from, *str+i, flen)) {
-            char *print;
-            int j = i+flen;
-            char *prefix = malloc(i+1);
-            char *postfix = malloc(len-j+1);
-
-            assert(prefix && postfix);
-            memset(prefix, 0, i+1);
-            memset(postfix, 0, len-j+1);
-            MEMCPY(prefix, *str, i);
-            MEMCPY(postfix, *str+i+flen, len-j);
-
-            print = malloc(strlen(to)+i+len-j+1);
-            SPRINTF(print, "%s%s%s", prefix, to, postfix);
-            free(prefix);
-            free(postfix);
-            free(*str);
-            *str = print;
+    char output[MAXLINE];
+    int offset = 0;
+    char *c;
+    int flen = strlen(from);
+    int tlen = strlen(to);
+    
+    for (c = str; *c && offset < MAXLINE - 1; c++, offset++) {
+        if (strncasecmp(from, c, flen) == 0) {
+            SNPRINTF(output + offset, MAXLINE - offset, to);
+            c += flen - 1;
+            offset += tlen - 1;
+        } else {
+            output[offset] = *c;
         }
     }
+
+    if (offset >= MAXLINE)
+        offset = MAXLINE - 1;
+    output[offset] = '\0';
+    
+    return strdup(output);
 }
 
 
@@ -991,7 +980,7 @@
     MPstr_to_num(v->display, v->base, v->MPdisp_val);
 
     if (v->dtype == FIX) {
-        STRCPY(v->fnum, v->display);
+        STRNCPY(v->fnum, v->display, MAX_LOCALIZED - 1);
         ui_set_display(v->fnum, -1);
     }
 }
@@ -1051,7 +1040,7 @@
 {
     v->pointed = (strchr(v->display, '.') != NULL);
     if (!v->new_input) {
-        STRCPY(v->display, "1.0 +");
+        STRNCPY(v->display, "1.0 +", MAX_LOCALIZED - 1);
         v->new_input = v->pointed = 1;
     } else if (!v->pointed) {
         STRNCAT(v->display, ". +", 3);
@@ -1431,7 +1420,7 @@
 {
     if (!v->pointed) {
         if (v->toclear) {
-            STRCPY(v->display, ".");
+            STRNCPY(v->display, ".", MAX_LOCALIZED - 1);
             v->toclear = 0;
         } else {
             STRCAT(v->display, ".");

Modified: trunk/gcalctool/functions.h
==============================================================================
--- trunk/gcalctool/functions.h	(original)
+++ trunk/gcalctool/functions.h	Wed Mar 12 02:05:33 2008
@@ -25,7 +25,7 @@
 #include "calctool.h"
 
 void show_error(char *);
-void str_replace(char **, char *, char *);
+char *str_replace(char *, char *, char *);
 void syntaxdep_show_display();
 char *gc_strdup(char *str);
 int usable_num(int MPnum[MP_SIZE]);

Modified: trunk/gcalctool/get.c
==============================================================================
--- trunk/gcalctool/get.c	(original)
+++ trunk/gcalctool/get.c	Wed Mar 12 02:05:33 2008
@@ -333,22 +333,6 @@
     }
 }
 
-
-void
-read_str(char **str, char *value)
-{
-    if (*str != NULL) {
-        (void) free(*str);
-    }
-    if (value != NULL && strlen(value)) {
-        *str = (char *) malloc((unsigned) (strlen(value) + 1));
-        STRCPY(*str, value);
-    } else {
-        *str = NULL;
-    }
-}
-
-
 void
 resources_init()        /* Load gconf configuration database for gcalctool. */
 { 

Modified: trunk/gcalctool/get.h
==============================================================================
--- trunk/gcalctool/get.h	(original)
+++ trunk/gcalctool/get.h	Wed Mar 12 02:05:33 2008
@@ -58,6 +58,5 @@
 
 const char *get_radix();
 const char *get_tsep();
-void read_str(char **, char *);
 
 #endif /* GET_H */

Modified: trunk/gcalctool/gtk.c
==============================================================================
--- trunk/gcalctool/gtk.c	(original)
+++ trunk/gcalctool/gtk.c	Wed Mar 12 02:05:33 2008
@@ -725,7 +725,7 @@
 make_hostname()
 {
     Display *dpy = GDK_DISPLAY();
-    char client_hostname[MAXHOSTNAMELEN + 4];
+    char client_hostname[MAXLINE] = "";
     char hostname[MAXHOSTNAMELEN];
     char *display = DisplayString(dpy);
     char *scanner = display;
@@ -746,10 +746,8 @@
         strcmp(display, "localhost") &&     
         strcmp(display, "unix") &&          
         strcmp(display, "")) {              
-        SPRINTF(client_hostname, " [%s] ", hostname);
-    } else {                                
-        STRCPY(client_hostname, "");        
-    }                                       
+        SNPRINTF(client_hostname, MAXLINE, " [%s] ", hostname);
+    }
 
     *scanner = ':';
     
@@ -888,7 +886,7 @@
 {
     int bit_str_len, i, MP1[MP_SIZE], MP2[MP_SIZE];
     int MP[MP_SIZE];
-    char *bit_str, label[3], tmp[MAXLINE];
+    char *bit_str, label[MAXLINE], tmp[MAXLINE];
 
     switch (v->syntax) {
         case NPA:
@@ -903,9 +901,12 @@
                 if (bit_str_len <= MAXBITS) {
                     gtk_widget_set_sensitive(X->bit_panel, TRUE);
 
-                    STRCPY(label, " 0");
                     for (i = 0; i < MAXBITS; i++) {
-                        label[1] = (i < bit_str_len) ? bit_str[bit_str_len-i-1] : '0';
+                        if (i < bit_str_len) {
+                            SNPRINTF(label, MAXLINE, " %c", bit_str[bit_str_len-i-1]);
+                        } else {
+                            SNPRINTF(label, MAXLINE, " 0");
+                        }
                         gtk_label_set_text(GTK_LABEL(X->bits[MAXBITS - i - 1]), label);
                     }
 
@@ -928,9 +929,12 @@
                   if (bit_str_len <= MAXBITS) {
                       gtk_widget_set_sensitive(X->bit_panel, TRUE);
                       
-                      STRCPY(label, " 0");
                       for (i = 0; i < MAXBITS; i++) {
-                          label[1] = (i < bit_str_len) ? bit_str[bit_str_len-i-1] : '0';
+                          if (i < bit_str_len) {
+                              SNPRINTF(label, MAXLINE, " %c", bit_str[bit_str_len-i-1]);
+                          } else {
+                              SNPRINTF(label, MAXLINE, " 0");
+                          }
                           gtk_label_set_text(GTK_LABEL(X->bits[MAXBITS - i - 1]), label);
                       }
                   } else {
@@ -972,7 +976,7 @@
         str = " ";
     } else {
         if (v->noparens == 0) {
-            localize_number(localized, str);
+            localize_number(localized, str, MAX_LOCALIZED);
             str = localized;
         }
     }
@@ -2851,7 +2855,7 @@
         get_constant(n);
     }
     for (n = 0; n < MAX_FUNCTIONS; n++) {
-        STRCPY(v->fun_vals[n], "");    /* Initially empty function strings. */
+        STRNCPY(v->fun_vals[n], "", MAXLINE - 1);    /* Initially empty function strings. */
         get_function(n);
     }
 }

Modified: trunk/gcalctool/mp.c
==============================================================================
--- trunk/gcalctool/mp.c	(original)
+++ trunk/gcalctool/mp.c	Wed Mar 12 02:05:33 2008
@@ -1212,7 +1212,7 @@
     v->dtype = FIX;
     v->accuracy = MAX_DIGITS;
     mpcmf(&x[1], tmp);
-    STRCPY(disp, make_number(tmp, v->base, FALSE));
+    STRNCPY(disp, make_number(tmp, v->base, FALSE), MAXLINE - 1);
 
     if (disp[0] == '1') {
         y[ll+1]++;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]