tools: Fix realloc() use in fdarray__grow()
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.
---
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
@@ -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
Quoted 22 lines · Jann Horn
If
entries != NULL, thenfda->entrieshas been freed, so whatever happens afterwards, we must storeentriesinfda->entries. If we bail out at the secondrealloc(), the new allocation will be bigger than whatfda->nr_allocsays, but that's fine.Fixes 2171a9256862 ("tools lib fd array: Allow associating an integer cookie with each entry")Signed-off-by Jann Horn <[email protected]>---
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
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
Quoted 28 lines · Jiri Olsa
Quoted 27 lines · Jann Horn
If
entries != NULL, thenfda->entrieshas been freed, so whatever happens afterwards, we must storeentriesinfda->entries. If we bail out at the secondrealloc(), the new allocation will be bigger than whatfda->nr_allocsays, but that's fine.Fixes 2171a9256862 ("tools lib fd array: Allow associating an integer cookie with each entry")Signed-off-by Jann Horn <[email protected]>---
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@@ -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
-2priv =realloc (fda->priv, psize); -if (priv ==NULL ) { -free (entries);so we are sure we always call
fdarray__exiteven
if we fail in here? if that's the case thenAcked-by Jiri Olsa <[email protected]>
Ugh... actually, no, at least fdarray__new() does a plain free(). While other places like FDA_ADD use fdarray_delete()...