Skip to contentSkip to conversation

Avoid use of GNU-specific memmem().

PATCH Alex Williamson 1 participant 2 messages Jan 5, 2007 – Jan 8, 2007 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw
Quoted 10 lines · Xen staging patchbot-unstable

# HG changeset patch
# User [email protected]
# Date 1167905854 0
# Node ID bdbfbfdfbd64ddedc775d55f1a9e842847fea7b2 # Parent 60f91c9f1a248491e2f216d009a27a4c7e5e67d4 [XEN] Avoid use of GNU-specific memmem().

xen/tools/symbols.c static void build_initial_tok_table(void · line 359

Quoted 10 lines
xen/tools/symbols.c +7 -0
@@ -350,6 +350,14 @@ static void build_initial_tok_table(void
 	table_cnt = pos;
 }
 
+static void *memmem_pvt(void *h, size_t hlen, void *n, size_t nlen)
+{
+	char *p;
+	for (p = h; (p - (char *)h) <= (hlen - nlen); p++)
+		if (!memcmp(p, n, nlen)) return p;
+	return NULL;
+}

size_t is unsigned, (hlen - nlen) can wrap and cause a segfault. The
patch below fixes it. Thanks,

Alex

---

xen/tools/symbols.c +1 -1
diff -r a8930b548048 xen/tools/symbols.c
@@ -353,7 +353,7 @@ static void *memmem_pvt(void *h, size_t 
 static void *memmem_pvt(void *h, size_t hlen, void *n, size_t nlen)
 {
 	char *p;
-	for (p = h; (p - (char *)h) <= (hlen - nlen); p++)
+	for (p = h; (p - (char *)h) <= (long)(hlen - nlen); p++)
 		if (!memcmp(p, n, nlen)) return p;
 	return NULL;
 }
#2 lore raw

Hi Keir,

Below is an alternative patch that perhaps more clearly illustrates
the problem with this memmem() replacement. This segfault occurs when parsing the nm output on ia64. Thanks,

Alex

Quoted 34 lines · Alex Williamson
Quoted 10 lines · Xen staging patchbot-unstable

# HG changeset patch
# User [email protected]
# Date 1167905854 0
# Node ID bdbfbfdfbd64ddedc775d55f1a9e842847fea7b2 # Parent 60f91c9f1a248491e2f216d009a27a4c7e5e67d4 [XEN] Avoid use of GNU-specific memmem().

xen/tools/symbols.c static void build_initial_tok_table(void · line 359

Quoted 10 lines
xen/tools/symbols.c +7 -0
@@ -350,6 +350,14 @@ static void build_initial_tok_table(void
 	table_cnt = pos;
 }
 
+static void *memmem_pvt(void *h, size_t hlen, void *n, size_t nlen)
+{
+	char *p;
+	for (p = h; (p - (char *)h) <= (hlen - nlen); p++)
+		if (!memcmp(p, n, nlen)) return p;
+	return NULL;
+}

size_t is unsigned, (hlen - nlen) can wrap and cause a segfault. The
patch below fixes it. Thanks,

Alex

---

diff -r a8930b548048 xen/tools/symbols.c

xen/tools/symbols.c static void *memmem_pvt(void *h, size_t · line 359

Quoted 8 lines
xen/tools/symbols.c +1 -1
@@ -353,7 +353,7 @@ static void *memmem_pvt(void *h, size_t 
 static void *memmem_pvt(void *h, size_t hlen, void *n, size_t nlen)
 {
 	char *p;
-	for (p = h; (p - (char *)h) <= (hlen - nlen); p++)
+	for (p = h; (p - (char *)h) <= (long)(hlen - nlen); p++)
 		if (!memcmp(p, n, nlen)) return p;
 	return NULL;
 }

---

xen/tools/symbols.c +2 -0
diff -r a8930b548048 xen/tools/symbols.c
@@ -353,6 +353,8 @@ static void *memmem_pvt(void *h, size_t 
 static void *memmem_pvt(void *h, size_t hlen, void *n, size_t nlen)
 {
 	char *p;
+	if (nlen > hlen)
+		return NULL;
 	for (p = h; (p - (char *)h) <= (hlen - nlen); p++)
 		if (!memcmp(p, n, nlen)) return p;
 	return NULL;