s4:dynconfig: fix segfault in the set_dyn_*() functions
authorStefan Metzmacher <metze@samba.org>
Thu, 24 Feb 2011 10:15:06 +0000 (11:15 +0100)
committerStefan Metzmacher <metze@samba.org>
Thu, 24 Feb 2011 10:44:19 +0000 (11:44 +0100)
We should not try to call free on a const string (the default one).
Now we make sure that the dyn_* variable is never NULL
and only allocated if it's not the default value.

metze

source4/dynconfig/dynconfig.c

index 45b7b737208ae5902be1abb4a1094e65e20b70cf..65e57e0fc174f2ac3e43aea20dc090e82e79cbe1 100644 (file)
 #define DEFINE_DYN_CONFIG_PARAM(name) \
 const char *dyn_##name = name; \
 \
- const char *get_dyn_##name(void) \
+bool is_default_dyn_##name(void) \
 {\
-       if (dyn_##name == NULL) {\
-               return name;\
-       }\
-       return dyn_##name;\
+       if (strcmp(name, dyn_##name) == 0) { \
+               return true; \
+       } \
+       return false; \
 }\
 \
- const char *set_dyn_##name(const char *newpath) \
+const char *get_dyn_##name(void) \
 {\
-       if (dyn_##name) {\
-               free(discard_const(dyn_##name));        \
-       }\
-       dyn_##name = strdup(newpath);\
        return dyn_##name;\
 }\
- bool is_default_dyn_##name(void) \
+\
+const char *set_dyn_##name(const char *newpath) \
 {\
-       return (dyn_##name == NULL);\
+       if (newpath == NULL) { \
+               return NULL; \
+       } \
+       if (strcmp(name, newpath) == 0) { \
+               return dyn_##name; \
+       } \
+       newpath = strdup(newpath);\
+       if (newpath == NULL) { \
+               return NULL; \
+       } \
+       if (is_default_dyn_##name()) { \
+               /* do not free a static string */ \
+       } else if (dyn_##name) {\
+               free(discard_const(dyn_##name)); \
+       }\
+       dyn_##name = newpath; \
+       return dyn_##name;\
 }
 
 /* these are in common with s3 */