]> git.xonotic.org Git - xonotic/gmqcc.git/commitdiff
Fix option string allocated/non allocated storage.
authorDale Weiler <killfieldengine@gmail.com>
Thu, 17 Oct 2013 00:14:49 +0000 (20:14 -0400)
committerDale Weiler <killfieldengine@gmail.com>
Thu, 17 Oct 2013 00:14:49 +0000 (20:14 -0400)
gmqcc.h
main.c
opts.c

diff --git a/gmqcc.h b/gmqcc.h
index 7ebb1c6ce091fe2a81b5054e24a883f4a8ee0599..7ad9a28319dfe26b447320b3bb32cfa1fe9bc656 100644 (file)
--- a/gmqcc.h
+++ b/gmqcc.h
@@ -1012,11 +1012,19 @@ typedef enum {
     COMPILER_GMQCC    /* this   QuakeC */
 } opts_std_t;
 
-typedef union {
-    bool     B;
-    uint16_t U16;
-    uint32_t U32;
-    char    *STR;
+typedef struct {
+    union {
+        bool     b;
+        uint16_t u16;
+        uint32_t u32;
+
+        union {
+            char       *p;
+            const char *c;
+        } str;
+    } data;
+
+    bool allocated;
 } opt_value_t;
 
 
@@ -1038,9 +1046,11 @@ extern opts_cmd_t opts;
 #define OPTS_WARN(i)         OPTS_GENERIC(opts.warn,         (i))
 #define OPTS_WERROR(i)       OPTS_GENERIC(opts.werror,       (i))
 #define OPTS_OPTIMIZATION(i) OPTS_GENERIC(opts.optimization, (i))
-#define OPTS_OPTION_BOOL(X) (opts.options[X].B)
-#define OPTS_OPTION_U16(X)  (opts.options[X].U16)
-#define OPTS_OPTION_U32(X)  (opts.options[X].U32)
-#define OPTS_OPTION_STR(X)  (opts.options[X].STR)
+#define OPTS_OPTION_DUPED(X) (opts.options[X].allocated)
+#define OPTS_OPTION_BOOL(X) (opts.options[X].data.b)
+#define OPTS_OPTION_U16(X)  (opts.options[X].data.u16)
+#define OPTS_OPTION_U32(X)  (opts.options[X].data.u32)
+#define OPTS_OPTION_DUP(X) *(OPTS_OPTION_DUPED(X)=true, &(opts.options[X].data.str.p))
+#define OPTS_OPTION_STR(X)  (opts.options[X].data.str.c)
 
 #endif /*! GMQCC_HDR */
diff --git a/main.c b/main.c
index f9a71e26240e93cf1513f6d9bd2fafed4a1c05c8..78293d6d7615a3682a6153ff6b221381e5dc44cf 100644 (file)
--- a/main.c
+++ b/main.c
@@ -557,7 +557,6 @@ static bool progs_nextline(char **out, size_t *alen, fs_file_t *src) {
 int main(int argc, char **argv) {
     size_t          itr;
     int             retval           = 0;
-    bool            opts_output_free = false;
     bool            operators_free   = false;
     bool            progs_src        = false;
     fs_file_t       *outfile         = NULL;
@@ -690,8 +689,7 @@ int main(int argc, char **argv) {
                 item.type     = TYPE_QC;
                 vec_push(items, item);
             } else if (!opts_output_wasset) {
-                OPTS_OPTION_STR(OPTION_OUTPUT) = util_strdup(line);
-                opts_output_free               = true;
+                OPTS_OPTION_DUP(OPTION_OUTPUT) = util_strdup(line);
                 hasline                        = true;
             }
         }
@@ -780,8 +778,12 @@ cleanup:
 
     if (!OPTS_OPTION_BOOL(OPTION_PP_ONLY))
         if(parser) parser_cleanup(parser);
-    if (opts_output_free)
-        mem_d(OPTS_OPTION_STR(OPTION_OUTPUT));
+
+    /* free allocated option strings */
+    for (itr = 0; itr < OPTION_COUNT; itr++)
+        if (OPTS_OPTION_DUPED(itr))
+            mem_d(OPTS_OPTION_STR(itr));
+
     if (operators_free)
         mem_d((void*)operators);
 
diff --git a/opts.c b/opts.c
index 23a0f4c827fd9c3e84663074fe0f0b8be14fcf53..e50bd7441791925861f046a3420b1d90e6d0454a 100644 (file)
--- a/opts.c
+++ b/opts.c
@@ -60,7 +60,7 @@ opts_cmd_t   opts; /* command line options */
 static void opts_setdefault(void) {
     memset(&opts, 0, sizeof(opts_cmd_t));
     OPTS_OPTION_BOOL(OPTION_CORRECTION) = true;
-    OPTS_OPTION_STR(OPTION_PROGSRC) = (char*)"progs.src";
+    OPTS_OPTION_STR(OPTION_PROGSRC)     = "progs.src";
 
     /* warnings */
     opts_set(opts.warn,  WARN_UNUSED_VARIABLE,           true);
@@ -129,7 +129,7 @@ void opts_restore_non_Werror_all() {
 void opts_init(const char *output, int standard, size_t arraysize) {
     opts_setdefault();
 
-    OPTS_OPTION_STR(OPTION_OUTPUT)         = (char*)output;
+    OPTS_OPTION_STR(OPTION_OUTPUT)         = output;
     OPTS_OPTION_U32(OPTION_STANDARD)       = standard;
     OPTS_OPTION_U32(OPTION_MAX_ARRAY_SIZE) = arraysize;
     OPTS_OPTION_U16(OPTION_MEMDUMPCOLS)    = 16;