Skip to contentSkip to conversation

Build fail in drivers/gpu/drm/i915/display/intel_tc.c

DISCUSSION David Laight 3 participants 6 messages Nov 9, 2023 – Nov 15, 2023

lore
Share & tools
lore
#0 lore raw

I'm seeing a build fail at line 1878 of intel_tc.c

gcc is being a bit over-enthusiastic about snprintf() formats and reports that "%c/TC#%d", xxx, tc_port + 1 might not fit in the 8 bytes available.
'tc_port' is an enum with values -1 to 5.
I guess it is either allowing for the full 'int' value of the enum.
Changing to '(tc_port & 7) + 1' stops the compiler bleating.

The code isn't new, so I guess the compiler flags have changed?

It might depend on the gcc version as well.
I'm using gcc 7.5.0 from Ubuntu 18.04.

David

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

#1 lore raw
Quoted 10 lines · David Laight

I'm seeing a build fail at line 1878 of intel_tc.c

gcc is being a bit over-enthusiastic about snprintf() formats and reports that "%c/TC#%d", xxx, tc_port + 1 might not fit in the 8 bytes available.
'tc_port' is an enum with values -1 to 5.
I guess it is either allowing for the full 'int' value of the enum.
Changing to '(tc_port & 7) + 1' stops the compiler bleating.

Quoted 4 lines

The code isn't new, so I guess the compiler flags have changed?

It might depend on the gcc version as well.
I'm using gcc 7.5.0 from Ubuntu 18.04.

The compiler warn should be fixed/suppressed by:
https://lore.kernel.org/all/[email protected]

--Imre

Quoted 7 lines

David

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

#2 lore raw
Quoted 3 lines · Imre Deak

The compiler warn should be fixed/suppressed by:
https://lore.kernel.org/all/[email protected]

Ugh, so now it's a dynamic allocation, wasting memory, and a pointer to it, using as much memory as the array did in the first place.

All because of a pointless warning that was a false positive - and was always harmless anyway, since snprintf() is safe (ie it was only a "might be truncated").

Please don't do this. Either do that ((tc_port & 7) + 1) suggestion of David's, or just do '%c' and make the expression be

'1' + tc_port

which should be fine since I915_MAX_PORTS is 8 or whatever.

I do wonder why those ports are printed out as '1-8', when the 'enum port' is PORT_A..I.

So it would actually have made more sense to print them out as %c with the expression being

'A'+tc_port

but I guess by now people might depend on the 1..8 naming?

Linus

#3 lore raw

From: Linus Torvalds

Quoted 7 lines

Sent: 10 November 2023 00:52

Quoted 3 lines · Imre Deak

The compiler warn should be fixed/suppressed by:
https://lore.kernel.org/all/[email protected]

Quoted 6 lines

Ugh, so now it's a dynamic allocation, wasting memory, and a pointer to it, using as much memory as the array did in the first place.

All because of a pointless warning that was a false positive - and was always harmless anyway, since snprintf() is safe (ie it was only a "might be truncated").

That entire warning for snprintf() is a false positive. The ones that are likely to overflow unexpectedly are the ones with a "%s" format for a 'char *' pointer where there is no implied length.

The same check for printf() using the implied buffer length probably does make sense.

I don't even think there is a way of avoiding it on a case by case basis - apart from passing both the buffer address and length to an inline asm that the compiler has to assume might change the values, but that tends to generate an extra 'mov' instruction for no good reason at all.

Quoted 7 lines

Please don't do this. Either do that ((tc_port & 7) + 1) suggestion of David's, or just do '%c' and make the expression be

'1' + tc_port

which should be fine since I915_MAX_PORTS is 8 or whatever.

If I'd though for 2 seconds that is what I'd have done. But I wanted to get something through the compiler.

Quoted 2 lines

I do wonder why those ports are printed out as '1-8', when the 'enum port' is PORT_A..I.

They look like TC_PORT_[1..6] to me - the enum values are 0..5 which is why there is a 'random' '+ 1'.

David

Quoted 9 lines

So it would actually have made more sense to print them out as %c with the expression being

   'A'+tc_port

but I guess by now people might depend on the 1..8 naming?

Linus

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

#4 lore raw
Quoted 20 lines · David Laight

From: Linus Torvalds

Quoted 7 lines

Sent: 10 November 2023 00:52

Quoted 3 lines · Imre Deak

The compiler warn should be fixed/suppressed by:
https://lore.kernel.org/all/[email protected]

Quoted 6 lines

Ugh, so now it's a dynamic allocation, wasting memory, and a pointer to it, using as much memory as the array did in the first place.

All because of a pointless warning that was a false positive - and was always harmless anyway, since snprintf() is safe (ie it was only a "might be truncated").

That entire warning for snprintf() is a false positive. The ones that are likely to overflow unexpectedly are the ones with a "%s" format for a 'char *' pointer where there is no implied length.

Quoted 16 lines

The same check for printf() using the implied buffer length probably does make sense.

I don't even think there is a way of avoiding it on a case by case basis - apart from passing both the buffer address and length to an inline asm that the compiler has to assume might change the values, but that tends to generate an extra 'mov' instruction for no good reason at all.

Quoted 7 lines

Please don't do this. Either do that ((tc_port & 7) + 1) suggestion of David's, or just do '%c' and make the expression be

'1' + tc_port

which should be fine since I915_MAX_PORTS is 8 or whatever.

Ok, the patch above was merged already to drm-tip, but I agree not to use kasprintf for this. The above looks ok and there is already tc_port_name() for this, would just need to export that from intel_display.h.

I can follow up with a patch for this, or if you or David wanted to do that please send a patch to the [email protected] list.

Thanks,
Imre

Quoted 24 lines

If I'd though for 2 seconds that is what I'd have done. But I wanted to get something through the compiler.

Quoted 2 lines

I do wonder why those ports are printed out as '1-8', when the 'enum port' is PORT_A..I.

They look like TC_PORT_[1..6] to me - the enum values are 0..5 which is why there is a 'random' '+ 1'.

David

Quoted 9 lines

So it would actually have made more sense to print them out as %c with the expression being

   'A'+tc_port

but I guess by now people might depend on the 1..8 naming?

Linus

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

#5 lore raw

From: Imre Deak

Quoted 33 lines

Sent: 14 November 2023 19:08

Quoted 30 lines · David Laight

From: Linus Torvalds

Quoted 7 lines

Sent: 10 November 2023 00:52

Quoted 3 lines · Imre Deak

The compiler warn should be fixed/suppressed by:
https://lore.kernel.org/all/[email protected]

Quoted 6 lines

Ugh, so now it's a dynamic allocation, wasting memory, and a pointer to it, using as much memory as the array did in the first place.

All because of a pointless warning that was a false positive - and was always harmless anyway, since snprintf() is safe (ie it was only a "might be truncated").

That entire warning for snprintf() is a false positive. The ones that are likely to overflow unexpectedly are the ones with a "%s" format for a 'char *' pointer where there is no implied length.

The same check for printf() using the implied buffer length probably does make sense.

I don't even think there is a way of avoiding it on a case by case basis - apart from passing both the buffer address and length to an inline asm that the compiler has to assume might change the values, but that tends to generate an extra 'mov' instruction for no good reason at all.

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 6 lines

Please don't do this. Either do that ((tc_port & 7) + 1) suggestion of David's, or just do '%c' and make the expression be

'1' + tc_port

which should be fine since I915_MAX_PORTS is 8 or whatever.

Ok, the patch above was merged already to drm-tip,

I'd noticed ....

Quoted 4 lines

but I agree not to use kasprintf for this.
The above looks ok and there is already tc_port_name() for this, would just need to export that from intel_display.h.

I'm not sure that #define does anything except obscure what is going on.
Anyone reading the code is going to search for it and then sigh. But it is your code :-)

Quoted 2 lines

I can follow up with a patch for this, or if you or David wanted to do that please send a patch to the [email protected] list.

Probably much easier for you to do it.
My process to get patches emailed is a PITA.

David

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