[PATCH 7/7] Fix gcc -Wwrite-strings warnings in xf86Modes code

Alan Coopersmith alan.coopersmith at oracle.com
Wed Nov 9 20:53:21 PST 2011


On 11/09/11 16:47, Alan Coopersmith wrote:
> On 11/09/11 05:54, walter harms wrote:
>> this looks like a case for asprintf()
>
> Yes it does, though that wouldn't belong in this patch, since it has nothing
> to do with clearing the -Wwrite-strings warnings. Separate patch to come.

After further analysis, I've changed my mind.  The conversion is easy:

diff --git a/hw/xfree86/modes/xf86Modes.c b/hw/xfree86/modes/xf86Modes.c
index 49cc149..ea172d4 100644
--- a/hw/xfree86/modes/xf86Modes.c
+++ b/hw/xfree86/modes/xf86Modes.c
@@ -274,9 +274,11 @@ xf86ModesEqual(const DisplayModeRec *pMode1, const DisplayM
  static void
  add(char **p, const char *new)
  {
-    *p = xnfrealloc(*p, strlen(*p) + strlen(new) + 2);
-    strcat(*p, " ");
-    strcat(*p, new);
+    char *res;
+
+    XNFasprintf(&res, "%s %s", *p, new);
+    free(*p);
+    *p = res;
  }

  /**


but replacing the realloc with a new malloc/free each time is fairly certain
to lead to increased memory fragmentation, and the code isn't really any
clearer or better than before, so I'm going to pass on this one.

There's a bunch of ways we could totally rework the calling code, but for
just pasting together a bunch of strings for a log message that we print
then free, once at startup, it's just not worth the effort for a major refactoring.


-- 
	-Alan Coopersmith-        alan.coopersmith at oracle.com
	 Oracle Solaris Platform Engineering: X Window System



More information about the xorg-devel mailing list