From c8e63812c5f2402bab4f74f68d46843d7d94123c Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Sun, 16 Jun 2019 11:03:08 +0200 Subject: errors: introduce `git_error_vset` function Right now, we only provide a `git_error_set` that has a variadic function signature. It's impossible to drive this function in a C89-compliant way from other functions that have a variadic signature, though, like for example `git_parse_error`. Implement a new `git_error_vset` function that gets a `va_list` as parameter, fixing the above problem. --- src/errors.c | 19 ++++++++++++------- src/errors.h | 4 ++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/errors.c b/src/errors.c index 8ef491916..18d6c2dc8 100644 --- a/src/errors.c +++ b/src/errors.c @@ -49,9 +49,17 @@ void git_error_set_oom(void) GIT_GLOBAL->last_error = &g_git_oom_error; } -void git_error_set(int error_class, const char *string, ...) +void git_error_set(int error_class, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + git_error_vset(error_class, fmt, ap); + va_end(ap); +} + +void git_error_vset(int error_class, const char *fmt, va_list ap) { - va_list arglist; #ifdef GIT_WIN32 DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0; #endif @@ -59,11 +67,8 @@ void git_error_set(int error_class, const char *string, ...) git_buf *buf = &GIT_GLOBAL->error_buf; git_buf_clear(buf); - if (string) { - va_start(arglist, string); - git_buf_vprintf(buf, string, arglist); - va_end(arglist); - + if (fmt) { + git_buf_vprintf(buf, fmt, ap); if (error_class == GIT_ERROR_OS) git_buf_PUTS(buf, ": "); } diff --git a/src/errors.h b/src/errors.h index f2af1e37d..86f06f9c7 100644 --- a/src/errors.h +++ b/src/errors.h @@ -14,8 +14,8 @@ /* * Set the error message for this thread, formatting as needed. */ - -void git_error_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3); +void git_error_set(int error_class, const char *fmt, ...) GIT_FORMAT_PRINTF(2, 3); +void git_error_vset(int error_class, const char *fmt, va_list ap); /** * Set the error message for a regex failure, using the internal regex -- cgit v1.2.1 From 27b8b31e2955ec264a9b748c8e5328e2c8e04419 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 1 Aug 2019 11:57:03 +0200 Subject: parse: remove use of variadic macros which are not C89 compliant The macro `git_parse_error` is implemented in a variadic way so that it's possible to pass printf-style parameters. Unfortunately, variadic macros are not defined by C89 and thus we cannot use that functionality. But as we have implemented `git_error_vset` in the previous commit, we can now just use that instead. Convert `git_parse_error` to a variadic function and use `git_error_vset` to fix the compliance violation. While at it, move the function to "patch_parse.c". --- src/parse.h | 3 --- src/patch_parse.c | 12 ++++++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/parse.h b/src/parse.h index 42a2aff1a..188ac281c 100644 --- a/src/parse.h +++ b/src/parse.h @@ -28,9 +28,6 @@ typedef struct { int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len); void git_parse_ctx_clear(git_parse_ctx *ctx); -#define git_parse_err(...) \ - ( git_error_set(GIT_ERROR_PATCH, __VA_ARGS__), -1 ) - #define git_parse_ctx_contains_s(ctx, str) \ git_parse_ctx_contains(ctx, str, sizeof(str) - 1) diff --git a/src/patch_parse.c b/src/patch_parse.c index 29dc8b818..84953ee14 100644 --- a/src/patch_parse.c +++ b/src/patch_parse.c @@ -33,6 +33,18 @@ typedef struct { char *old_prefix, *new_prefix; } git_patch_parsed; +static int git_parse_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2); +static int git_parse_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + git_error_vset(GIT_ERROR_PATCH, fmt, ap); + va_end(ap); + + return -1; +} + static size_t header_path_len(git_patch_parse_ctx *ctx) { bool inquote = 0; -- cgit v1.2.1 From 63d8cd189e7eb9a410f04283cbe46d3d3f0a1924 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Sun, 16 Jun 2019 11:17:17 +0200 Subject: apply: remove use of variadic error macro The macro `apply_err` is implemented as a variadic macro, which are not defined by C89. Convert it to a variadic function, instead. --- src/apply.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/apply.c b/src/apply.c index 55b1a397f..1ee9291d3 100644 --- a/src/apply.c +++ b/src/apply.c @@ -24,9 +24,6 @@ #include "reader.h" #include "index.h" -#define apply_err(...) \ - ( git_error_set(GIT_ERROR_PATCH, __VA_ARGS__), GIT_EAPPLYFAIL ) - typedef struct { /* The lines that we allocate ourself are allocated out of the pool. * (Lines may have been allocated out of the diff.) @@ -35,6 +32,18 @@ typedef struct { git_vector lines; } patch_image; +static int apply_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2); +static int apply_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + git_error_vset(GIT_ERROR_PATCH, fmt, ap); + va_end(ap); + + return GIT_EAPPLYFAIL; +} + static void patch_line_init( git_diff_line *out, const char *in, -- cgit v1.2.1 From 1721ab047da75562c6f0e5c72af6c3e0d6b7e7e5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Sun, 16 Jun 2019 11:25:47 +0200 Subject: unix: posix: avoid use of variadic macro `p_snprintf` The macro `p_snprintf` is implemented as a variadic macro that calls `snprintf` directly with `__VA_ARGS__`. In C89, variadic macros are not allowed, but as the arguments of `p_snprintf` and `snprintf` are matching 1:1, we can fix this by simply removing the parameter list from `p_snprintf`. --- src/unix/posix.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unix/posix.h b/src/unix/posix.h index f969f8362..d1f902489 100644 --- a/src/unix/posix.h +++ b/src/unix/posix.h @@ -59,7 +59,7 @@ GIT_INLINE(int) p_fsync(int fd) #define p_strcasecmp(s1, s2) strcasecmp(s1, s2) #define p_strncasecmp(s1, s2, c) strncasecmp(s1, s2, c) #define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a) -#define p_snprintf(b, c, ...) snprintf(b, c, __VA_ARGS__) +#define p_snprintf snprintf #define p_mkstemp(p) mkstemp(p) #define p_chdir(p) chdir(p) #define p_chmod(p,m) chmod(p, m) -- cgit v1.2.1