From c28f9557f19deb9621b179d3c6ccbdf0458bd304 Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Mon, 8 Nov 2010 23:03:13 +0100 Subject: [PATCH] Bugfix for safeStrCpy patch for XBoard 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 | 2 +- book.c | 8 ++++---- xboard.c | 2 +- xhistory.c | 14 +++++++------- xoptions.c | 2 +- 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/backend.c b/backend.c index 25fe5f5..a3a06b7 100644 --- 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 --- 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); } } diff --git a/xboard.c b/xboard.c index 67992f1..d1d00a3 100644 --- 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); diff --git a/xhistory.c b/xhistory.c index d7d3e7f..eb86538 100644 --- a/xhistory.c +++ b/xhistory.c @@ -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;iaNr;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) { diff --git a/xoptions.c b/xoptions.c index b5368a3..2f58c7b 100644 --- a/xoptions.c +++ b/xoptions.c @@ -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); } -- 1.7.0.4