LinuxCNC / linuxcnc

LinuxCNC controls CNC machines. It can drive milling machines, lathes, 3d printers, laser cutters, plasma cutters, robot arms, hexapods, and more.
http://linuxcnc.org/
GNU General Public License v2.0
1.81k stars 1.16k forks source link

Fix warning for possible string truncation #2790

Closed havardAasen closed 11 months ago

havardAasen commented 11 months ago

We check the length of the string before using strncpy(), we check with >= which means that the string must be one smaller and therefore have room for at least one NULL terminating character.

The previous solution also checked for NULL terminated strings but the compiler wasn't satisfied with it happening after the command to strncpy().

For the error message, the unnecessary cast to unsigned long is removed, and the format string has been changed to zu.

andypugh commented 11 months ago

How many does this catch? I have notice that the compile log is sprinkled with hundreds of string truncation warnings.

havardAasen commented 11 months ago

On 13.12.2023 17:13, andypugh wrote:

How many does this catch? I have notice that the compile log is sprinkled with hundreds of string truncation warnings.

Only three, it was all from that file. If you are satisfied with how i solved it, I can see if I can't fix some more of them, but not hundreds.

I forgot to check if they are present in 2.9, should i target any specific branch?

andypugh commented 11 months ago

There are many truncation warnings when compiling 2.9. So I would suggest targetting that. I probably won't merge them until after 2.9.2 though.

havardAasen commented 11 months ago

This fixes quite a lot of the warning for string truncation. The one responsible for most of them was fixed with 7f54de8.

andypugh commented 11 months ago

I changed my mind, there was quite a backlog of 2.9 fixes which I am working through. This might as well go in too.