Use proper max in lwlib
[sxemacs] / src / ui / lwlib / lwlib.c
index 7d9b957..b23e76d 100644 (file)
@@ -97,10 +97,11 @@ static char *safe_strdup(const char *s)
        char *result;
        if (!s)
                return 0;
-       result = (char *)xmalloc(strlen(s) + 1);
+       ssize_t len = strlen(s)+1;
+       result = (char *)xmalloc(len);
        if (!result)
                return 0;
-       strcpy(result, s);
+       strncpy(result, s, len);
        return result;
 }
 
@@ -317,12 +318,15 @@ widget_value *replace_widget_value_tree(widget_value * node,
 
        if (!node || !newtree)
                abort();
+       else {
 
-       copy = copy_widget_value_tree(newtree, STRUCTURAL_CHANGE);
+               copy = copy_widget_value_tree(newtree, STRUCTURAL_CHANGE);
 
-       free_widget_value_contents(node);
-       *node = *copy;
-       free_widget_value(copy);        /* free the node, but not its contents. */
+               free_widget_value_contents(node);
+               *node = *copy;
+               /* free the node, but not its contents. */
+               free_widget_value(copy);
+       }
        return node;
 }
 
@@ -446,8 +450,7 @@ lw_map_widget_values(LWLIB_ID id, int (*mapfunc) (widget_value * value,
 
        if (!info)
                abort();
-
-       if (info->val)
+       else if (info->val)
                return map_widget_values(info->val, mapfunc, closure);
        return 0;
 }
@@ -497,6 +500,12 @@ static Boolean safe_strcmp(const char *s1, const char *s2)
        return (s1 && s2) ? strcmp(s1, s2) : s1 ? False : !!s2;
 }
 
+
+/* We only expect to use the following max function */
+#ifdef max
+#undef max
+#endif
+
 static change_type max(change_type i1, change_type i2)
 {
        return (int)i1 > (int)i2 ? i1 : i2;
@@ -895,10 +904,10 @@ static void instantiate_widget_instance(widget_instance * instance)
                fprintf(stderr, "No creation function for widget type %s\n",
                        instance->info->type);
                abort();
+       } else {
+               instance->widget = (*function) (instance);
        }
 
-       instance->widget = (*function) (instance);
-
        if (!instance->widget)
                abort();