Skip to content

Fix a false positive on gcc stringop-truncations warning#7268

Closed
ashfordium wants to merge 1 commit intoDynamoRIO:masterfrom
ashfordium:flandini/stringop-truncations-2
Closed

Fix a false positive on gcc stringop-truncations warning#7268
ashfordium wants to merge 1 commit intoDynamoRIO:masterfrom
ashfordium:flandini/stringop-truncations-2

Conversation

@ashfordium
Copy link
Copy Markdown
Contributor

@ashfordium ashfordium commented Feb 10, 2025

Related to #7267. Trying to fix these all up so I can build and test locally.

This one is a false positive on gcc's stringop-truncations warnings saying that the size argument to strncpy is related to the source buffer and not the destination buffer.

Based on the checks before this strncpy, it is safe to use buflen instead as the size and to remove the explicit null terminator check set.

The code assigns: size_t len_var = strlen(tmp_buf);. At the strncpy, len_var + 1 <= buflen so then len_var <= buflen - 1 assuming len_var + 1 doesn't wrap, so strncpy copies at most buflen-1 characters, so strncpy(...,..., buflen) guarantees a null terminator at or before the end of buf.

Copy link
Copy Markdown
Contributor

@edeiana edeiana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make a single PR (see #7267 (review))

}
strncpy(buf, tmp_buf, len_var + 1);
buf[buflen - 1] = '\0';
strncpy(buf, tmp_buf, buflen);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always prefer to null-terminate after strncpy(), it improves readability.

@ashfordium ashfordium closed this Feb 12, 2025
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