summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Layton <jlayton@poochiereds.net>2014-05-01 11:15:16 -0400
committerRichard Maw <richard.maw@codethink.co.uk>2015-04-23 06:13:51 +0000
commit0da9f20a22bb32b2da6b587b4e85dafde087c0f7 (patch)
tree2fac05f8681d4cd0e0af2689b7a894f34062658e
parent08baf3bc2f66a1ca6401191e19380028d2fc2c6d (diff)
downloadnfs-utils-baserock/master.tar.gz
mountd: fix segfault in add_name with newer gcc compilersbaserock/master
I hit a segfault in add_name with a mountd built with gcc-4.9.0. Some NULL pointer checks got reordered such that a pointer was dereferenced before checking to see whether it was NULL. The problem was due to nfs-utils relying on undefined behavior, which tricked gcc into assuming that the pointer would never be NULL. At first I assumed that this was a compiler bug, but Jakub Jelinek and Jeff Law pointed out: "If old is NULL, then: strncpy(new, old, cp-old); is undefined behavior (even when cp == old == NULL in that case), therefore gcc assumes that old is never NULL, as otherwise it would be invalid. Just guard strncpy(new, old, cp-old); new[cp-old] = 0; with if (old) { ... }." This patch does that. If old is NULL though, then we still need to ensure that new is NULL terminated, lest the subsequent strcats walk off the end of it. Cc: Jeff Law <law@redhat.com> Cc: Jakub Jelinek <jakub@redhat.com> Signed-off-by: Jeff Layton <jlayton@poochiereds.net> Signed-off-by: Steve Dickson <steved@redhat.com>
-rw-r--r--support/export/client.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/support/export/client.c b/support/export/client.c
index ba2db8f..e749cac 100644
--- a/support/export/client.c
+++ b/support/export/client.c
@@ -482,8 +482,12 @@ add_name(char *old, const char *add)
else
cp = cp + strlen(cp);
}
- strncpy(new, old, cp-old);
- new[cp-old] = 0;
+ if (old) {
+ strncpy(new, old, cp-old);
+ new[cp-old] = 0;
+ } else {
+ new[0] = 0;
+ }
if (cp != old && !*cp)
strcat(new, ",");
strcat(new, add);