Skip to contentSkip to conversation

tools: Fix realloc() use in fdarray__grow()

PATCH In review Jann Horn 2 participants 3 messages Feb 29, 2020 – Mar 2, 2020

lore
Share & tools
lore

Jann Horn

author
#0 lore raw

PATCH 1/1 tools: Fix realloc() use in fdarray__grow()

If entries != NULL, then fda->entries has been freed, so whatever happens afterwards, we must store entries in fda->entries. If we bail out at the second realloc(), the new allocation will be bigger than what fda->nr_alloc says, but that's fine.

Fixes 2171a9256862 ("tools lib fd array: Allow associating an integer cookie with each entry")

---
To the maintainer:
I'm not sure about the etiquette for using CC stable in patches for somewhat theoretical issues in userland tools; feel free to tack a CC stable onto this if you think it should go into stable.

 tools/lib/api/fd/array.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/lib/api/fd/array.c +2 -4
index 58d44d5eee31..acf8eca1a94a 100644
@@ -27,15 +27,13 @@ int fdarray__grow(struct fdarray *fda, int nr)
 
 	if (entries == NULL)
 		return -ENOMEM;
+	fda->entries = entries;
 
 	priv = realloc(fda->priv, psize);
-	if (priv == NULL) {
-		free(entries);
+	if (priv == NULL)
 		return -ENOMEM;
-	}
 
 	fda->nr_alloc = nr_alloc;
-	fda->entries  = entries;
 	fda->priv     = priv;
 	return 0;
 }
Signature · 1 line
2.25.0
Acked-by on v1 1/1
#1 lore raw
Quoted 22 lines · Jann Horn

If entries != NULL, then fda->entries has been freed, so whatever happens afterwards, we must store entries in fda->entries. If we bail out at the second realloc(), the new allocation will be bigger than what fda->nr_alloc says, but that's fine.

Fixes 2171a9256862 ("tools lib fd array: Allow associating an integer cookie with each entry")

---
To the maintainer:
I'm not sure about the etiquette for using CC stable in patches for somewhat theoretical issues in userland tools; feel free to tack a CC stable onto this if you think it should go into stable.

 tools/lib/api/fd/array.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/lib/api/fd/array.c +0 -0
index 58d44d5eee31..acf8eca1a94a 100644

tools/lib/api/fd/array.c int fdarray__grow(struct fdarray *fda, int nr) · line 33

Quoted 8 lines
tools/lib/api/fd/array.c +1 -2
@@ -27,15 +27,13 @@ int fdarray__grow(struct fdarray *fda, int nr)
 
 	if (entries == NULL)
 		return -ENOMEM;
+	fda->entries = entries;
 
 	priv = realloc(fda->priv, psize);
-	if (priv == NULL) {
-		free(entries);

so we are sure we always call fdarray__exit even
if we fail in here? if that's the case then

thanks,
jirka

Quoted 12 lines
tools/lib/api/fd/array.c +1 -2
+	if (priv == NULL)
 		return -ENOMEM;
-	}
 
 	fda->nr_alloc = nr_alloc;
-	fda->entries  = entries;
 	fda->priv     = priv;
 	return 0;
 }
Signature · 1 line
2.25.0
author
#2 lore raw
Quoted 28 lines · Jiri Olsa
Quoted 27 lines · Jann Horn

If entries != NULL, then fda->entries has been freed, so whatever happens afterwards, we must store entries in fda->entries. If we bail out at the second realloc(), the new allocation will be bigger than what fda->nr_alloc says, but that's fine.

Fixes 2171a9256862 ("tools lib fd array: Allow associating an integer cookie with each entry")

---
To the maintainer:
I'm not sure about the etiquette for using CC stable in patches for somewhat theoretical issues in userland tools; feel free to tack a CC stable onto this if you think it should go into stable.

 tools/lib/api/fd/array.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/lib/api/fd/array.c +1 -0
index 58d44d5eee31..acf8eca1a94a 100644
@@ -27,15 +27,13 @@ int fdarray__grow(struct fdarray *fda, int nr)
 
      if (entries == NULL)
              return -ENOMEM;
+     fda->entries = entries;
 
Quoted 8 lines

tools/lib/api/fd/array.c int fdarray__grow(struct fdarray *fda, int nr) · line 33

Quoted 3 lines
tools/lib/api/fd/array.c +0 -2
      priv = realloc(fda->priv, psize);
-     if (priv == NULL) {
-             free(entries);

so we are sure we always call fdarray__exit even
if we fail in here? if that's the case then

Ugh... actually, no, at least fdarray__new() does a plain free(). While other places like FDA_ADD use fdarray_delete()...