Skip to contentSkip to conversation

decompress_bunzip2: fix sizeof type in start_bunzip

PATCH In review [email protected] 3 participants 7 messages Jul 12, 2020 – Jul 13, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 decompress_bunzip2: fix sizeof type in start_bunzip

From: Tom Rix <[email protected]>

clang static analysis flags this error

lib/decompress_bunzip2.c:671:13: warning: Result of 'malloc' is converted
to a pointer of type 'unsigned int', which is incompatible with sizeof operand type 'int' [unix.MallocSizeof]

        bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reviewing the bunzip_data structure, the element dbuf is type

	/* Intermediate buffer and its size (in bytes) */
	unsigned int *dbuf, dbufSize;

So change the type in sizeof to 'unsigned int'

Fixes bc22c17e12c1 ("bzip2/lzma: library support for gzip, bzip2 and lzma decompression")
---
 lib/decompress_bunzip2.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/decompress_bunzip2.c +1 -1
index 7c4932eed748..59ab76bda7a7 100644
@@ -668,7 +668,7 @@ static int INIT start_bunzip(struct bunzip_data **bdp, void *inbuf, long len,
 	   uncompressed data.  Allocate intermediate buffer for block. */
 	bd->dbufSize = 100000*(i-BZh0);
 
-	bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
+	bd->dbuf = large_malloc(bd->dbufSize * sizeof(unsigned int));
 	if (!bd->dbuf)
 		return RETVAL_OUT_OF_MEMORY;
 	return RETVAL_OK;
Signature · 1 line
2.18.1
#1 lore raw
Quoted 10 lines · On 2020-07-12 05:59, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this error

lib/decompress_bunzip2.c:671:13: warning: Result of 'malloc' is converted
to a pointer of type 'unsigned int', which is incompatible with sizeof operand type 'int' [unix.MallocSizeof]

        bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

Reviewing the bunzip_data structure, the element dbuf is type

	/* Intermediate buffer and its size (in bytes) */
	unsigned int *dbuf, dbufSize;

So change the type in sizeof to 'unsigned int'

You must be kidding.

If you want to change it, change it to sizeof(bd->dbuf) instead, but this flag is at least in my opinion a total joke. For sizeof(int) != sizeof(unsigned int) is beyond bizarre, no matter how stupid the platform.

-hpa

author
#2 lore raw
Quoted 16 lines · H. Peter Anvin
Quoted 15 lines · On 2020-07-12 05:59, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this error

lib/decompress_bunzip2.c:671:13: warning: Result of 'malloc' is converted
to a pointer of type 'unsigned int', which is incompatible with sizeof operand type 'int' [unix.MallocSizeof]

        bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reviewing the bunzip_data structure, the element dbuf is type

	/* Intermediate buffer and its size (in bytes) */
	unsigned int *dbuf, dbufSize;
Quoted 7 lines
Quoted 2 lines

So change the type in sizeof to 'unsigned int'

You must be kidding.

If you want to change it, change it to sizeof(bd->dbuf) instead, but this flag is at least in my opinion a total joke. For sizeof(int) != sizeof(unsigned int) is beyond bizarre, no matter how stupid the platform.

Using the actual type is more correct that using a type of the same size.

trix

Quoted 2 lines

-hpa

#3 lore raw
Quoted 3 lines · Tom Rix
Quoted 2 lines · H. Peter Anvin
Quoted 1 line · On 2020-07-12 05:59, [email protected] wrote:

From: Tom Rix <[email protected]>

[]

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

So change the type in sizeof to 'unsigned int'

You must be kidding.

If you want to change it, change it to sizeof(bd->dbuf) instead, but this flag is at least in my opinion a total joke. For sizeof(int) != sizeof(unsigned int) is beyond bizarre, no matter how stupid the platform.

Using the actual type is more correct that using a type of the same size.

Sure.

But this hardly matters as this same type conversion from signed to unsigned or the other way round is _everywhere_ in the kernel.

And especially the cc of stable is unnecessary.

#4 lore raw
In reply to Tom Rix
Quoted 26 lines · Tom Rix
Quoted 6 lines · H. Peter Anvin
Quoted 5 lines · On 2020-07-12 05:59, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this error

lib/decompress_bunzip2.c:671:13: warning: Result of 'malloc' is

converted

Quoted 1 line
Quoted 1 line

to a pointer of type 'unsigned int', which is incompatible with

sizeof

Quoted 6 lines
Quoted 6 lines
  operand type 'int' [unix.MallocSizeof]
        bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reviewing the bunzip_data structure, the element dbuf is type

Quoted 8 lines
Quoted 5 lines
	/* Intermediate buffer and its size (in bytes) */
	unsigned int *dbuf, dbufSize;

So change the type in sizeof to 'unsigned int'

You must be kidding.

If you want to change it, change it to sizeof(bd->dbuf) instead, but

this flag

Quoted 11 lines
Quoted 1 line

is at least in my opinion a total joke. For sizeof(int) !=

sizeof(unsigned

Quoted 1 line

int) is beyond bizarre, no matter how stupid the platform.

Using the actual type is more correct that using a type of the same size.

trix

Quoted 2 lines

-hpa

"More correct?" All it is is more verbose.

Using the sizeof of the actual object at least adds some actual safety.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.
author
#5 lore raw
Quoted 32 lines · On 7/12/20 3:21 PM, [email protected] wrote:
Quoted 31 lines · Tom Rix
Quoted 6 lines · H. Peter Anvin
Quoted 5 lines · On 2020-07-12 05:59, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this error

lib/decompress_bunzip2.c:671:13: warning: Result of 'malloc' is

converted

Quoted 1 line
Quoted 1 line

to a pointer of type 'unsigned int', which is incompatible with

sizeof

Quoted 6 lines
Quoted 6 lines
  operand type 'int' [unix.MallocSizeof]
        bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reviewing the bunzip_data structure, the element dbuf is type

Quoted 8 lines
Quoted 5 lines
	/* Intermediate buffer and its size (in bytes) */
	unsigned int *dbuf, dbufSize;

So change the type in sizeof to 'unsigned int'

You must be kidding.

If you want to change it, change it to sizeof(bd->dbuf) instead, but

this flag

Quoted 1 line

is at least in my opinion a total joke. For sizeof(int) !=

sizeof(unsigned

Quoted 1 line

int) is beyond bizarre, no matter how stupid the platform.

Using the actual type is more correct that using a type of the same size.

Quoted 7 lines
Quoted 4 lines

trix

Quoted 2 lines

-hpa

"More correct?" All it is is more verbose.

Using the sizeof of the actual object at least adds some actual safety.

Sorry, I am being pedantic, I mean anything that produces the correct assembly is correct. But there are different path to being correct.  The path I was suggesting to follow the type of the element/final pointer when allocating an memory.

large_malloc(bd->dbufSize * sizeof(*bd->dbuf)) would also work

I will respin.

trix

#6 lore raw
Quoted 49 lines · Tom Rix
Quoted 17 lines · On 7/12/20 3:21 PM, [email protected] wrote:
Quoted 16 lines · Tom Rix
Quoted 6 lines · H. Peter Anvin
Quoted 5 lines · On 2020-07-12 05:59, [email protected] wrote:

From: Tom Rix <[email protected]>

clang static analysis flags this error

lib/decompress_bunzip2.c:671:13: warning: Result of 'malloc' is

converted

Quoted 1 line
Quoted 1 line

to a pointer of type 'unsigned int', which is incompatible with

sizeof

Quoted 6 lines
Quoted 6 lines
  operand type 'int' [unix.MallocSizeof]
        bd->dbuf = large_malloc(bd->dbufSize * sizeof(int));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Reviewing the bunzip_data structure, the element dbuf is type

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
	/* Intermediate buffer and its size (in bytes) */
	unsigned int *dbuf, dbufSize;

So change the type in sizeof to 'unsigned int'

You must be kidding.

If you want to change it, change it to sizeof(bd->dbuf) instead,

but

Quoted 7 lines
Quoted 7 lines

this flag

Quoted 1 line

is at least in my opinion a total joke. For sizeof(int) !=

sizeof(unsigned

Quoted 1 line

int) is beyond bizarre, no matter how stupid the platform.

Using the actual type is more correct that using a type of the same size.

Quoted 7 lines
Quoted 4 lines

trix

Quoted 2 lines

-hpa

"More correct?" All it is is more verbose.

Using the sizeof of the actual object at least adds some actual

safety.

Sorry, I am being pedantic, I mean anything that produces the correct assembly is correct. But there are different path to being correct. The path I was suggesting to follow the type of the element/final pointer when allocating an memory.

Quoted 5 lines

large_malloc(bd->dbufSize * sizeof(*bd->dbuf)) would also work

I will respin.

trix

This isn't Linux style, but in the NASM source I have been migrating to macros:

nasm_new(ptr);
nasm_newn(ptr,n);

... using sizeof() in exactly this manner.

Signature · 1 line
Sent from my Android device with K-9 Mail. Please excuse my brevity.