Irrelevant warning "The result from calling 'memcpy' is not null-terminated"
| Bugzilla Link | 47649 |
| Version | unspecified |
| OS | Windows NT |
| CC | @Charusso,@EugeneZelenko |
Extended Description
/* Return the concatenation of s1 and s2, freshly allocated. */ char * str_concat2(const char *s1, const char *s2) { size_t len1 = strlen(s1); size_t len2 = strlen(s2); char *result = xmalloc(len1 + len2 + 1); memcpy(result, s1, len1); memcpy(result + len1, s2, len2 + 1); return result; }
At the first memcpy, Clang-Tidy warns that the result be not null-terminated.
So what? In the immediately following line the next memcpy appends to this memory region and null-terminates it. There is no way this memory region could escape between the two memcpy calls.
Therefore there should be no warning.
Source: https://github.com/NetBSD/src/blob/b565d0ac79e002c81454fc886fa46976e6b94d16/usr.bin/make/str.c#L76
Hey! Clang-Tidy is path insensitive, so that it cannot reason about sequential function calls very well. The Clang Static Analyzer is the tool for such path sensitive analysis, however, it has different weaknesses. I have tried to implement this check with both the tools, but I did not finish the Static Analyzer checkers which wanted to replace the Clang-Tidy check. Since then, I have left the project, so it remains a little-bit noisy, but useful check:
memcpy(result, s1, strlen(s1));
In a non-path-sensitive manner, this call is bugprone, and the tool is right. Sorry if that is in your case more trouble, than useful. If that is the case, please switch off the following check: bugprone-not-null-terminated-result.
Closing due to lack of activity. Warning can be suppressed via NOLINT or just by disabling the check entirely.