Correct string length handling in strlimcpy in halcmd.c#1858
Merged
jepler merged 1 commit intoLinuxCNC:masterfrom Jul 21, 2022
Merged
Correct string length handling in strlimcpy in halcmd.c#1858jepler merged 1 commit intoLinuxCNC:masterfrom
jepler merged 1 commit intoLinuxCNC:masterfrom
Conversation
The strlen value is the string length, often fetched from strlen(), which return the number of characters in the string, excluding the terminal NUL character. To have room to copy the string into *dest, the *destspace available must be strlen+1, not strlen, to also have room for the NUL at the end. Setting NUL explicitly will not be needed in the case srclen reflect the real length of str, but if srclen is smaller than the real length of str, will will ensure the string is always NUL terminated. Pass *destspace instead of strlen+1 to strncpy() ensure the actual target size limit is used no matter what the content of srclen is.
Contributor
|
You're right. I made a standalone test program for this just so I could be sure. #include <stdio.h>
#include <string.h>
static int strlimcpy(char **dest, const char *src, int srclen, int *destspace) {
if (*destspace < srclen) {
return -1;
} else {
strncpy(*dest, src, srclen);
(*dest)[srclen] = '\0';
srclen = strlen(*dest); /* use the actual number of bytes copied */
*destspace -= srclen;
*dest += srclen;
}
return 0;
}
static int strlimcpy2(char **dest, const char *src, int srclen, int *destspace) {
if (*destspace < srclen + 1) {
return -1;
} else {
strncpy(*dest, src, *destspace);
(*dest)[srclen] = '\0';
srclen = strlen(*dest); /* use the actual number of bytes copied */
*destspace -= srclen;
*dest += srclen;
}
return 0;
}
int main() {
const char src[] = "too big?";
char buf[8];
char *bufptr = buf;
int space = sizeof(buf);
int result = strlimcpy2(&bufptr, src, strlen(src), &space);
if(result < 0) { printf("failed\n"); }
else {
for(int i=0; i<sizeof(buf); i++) {
printf("buf[%d] = %d\n", i, (unsigned char)buf[i]);
}
}
printf("\n\n");
bufptr = buf;
space = sizeof(buf);
result = strlimcpy(&bufptr, src, strlen(src), &space);
if(result < 0) { printf("failed\n"); }
else {
for(int i=0; i<sizeof(buf); i++) {
printf("buf[%d] = %d\n", i, (unsigned char)buf[i]);
}
}
}With gcc -fsanitize=address this got a nice diagnostic on the call of the unmodified strlimcpy: The improved version of the function correctly returns -1 to indicate failure. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The strlen value is the string length, often fetched from strlen(),
which return the number of characters in the string, excluding the
terminal NUL character. To have room to copy the string into *dest,
the *destspace available must be strlen+1, not strlen, to also have
room for the NUL at the end. Setting NUL explicitly will not be
needed in the case srclen reflect the real length of str, but if srclen
is smaller than the real length of str, will will ensure the string
is always NUL terminated. Pass *destspace instead of strlen+1 to
strncpy() ensure the actual target size limit is used no matter what
the content of srclen is.