Fix usage of memory after free in -replace option
authorH.G.Muller <hgm@hgm-xboard.(none)>
Fri, 13 Jan 2017 12:26:44 +0000 (13:26 +0100)
committerH.G.Muller <hgm@hgm-xboard.(none)>
Fri, 13 Jan 2017 15:39:25 +0000 (16:39 +0100)
The code for implementing the -replace option was using a string after
its memory was freed, which might not work in all allocation systems.

args.h

diff --git a/args.h b/args.h
index 1da2448..fbdd498 100644 (file)
--- a/args.h
+++ b/args.h
@@ -1186,9 +1186,9 @@ ParseArgs(GetFunc get, void *cl)
         break;
       }
       if(replace) { // previous -replace option makes this string option conditional
-       char *p = (char*) replace;
+       int differs = strcmp(*(char**) ad->argLoc, (char*) replace);
        free(replace); replace = NULL; // but expires in the process
-        if(strcmp(*(char**) ad->argLoc, p)) break; // only use to replace the given string
+        if(differs) break; // only use to replace the given string
       }
       ASSIGN(*(char **) ad->argLoc, argValue);
       break;