Skip to contentSkip to conversation

debugfs: fix memory leak problem in read_list()

PATCH In review Zhiqiang Liu 3 participants 6 messages Feb 20, 2021 – Feb 26, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 debugfs: fix memory leak problem in read_list()

In read_list func, if strtoull() fails in while loop, we will return the error code directly. Then, memory of variable lst will be leaked without setting to *list.

---
 debugfs/util.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
debugfs/util.c +8 -4
index be6b550e..9e880548 100644
@@ -530,12 +530,16 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
 		errno = 0;
 		y = x = strtoull(tok, &e, 0);
-		if (errno)
-			return errno;
+		if (errno) {
+			retval = errno;
+			break;
+		}
 		if (*e == '-') {
 			y = strtoull(e + 1, NULL, 0);
-			if (errno)
-				return errno;
+			if (errno) {
+				retval = errno;
+				break;
+			}
 		} else if (*e != 0) {
 			retval = EINVAL;
 			break;
Signature · 1 line
2.19.1
#1 lore raw

friendly ping ...

Quoted 37 lines · Zhiqiang Liu

In read_list func, if strtoull() fails in while loop, we will return the error code directly. Then, memory of variable lst will be leaked without setting to *list.

---
 debugfs/util.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
debugfs/util.c +8 -4
index be6b550e..9e880548 100644
@@ -530,12 +530,16 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
 		errno = 0;
 		y = x = strtoull(tok, &e, 0);
-		if (errno)
-			return errno;
+		if (errno) {
+			retval = errno;
+			break;
+		}
 		if (*e == '-') {
 			y = strtoull(e + 1, NULL, 0);
-			if (errno)
-				return errno;
+			if (errno) {
+				retval = errno;
+				break;
+			}
 		} else if (*e != 0) {
 			retval = EINVAL;
 			break;
#2 lore raw
In reply to Zhiqiang Liu
Quoted 17 lines · Zhiqiang Liu

In read_list func, if strtoull() fails in while loop, we will return the error code directly. Then, memory of variable lst will be leaked without setting to *list.

---
 debugfs/util.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
debugfs/util.c +0 -0
index be6b550e..9e880548 100644

debugfs/util.c errcode_t read_list(char *str, blk64_t **list, size_t *len) · line 536

Quoted 9 lines
debugfs/util.c +4 -2
@@ -530,12 +530,16 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
                errno = 0;
                y = x = strtoull(tok, &e, 0);
-               if (errno)
-                       return errno;
+               if (errno) {
+                       retval = errno;
+                       break;
+               }

Shouldn't we have goto err; here instead of break? strtoull failure here indicates that no valid value was found, so instead of returning the allocated memory, we should just free the memory and return error.

- Harshad

Quoted 15 lines
debugfs/util.c +4 -3
                if (*e == '-') {
                        y = strtoull(e + 1, NULL, 0);
-                       if (errno)
-                               return errno;
+                       if (errno) {
+                               retval = errno;
+                               break;
+                       }
                } else if (*e != 0) {
                        retval = EINVAL;
                        break;
--

2.19.1

#3 lore raw
Quoted 19 lines · harshad shirwadkar
Quoted 18 lines · Zhiqiang Liu

In read_list func, if strtoull() fails in while loop, we will return the error code directly. Then, memory of variable lst will be leaked without setting to *list.

---
 debugfs/util.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
debugfs/util.c +0 -0
index be6b550e..9e880548 100644
@@ -530,12 +530,16 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
Quoted 11 lines

debugfs/util.c errcode_t read_list(char *str, blk64_t **list, size_t *len) · line 536

Quoted 8 lines
debugfs/util.c +4 -2
                errno = 0;
                y = x = strtoull(tok, &e, 0);
-               if (errno)
-                       return errno;
+               if (errno) {
+                       retval = errno;
+                       break;
+               }

Shouldn't we have goto err; here instead of break? strtoull failure here indicates that no valid value was found, so instead of returning the allocated memory, we should just free the memory and return error.

As of commit 462c424500a5 ("debugfs: fix memory allocation failures when parsing journal_write arguments") there is no longer the err:
goto target. The goal is to move to a model where the caller is exclusively responsible for freeing any allocated memory, since if realloc() has gotten into the act, the memory pointed to in *list would have been freed by realloc(). The fix is to make sure *list is updated before we return. This also allows the caller to have access to the list of numbers parsed before we ran into an error.

So the Zhiqiang's patch is correc, and I will apply it.

- Ted

#4 lore raw

Thanks for the clarification, I think my repo was a bit stale.

- Harshad

Quoted 45 lines · Theodore Ts'o
Quoted 19 lines · harshad shirwadkar
Quoted 18 lines · Zhiqiang Liu

In read_list func, if strtoull() fails in while loop, we will return the error code directly. Then, memory of variable lst will be leaked without setting to *list.

---
 debugfs/util.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
debugfs/util.c +0 -0
index be6b550e..9e880548 100644
@@ -530,12 +530,16 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
Quoted 11 lines

debugfs/util.c errcode_t read_list(char *str, blk64_t **list, size_t *len) · line 536

Quoted 8 lines
debugfs/util.c +4 -2
                errno = 0;
                y = x = strtoull(tok, &e, 0);
-               if (errno)
-                       return errno;
+               if (errno) {
+                       retval = errno;
+                       break;
+               }

Shouldn't we have goto err; here instead of break? strtoull failure here indicates that no valid value was found, so instead of returning the allocated memory, we should just free the memory and return error.

As of commit 462c424500a5 ("debugfs: fix memory allocation failures when parsing journal_write arguments") there is no longer the err:
goto target. The goal is to move to a model where the caller is exclusively responsible for freeing any allocated memory, since if realloc() has gotten into the act, the memory pointed to in *list would have been freed by realloc(). The fix is to make sure *list is updated before we return. This also allows the caller to have access to the list of numbers parsed before we ran into an error.

So the Zhiqiang's patch is correc, and I will apply it.

- Ted

#5 lore raw
In reply to Theodore Ts'o

Thank you and harshad shirwadkar.

Quoted 47 lines · Theodore Ts'o
Quoted 19 lines · harshad shirwadkar
Quoted 18 lines · Zhiqiang Liu

In read_list func, if strtoull() fails in while loop, we will return the error code directly. Then, memory of variable lst will be leaked without setting to *list.

---
 debugfs/util.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
debugfs/util.c +0 -0
index be6b550e..9e880548 100644
@@ -530,12 +530,16 @@ errcode_t read_list(char *str, blk64_t **list, size_t *len)
 
Quoted 11 lines

debugfs/util.c errcode_t read_list(char *str, blk64_t **list, size_t *len) · line 536

Quoted 8 lines
debugfs/util.c +4 -2
                errno = 0;
                y = x = strtoull(tok, &e, 0);
-               if (errno)
-                       return errno;
+               if (errno) {
+                       retval = errno;
+                       break;
+               }

Shouldn't we have goto err; here instead of break? strtoull failure here indicates that no valid value was found, so instead of returning the allocated memory, we should just free the memory and return error.

As of commit 462c424500a5 ("debugfs: fix memory allocation failures when parsing journal_write arguments") there is no longer the err:
goto target. The goal is to move to a model where the caller is exclusively responsible for freeing any allocated memory, since if realloc() has gotten into the act, the memory pointed to in *list would have been freed by realloc(). The fix is to make sure *list is updated before we return. This also allows the caller to have access to the list of numbers parsed before we ran into an error.

So the Zhiqiang's patch is correc, and I will apply it.

- Ted

.