@matheusbsbs / matheus-bagliano / commits / 15b52a44e0
Fork 0

compat-util: type-check parameters of no-op replacement functions

When there is no need to run a specific function on certain platforms, we often #define an empty function to swallow its parameters and make it into a no-op, e.g. #define precompose_argv(c,v) /* no-op */ While this guarantees that no unneeded code is generated, it also discards type and other checks on these parameters, e.g. a new code written with the argv-array API (diff_args is of type "struct argv_array" that has .argc and .argv members): precompose_argv(diff_args.argc, diff_args.argv); must be updated to use "struct strvec diff_args" with .nr and .v members, like so: precompose_argv(diff_args.nr, diff_args.v); after the argv-array API has been updated to the strvec API. However, the "no oop" C preprocessor macro is too aggressive to discard what is unused, and did not catch such a call that was left unconverted. Using a "static inline" function whose body is a no-op should still result in the same binary with decent compilers yet catch such a reference to a missing field or passing a value of a wrong type. While at it, I notice that precompute_str() has never been used anywhere in the code, since it was introduced at 76759c7d (git on Mac OS and precomposed unicode, 2012-07-08). Instead of turning it into a static inline, just remove it. Signed-off-by: Junio C Hamano <gitster@pobox.com>

Junio C Hamano committed Aug 6, 2020 at 17:25 UTC 15b52a44e0c92a0658e891194a5b0610d1f53afc
1 file changed +15 -5
git-compat-util.h
+15 -5
@@ -252,8 +252,10 @@ typedef unsigned long uintptr_t;
252 #ifdef PRECOMPOSE_UNICODE
253 #include "compat/precompose_utf8.h"
254 #else
255 -#define precompose_str(in,i_nfd2nfc)
256 -#define precompose_argv(c,v)
255 +static inline void precompose_argv(int argc, const char **argv)
256 +{
257 + ; /* nothing */
258 +}
259 #define probe_utf8_pathname_composition()
260 #endif
261
@@ -270,7 +272,9 @@ struct itimerval {
272 #endif
273
274 #ifdef NO_SETITIMER
273 -#define setitimer(which,value,ovalue)
275 +static inline int setitimer(int which, const struct itimerval *value, struct itimerval *newvalue) {
276 + ; /* nothing */
277 +}
278 #endif
279
280 #ifndef NO_LIBGEN_H
@@ -1231,8 +1235,14 @@ int warn_on_fopen_errors(const char *path);
1235 #endif
1236
1237 #ifndef _POSIX_THREAD_SAFE_FUNCTIONS
1234 -#define flockfile(fh)
1235 -#define funlockfile(fh)
1238 +static inline void flockfile(FILE *fh)
1239 +{
1240 + ; /* nothing */
1241 +}
1242 +static inline void funlockfile(FILE *fh)
1243 +{
1244 + ; /* nothing */
1245 +}
1246 #define getc_unlocked(fh) getc(fh)
1247 #endif
1248