We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 5163 - clang static analyser error if strlcpy and strlcat are not available
Summary: clang static analyser error if strlcpy and strlcat are not available
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: build (show other bugs)
Version: HG 2.0
Hardware: All Linux
: P2 minor
Assignee: Ryan C. Gordon
QA Contact: Sam Lantinga
URL:
Keywords: target-2.0.14
Depends on:
Blocks:
 
Reported: 2020-05-28 11:51 UTC by Martin Gerhardy
Modified: 2020-06-28 21:52 UTC (History)
2 users (show)

See Also:


Attachments
check for strl* (1.18 KB, patch)
2020-05-30 13:19 UTC, Mathieu Eyraud
Details | Diff
check for strl* v2 (1.04 KB, patch)
2020-06-27 14:03 UTC, Mathieu Eyraud
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Gerhardy 2020-05-28 11:51:33 UTC
If I run clang-tidy-10 I get some errors that are related to 

   #if defined(__clang_analyzer__) && !defined(SDL_DISABLE_ANALYZE_MACROS)
   #define SDL_strlcpy strlcpy
   #define SDL_strlcat strlcat

I wonder whether it would be a good idea to check for HAVE_STRLCPY before overriding them?

The error for strlcpy is:

------------------------------------------------
error: use of undeclared identifier 'strlcpy'; did you mean 'strncpy'? [clang-diagnostic-error]
                                SDL_strlcpy(&buf[idx], value.c_str(), remaining);
                                ^~~~~~~~~~~
                                strncpy
/usr/include/SDL2/SDL_stdinc.h:578:21: note: expanded from macro 'SDL_strlcpy'
#define SDL_strlcpy strlcpy
                    ^
/usr/include/string.h:125:14: note: 'strncpy' declared here
extern char *strncpy (char *__restrict __dest,
             ^
46 warnings and 1 error generated.
------------------------------------------------
Comment 1 Mathieu Eyraud 2020-05-29 10:53:18 UTC
Clang-tidy (and clang) finds more warning if you use standard functions. You should install libbsd-dev and add '#include <bsd/string.h>' before '#define SDL_strlcpy strlcpy' to get the missing functions.
Comment 2 Ryan C. Gordon 2020-05-29 18:02:36 UTC
We already make sure SDL_malloc becomes standard malloc() when doing static analysis, it would be smart to handle these, too.
Comment 3 Mathieu Eyraud 2020-05-30 13:19:49 UTC
Created attachment 4360 [details]
check for strl*

Here a patch that checks for HAVE_STRLCPY and HAVE_STRLCAT, then fallback to include bsd/string.h.
It uses '__has_include", which is a clang extension. But that should be fine as this code is only used for clang static analysis.
It also defines SDL_wcs* functions to standard functions.
Comment 4 Ryan C. Gordon 2020-06-26 23:39:04 UTC
(In reply to meyraud705 from comment #3)
> Here a patch that checks for HAVE_STRLCPY and HAVE_STRLCAT, then fallback to
> include bsd/string.h.

But I assume Clang static analysis can't perform any special strlcpy() checks if you have to install libbsd-dev to get this function, right...?

--ryan.
Comment 5 Mathieu Eyraud 2020-06-27 14:03:21 UTC
Created attachment 4399 [details]
check for strl* v2

I use clang-tidy and it knows standard functions enough to perform checks as long as the function is declared.

Attached version 2 of my patch: libbsd is actually not needed, only declares strlcat and strlcpy if not available.
Comment 6 Ryan C. Gordon 2020-06-28 21:52:57 UTC
I made a slight change to the #ifdefs in this patch--we want SDL_strlcpy defined to strlcpy no matter what, and just declare the C runtime function if the system didn't provide it--but otherwise, this looks good, and I've put it in revision control as https://hg.libsdl.org/SDL/rev/92ea0212b095

Thanks!

--ryan.