Skip to content

Fix warning for possible string truncation#2790

Merged
andypugh merged 3 commits intoLinuxCNC:2.9from
havardAasen:fix-string-truncation
Dec 14, 2023
Merged

Fix warning for possible string truncation#2790
andypugh merged 3 commits intoLinuxCNC:2.9from
havardAasen:fix-string-truncation

Conversation

@havardAasen
Copy link
Copy Markdown
Contributor

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
Copy link
Copy Markdown
Collaborator

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

@havardAasen
Copy link
Copy Markdown
Contributor Author

havardAasen commented Dec 13, 2023 via email

@andypugh
Copy link
Copy Markdown
Collaborator

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.

All of these fixes handles strncpy(), and the compiler warning have been
silenced by checking the length before copying the string, we check with
'>=' which means that the string must be one smaller and therefore have
room for at least one NULL terminating character, if this fails the
function provides a error message and/or return.

Some of the buffer lengths had a '+ 1' appended, some of these have been
removed. I'm also using sizeof() operator, in case we later want to
change the buffer size.

Most of the  previous solutions also checked for NULL terminated strings
but the compiler wasn't satisfied with it happening *after* the command
to strncpy().
This commit changes strncpy() for snprintf(), snprintf() guarantees a
NULL terminating string.

These places did not check the length at all, some hard-coded a NULL
terminating string as the last place in the array. With this, some of
the strings could potentially be without the NULL terminating character.

By changing to snprintf() we run into the possibility of truncating the
strings, no checks is currently in place to catch this.

There is also a strncat() thrown on here, hard-coded value was changed
from 1 to 2 to make room for the NULL terminating character.
Increased from 80 to 256, this buffer is used when creating the stack
trace used within LinuxCNC. It is used quite extensible throughout the
application.
@havardAasen havardAasen force-pushed the fix-string-truncation branch from d5b3cd1 to 7f54de8 Compare December 14, 2023 14:11
@havardAasen havardAasen changed the base branch from master to 2.9 December 14, 2023 14:12
@havardAasen
Copy link
Copy Markdown
Contributor Author

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

@andypugh
Copy link
Copy Markdown
Collaborator

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.

@andypugh andypugh merged commit 18f0295 into LinuxCNC:2.9 Dec 14, 2023
@havardAasen havardAasen deleted the fix-string-truncation branch December 15, 2023 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants