Bugfix for safeStrCpy patch for XBoard
authorH.G. Muller <h.g.muller@hccnet.nl>
Mon, 8 Nov 2010 22:03:13 +0000 (23:03 +0100)
committerH.G. Muller <h.g.muller@hccnet.nl>
Tue, 9 Nov 2010 13:39:28 +0000 (14:39 +0100)
By a trick (temporarily redefining safeStrCpy in common.h as
((char*) (a=1/(4-3rdArgument))) ) a compile-time warning (division by 0)
could be generated for every occurrence where the size argument was equal
to 4, i.e. where the 1st argument was likely a pointer rather than an
array. These cases are now fixed. The file xoptions.c, which is the
only ne that does not #include common.h had to be fixed by making
the #define in the file itself.

backend.c
book.c
xboard.c
xhistory.c
xoptions.c

index 25fe5f5..a3a06b7 100644 (file)
--- a/backend.c
+++ b/backend.c
@@ -13328,7 +13328,7 @@ if(appData.debugMode) fprintf(debugFP, "Append: in='%s' %d\n", text, addBraces);
     } else {
        commentList[index] = (char *) malloc(len + 6); // perhaps wastes 4...
        if(addBraces)
-         safeStrCpy(commentList[index], "{\n", sizeof(commentList[index])/sizeof(commentList[index][0]));
+         safeStrCpy(commentList[index], "{\n", 3);
        else commentList[index][0] = NULLCHAR;
        strcat(commentList[index], text);
        strcat(commentList[index], "\n");
diff --git a/book.c b/book.c
index e19ef0b..bd3a49f 100644 (file)
--- a/book.c
+++ b/book.c
@@ -445,13 +445,13 @@ void move_to_string(char move_s[6], uint16 move)
     // correct FRC-style castlings in variant normal. 
     // [HGM] This is buggy code! e1h1 could very well be a normal R or Q move.
     if(!strcmp(move_s,"e1h1")){
-      safeStrCpy(move_s,"e1g1", sizeof(move_s)/sizeof(move_s[0]));
+      safeStrCpy(move_s,"e1g1", 6);
     }else  if(!strcmp(move_s,"e1a1")){
-      safeStrCpy(move_s,"e1c1", sizeof(move_s)/sizeof(move_s[0]));
+      safeStrCpy(move_s,"e1c1", 6);
     }else  if(!strcmp(move_s,"e8h8")){
-      safeStrCpy(move_s,"e8g8", sizeof(move_s)/sizeof(move_s[0]));
+      safeStrCpy(move_s,"e8g8", 6);
     }else  if(!strcmp(move_s,"e8a8")){
-      safeStrCpy(move_s,"e8c8", sizeof(move_s)/sizeof(move_s[0]));
+      safeStrCpy(move_s,"e8c8", 6);
     }
 }
 
index 67992f1..d1d00a3 100644 (file)
--- a/xboard.c
+++ b/xboard.c
@@ -5815,7 +5815,7 @@ SendPositionSelection(Widget w, Atom *selection, Atom *target,
      * automatically call XtFree on the value returned.  So have to
      * make a copy of it allocated with XtMalloc */
     selection_tmp= XtMalloc(strlen(selected_fen_position)+16);
-    safeStrCpy(selection_tmp, selected_fen_position, sizeof(selection_tmp)/sizeof(selection_tmp[0]) );
+    safeStrCpy(selection_tmp, selected_fen_position, strlen(selected_fen_position)+16 );
 
     *value_return=selection_tmp;
     *length_return=strlen(selection_tmp);
index d7d3e7f..eb86538 100644 (file)
@@ -234,10 +234,10 @@ void HistorySet(char movelist[][2*MOVE_LEN],int first,int last,int current){
            strncpy(hist->white[i/2+1], movelist[i], p-movelist[i]);
            hist->white[i/2+1][p-movelist[i]] = NULLCHAR;
          } else {
-           safeStrCpy(hist->white[i/2+1],movelist[i], sizeof(hist->white[i/2+1])/sizeof(hist->white[i/2+1][0]));
+           safeStrCpy(hist->white[i/2+1],movelist[i], MOVE_LEN);
          }
        } else {
-         safeStrCpy(hist->white[i/2+1],dots, sizeof(hist->white[i/2+1])/sizeof(hist->white[i/2+1][0]));
+         safeStrCpy(hist->white[i/2+1],dots, MOVE_LEN);
        }
       } else {
        if(movelist[i][0]) {
@@ -246,14 +246,14 @@ void HistorySet(char movelist[][2*MOVE_LEN],int first,int last,int current){
            strncpy(hist->black[i/2+1], movelist[i], p-movelist[i]);
            hist->black[i/2+1][p-movelist[i]] = NULLCHAR;
          } else {
-           safeStrCpy(hist->black[i/2+1],movelist[i], sizeof(hist->black[i/2+1])/sizeof(hist->black[i/2+1][0]));
+           safeStrCpy(hist->black[i/2+1],movelist[i], MOVE_LEN);
          }
        } else {
-         safeStrCpy(hist->black[i/2+1],"", sizeof(hist->black[i/2+1])/sizeof(hist->black[i/2+1][0]));
+         safeStrCpy(hist->black[i/2+1],"", MOVE_LEN);
        }
       }
     }
-    safeStrCpy(hist->black[last/2+1],"", sizeof(hist->black[last/2+1])/sizeof(hist->black[last/2+1][0]));
+    safeStrCpy(hist->black[last/2+1],"", MOVE_LEN);
     b=first/2;
     m=(last+3)/2-b;
     XawFormDoLayout(hist->vbox, False);
@@ -405,8 +405,8 @@ Widget HistoryCreate()
     CatchDeleteWindow(hist->sh, "HistoryPopDown");
 
     for(i=1;i<hist->aNr;i++){
-      safeStrCpy(hist->white[i],dots, sizeof(hist->white[i])/sizeof(hist->white[i][0]));
-      safeStrCpy(hist->black[i],"", sizeof(hist->black[i])/sizeof(hist->black[i][0]));
+      safeStrCpy(hist->white[i],dots, MOVE_LEN);
+      safeStrCpy(hist->black[i],"", MOVE_LEN);
      }
 
     if(wpMoveHistory.width > 0) {
index b5368a3..2f58c7b 100644 (file)
@@ -1432,7 +1432,7 @@ void SettingsCallback(w, client_data, call_data)
                    XtSetArg(args[0], XtNstring, &val);
                    XtGetValues(currentCps->option[i].handle, args, 1);
                    if(strcmp(currentCps->option[i].textValue, val)) {
-                     safeStrCpy(currentCps->option[i].textValue, val, sizeof(currentCps->option[i].textValue)/sizeof(currentCps->option[i].textValue[0]));
+                     safeStrCpy(currentCps->option[i].textValue, val, MSG_SIZ - (currentCps->option[i].textValue - currentCps->option[i].name) );
                      snprintf(buf, MSG_SIZ,  "option %s=%s\n", currentCps->option[i].name, val);
                      SendToProgram(buf, currentCps);
                    }