Skip to contentSkip to conversation

column: use xalloc libs

PATCH Applied Dave Reisner 3 participants 7 messages Dec 20, 2011 – Jan 4, 2012 Util-Linux package

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 column: use xalloc libs

---
 text-utils/column.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
text-utils/column.c +1 -1
index 79d2842..c642680 100644
@@ -395,7 +395,7 @@ static wchar_t *mbs_to_wcs(const char *s)
 	n = mbstowcs((wchar_t *)0, s, 0);
 	if (n < 0)
 		return NULL;
-	wcs = malloc((n + 1) * sizeof(wchar_t));
+	wcs = xmalloc((n + 1) * sizeof(wchar_t));
 	if (!wcs)
 		return NULL;
 	n = mbstowcs(wcs, s, n + 1);
Signature · 1 line
1.7.8
#1 lore raw

PATCH 2/3 lscpu: use xalloc libs

---
 sys-utils/lscpu.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
sys-utils/lscpu.c +2 -2
index 72b56d2..0a7841d 100644
@@ -363,9 +363,9 @@ read_basicinfo(struct lscpu_desc *desc, struct lscpu_modifier *mod)
 	if (desc->flags) {
 		snprintf(buf, sizeof(buf), " %s ", desc->flags);
 		if (strstr(buf, " svm "))
-			desc->virtflag = strdup("svm");
+			desc->virtflag = xstrdup("svm");
 		else if (strstr(buf, " vmx "))
-			desc->virtflag = strdup("vmx");
+			desc->virtflag = xstrdup("vmx");
 		if (strstr(buf, " lm "))
 			desc->mode |= MODE_32BIT | MODE_64BIT;		/* x86_64 */
 		if (strstr(buf, " zarch "))
Signature · 1 line
1.7.8
maintainer Applied
#2 lore raw

Re: [PATCH 2/3] lscpu: use xalloc libs

Quoted 2 lines · Dave Reisner
 sys-utils/lscpu.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

Applied, thanks.

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

PATCH 3/3 tools/checkxalloc.sh: add new code checking script

In reply to Dave Reisner

Finds usage of strdup, malloc, calloc, and realloc when xalloc.h is included.

http://marc.info/?l=util-linux-ng&m=132438338929925&w=2

---
Ask and you shall receive ;)

 tools/checkxalloc.sh |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
 create mode 100755 tools/checkxalloc.sh
tools/checkxalloc.sh new file +14 -0
new file mode 100755
index 0000000..f8f8c8c
@@ -0,0 +1,14 @@
+#!/bin/sh
+#
+# Find files which include the xalloc.h header, but which still call
+# the unwrapped calloc and malloc.
+#
+
+cd "$(git rev-parse --show-toplevel)" || {
+  echo "error: failed to chdir to git root"
+  exit 1
+}
+
+git grep -zl '#include "xalloc.h"' |
+  xargs -0 grep -nwE '[^x](([cm]|re)alloc|strdup)\('
+
Signature · 1 line
1.7.8
maintainer Applied
#4 lore raw

Re: [PATCH 3/3] tools/checkxalloc.sh: add new code checking script

Quoted 1 line · Dave Reisner

Ask and you shall receive ;)

Thanks :-)

Quoted 13 lines
 tools/checkxalloc.sh |   14 ++++++++++++++
 1 files changed, 14 insertions(+), 0 deletions(-)
 create mode 100755 tools/checkxalloc.sh
tools/checkxalloc.sh new file +3 -0
new file mode 100755
index 0000000..f8f8c8c
@@ -0,0 +1,14 @@
+#!/bin/sh
+#
+# Find files which include the xalloc.h header, but which still call

tools/checkxalloc.sh line 13

Quoted 10 lines
tools/checkxalloc.sh new file +10 -0
+# the unwrapped calloc and malloc.
+#
+
+cd "$(git rev-parse --show-toplevel)" || {
+  echo "error: failed to chdir to git root"
+  exit 1
+}
+
+git grep -zl '#include "xalloc.h"' |
+  xargs -0 grep -nwE '[^x](([cm]|re)alloc|strdup)\('

Applied, with small improvement -- we need a proper return value (xargs returns too funny numbers) to make it usable from top-level Makefile.

Karel

Signature · 2 lines
 Karel Zak  <[email protected]>
 http://karelzak.blogspot.com
#5 lore raw
In reply to Dave Reisner
Quoted 10 lines · Dave Reisner
---
 text-utils/column.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
text-utils/column.c +0 -0
index 79d2842..c642680 100644

text-utils/column.c static wchar_t *mbs_to_wcs(const char *s) · line 400

Quoted 7 lines
text-utils/column.c +1 -1
@@ -395,7 +395,7 @@ static wchar_t *mbs_to_wcs(const char *s)
 	n = mbstowcs((wchar_t *)0, s, 0);
 	if (n < 0)
 		return NULL;
-	wcs = malloc((n + 1) * sizeof(wchar_t));
+	wcs = xmalloc((n + 1) * sizeof(wchar_t));
 	if (!wcs)
 		return NULL;

Hmm this isn't that straight forward, if the memory allocation fails here we don't necessarily abort the program.

Quoted 1 line
text-utils/column.c +0 -0
 	n = mbstowcs(wcs, s, n + 1);
maintainer Applied
#6 lore raw
Quoted 3 lines · Davidlohr Bueso
Quoted 2 lines · Dave Reisner
 text-utils/column.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

Applied, thanks.

Quoted 7 lines

text-utils/column.c static wchar_t *mbs_to_wcs(const char *s) · line 400

Quoted 4 lines
text-utils/column.c +1 -1
-	wcs = malloc((n + 1) * sizeof(wchar_t));
+	wcs = xmalloc((n + 1) * sizeof(wchar_t));
 	if (!wcs)
 		return NULL;

Hmm this isn't that straight forward, if the memory allocation fails here we don't necessarily abort the program.

Fixed.

Karel

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