问题
I would like to rid a C program of all uses of functions which have locale-dependent behavior, replacing them with similar functions which behave like their library counterparts in the "C" locale, regardless of the locale.
This is necessary for making the program into a library which will always have the same behavior for all inputs, even if linked into a program which invokes setlocale
.
How can we get the GCC toolchain, in a glibc environment, to produce a diagnostic for every call to a locale-dependent C function?
This would be not only used to do the initial conversion, but also going forward to instantly catch situations when usage of such functions creeps into the program.
回答1:
Per C 2018 5.2.1, the extended characters in the source and execution character sets are locale-specific. Therefore, every operation and function that works with characters may be affected by the locale. All functions in <wchar.h>
and <wctype.h>
are locale-dependent.
For operations and functions that only work on the numerical values and are agnostic about the interpretations of those values, you might consider them unaffected regardless of what characters are in the character set or what values they have. For example, the result of 'ü' - 'ö'
is locale-dependent, and so is strcmp
on strings containing those characters, but this is a function of the values of 'ü'
and 'ö'
, not of the behavior of -
or strcmp
.
Per 5.2.2 1, the direction of writing is locale-specific, so all character output functions are theoretically affected.
Additional functions for which locale-dependent behavior is mentioned in the C 2018 standard are:
mbtowc
,mbrtoc16
,mbrtoc32
(6.4.4.4 11).- All functions in
<ctype.h>
(7.4 2). - All functions in
<locale.h>
(7.11). strtod
,strtof
,strtold
(7.22.1.3 3).strtol
,strtoll
,strtoul
,strtoull
(7.22.1.4 6).mblen
,mbtowc
,wctomb
(7.22.7 1).mbstowcs
,wcstombs
(7.22.8 1).strcoll
(7.24.4.3 2).strerror
(7.24.6.2 4).strftime
(7.27.3.5 3).strtoimax
,strotoumax
(7.8.2.3 defines them by reference to the otherstrto
functions).nan
,nanf
, andnanl
(7.12.11.2 defines them by reference tostrto
functions).fscanf
,scanf
,sscanf
,vfsscanf
,vscanf
,vsscanf
,atof
,atoi
,atof
,atoll
(defined by reference to functions above).
Implementation-dependent and other incompletely specified operations could be affected by locale. For example, %p
for fprintf
is specified to format a pointer in an implementation-defined manner, so an implementation might define it to depend on locale. Candidates for such behavior are listed in Annex J, “Portability issues.”
来源:https://stackoverflow.com/questions/29337287/identify-use-of-c-library-functions-that-have-locale-dependent-behavior