Skip to contentSkip to conversation

xalloc: check for NULL before calling strdup

PATCH Applied Dave Reisner 2 participants 4 messages Sep 29, 2011 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 xalloc: check for NULL before calling strdup

This fixes a segfault in mount (and possibly elsewhere) when invoked without a -t parameter.

Broken in 7ef9fd7 when the common xalloc.h libs were introduced.

---
 include/xalloc.h |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)
include/xalloc.h +7 -2
index 8c505be..bea7e31 100644
@@ -51,9 +51,14 @@ void *xcalloc(const size_t nelems, const size_t size)
 
 static inline char *xstrdup(const char *str)
 {
-	char *ret = strdup(str);
+	char *ret;
 
-	if (!ret && str)
+	if (!str)
+		return NULL;
+
+	ret = strdup(str);
+
+	if (!ret)
 		err(XALLOC_EXIT_CODE, "cannot duplicate string");
 	return ret;
 }
Signature · 1 line
1.7.6.4
#1 lore raw

PATCH 2/2 xalloc: fix whitespace to be consistent

We use spaces for the rest of this file, so the random tabs within xstrdup stand out.

---
 include/xalloc.h |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)
include/xalloc.h +7 -7
index bea7e31..feeb114 100644
@@ -51,16 +51,16 @@ void *xcalloc(const size_t nelems, const size_t size)
 
 static inline char *xstrdup(const char *str)
 {
-	char *ret;
+        char *ret;
 
-	if (!str)
-		return NULL;
+        if (!str)
+                return NULL;
 
-	ret = strdup(str);
+        ret = strdup(str);
 
-	if (!ret)
-		err(XALLOC_EXIT_CODE, "cannot duplicate string");
-	return ret;
+        if (!ret)
+                err(XALLOC_EXIT_CODE, "cannot duplicate string");
+        return ret;
 }
 
 #endif
Signature · 1 line
1.7.6.4
maintainer Applied
#2 lore raw

Re: [PATCH 2/2] xalloc: fix whitespace to be consistent

Quoted 2 lines · Dave Reisner
 include/xalloc.h |   14 +++++++-------
 1 files changed, 7 insertions(+), 7 deletions(-)

Applied, thanks.

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
maintainer Applied
#3 lore raw
In reply to Dave Reisner
Quoted 2 lines · Dave Reisner
 include/xalloc.h |    9 +++++++--
 1 files changed, 7 insertions(+), 2 deletions(-)

Applied, thanks.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com