* x-struct-str.c: It's possible for struct members to be NULL, so make
authorJonathan Pryor <jpryor@novell.com>
Fri, 20 Mar 2009 16:32:43 +0000 (16:32 -0000)
committerJonathan Pryor <jpryor@novell.com>
Fri, 20 Mar 2009 16:32:43 +0000 (16:32 -0000)
  sure we don't pass NULL to strlen(3).

svn path=/trunk/mono/; revision=129882

support/ChangeLog
support/x-struct-str.c

index 6eeb5152621394df926a3dcb0e4827967e0ee85c..7c2b9fac500c77e34c6e56321ec95022048d8db1 100644 (file)
@@ -1,3 +1,8 @@
+2009-03-20  Jonathan Pryor  <jpryor@novell.com>
+
+       * x-struct-str.c: It's possible for struct members to be NULL, so make 
+         sure we don't pass NULL to strlen(3).
+
 2009-02-20  Jonathan Pryor  <jpryor@novell.com>
 
        * map.h: Flush (adds Mono_Unix_UnixSignal_SignalInfo::pipecnt, removes
index 89d5a61b120b6227f36aaad323efd33810fcf631..b71a9cca1f0a153b54f5b7007b49927fcb98692e 100644 (file)
@@ -39,7 +39,8 @@ _mph_copy_structure_strings (
 
        buflen = num_strings;
        for (i = 0; i < num_strings; ++i) {
-               len[i] = strlen (str_at(from, from_offsets[i]));
+               const char* s = str_at(from, from_offsets[i]);
+               len [i] = s ? strlen (s) : 0;
                if (len[i] < INT_MAX - buflen)
                        buflen += len[i];
                else
@@ -64,6 +65,12 @@ _mph_copy_structure_strings (
 
 #ifdef TEST
 
+/*
+ * To run the tests:
+ * $ gcc -DTEST -I.. `pkg-config --cflags --libs glib-2.0` x-struct-str.c
+ * $ ./a.out
+ */
+
 #include <stdio.h>
 
 struct foo {
@@ -105,6 +112,13 @@ main ()
        printf ("b.c=%s\n", b.c);
        printf ("b.e=%s\n", b.e);
 
+       f.c = NULL;
+       buf = _mph_copy_structure_strings (&b, bar_offsets, 
+                       &f, foo_offsets, 3);
+       printf ("b.a=%s\n", b.a);
+       printf ("b.c=%s\n", b.c);
+       printf ("b.e=%s\n", b.e);
+
        return 0;
 }
 #endif