Skip to contentSkip to conversation

drm/amdgpu: Fix realloc of ptr

PATCH Superseded [email protected] 2 participants 3 messages Feb 26, 2022 – Feb 27, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/amdgpu: Fix realloc of ptr

From: Tom Rix <[email protected]>

Clang static analysis reports this error amdgpu_debugfs.c:1690:9: warning: 1st function call
argument is an uninitialized value
tmp = krealloc_array(tmp, i + 1,

        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

realloc will free tmp, so tmp can not be garbage.
And the return needs to be checked.

Fixes 5ce5a584cb82 ("drm/amdgpu: add debugfs for reset registers list")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +5 -1
index 9eb9b440bd438..159b97c0b4ebc 100644
@@ -1676,7 +1676,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
 	char reg_offset[11];
-	uint32_t *tmp;
+	uint32_t *tmp = NULL;
 	int ret, i = 0, len = 0;
 
 	do {
@@ -1688,6 +1688,10 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 		}
 
 		tmp = krealloc_array(tmp, i + 1, sizeof(uint32_t), GFP_KERNEL);
+		if (!tmp) {
+			ret = -ENOMEM;
+			goto error_free;
+		}
 		if (sscanf(reg_offset, "%X %n", &tmp[i], &ret) != 1) {
 			ret = -EINVAL;
 			goto error_free;
Signature · 1 line
2.26.3
#1 lore raw

From: [email protected]

Quoted 4 lines

Sent: 26 February 2022 15:59

From: Tom Rix <[email protected]>

Quoted 8 lines

Clang static analysis reports this error amdgpu_debugfs.c:1690:9: warning: 1st function call
argument is an uninitialized value
tmp = krealloc_array(tmp, i + 1,

        ^~~~~~~~~~~~~~~~~~~~~~~~~~~

realloc will free tmp, so tmp can not be garbage.
And the return needs to be checked.

Are you sure?
A quick check seems to show that krealloc() behaves the same way as libc realloc() and the pointer isn't freed on failure.

David

Quoted 31 lines
Fixes 5ce5a584cb82 ("drm/amdgpu: add debugfs for reset registers list")
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +5 -2
index 9eb9b440bd438..159b97c0b4ebc 100644
@@ -1676,7 +1676,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 {
 	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
 	char reg_offset[11];
-	uint32_t *tmp;
+	uint32_t *tmp = NULL;
 	int ret, i = 0, len = 0;
 
 	do {
@@ -1688,6 +1688,10 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
 		}
 
 		tmp = krealloc_array(tmp, i + 1, sizeof(uint32_t), GFP_KERNEL);
+		if (!tmp) {
+			ret = -ENOMEM;
+			goto error_free;
+		}
 		if (sscanf(reg_offset, "%X %n", &tmp[i], &ret) != 1) {
 			ret = -EINVAL;
 			goto error_free;
--
Quoted 1 line

2.26.3

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

author
#2 lore raw
Quoted 11 lines · David Laight

From: [email protected]

Quoted 10 lines

Sent: 26 February 2022 15:59

From: Tom Rix <[email protected]>

Clang static analysis reports this error amdgpu_debugfs.c:1690:9: warning: 1st function call
argument is an uninitialized value
tmp = krealloc_array(tmp, i + 1,

         ^~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 5 lines
Quoted 2 lines

realloc will free tmp, so tmp can not be garbage.
And the return needs to be checked.

Are you sure?
A quick check seems to show that krealloc() behaves the same way as libc realloc() and the pointer isn't freed on failure.

I suck, I'll respin the patch

Thanks

Tom

Quoted 39 lines

David

Quoted 31 lines
Fixes 5ce5a584cb82 ("drm/amdgpu: add debugfs for reset registers list")
---
  drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c +5 -2
index 9eb9b440bd438..159b97c0b4ebc 100644
@@ -1676,7 +1676,7 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
  {
  	struct amdgpu_device *adev = (struct amdgpu_device *)file_inode(f)->i_private;
  	char reg_offset[11];
-	uint32_t *tmp;
+	uint32_t *tmp = NULL;
  	int ret, i = 0, len = 0;
 
  	do {
@@ -1688,6 +1688,10 @@ static ssize_t amdgpu_reset_dump_register_list_write(struct file *f,
  		}
 
  		tmp = krealloc_array(tmp, i + 1, sizeof(uint32_t), GFP_KERNEL);
+		if (!tmp) {
+			ret = -ENOMEM;
+			goto error_free;
+		}
  		if (sscanf(reg_offset, "%X %n", &tmp[i], &ret) != 1) {
  			ret = -EINVAL;
  			goto error_free;
--
Quoted 1 line

2.26.3

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)