Fix: Error out when passing an argument to long option that takes no argument
[argpar.git] / argpar / argpar.c
index 8cc20cd77490f37a24f169b8d07d7feb2f564ade..b110f18a90e2432a7f9469b71851af90e834aa2c 100644 (file)
 
 #define ARGPAR_ASSERT(_cond) assert(_cond)
 
-static
+#ifdef __MINGW_PRINTF_FORMAT
+# define ARGPAR_PRINTF_FORMAT __MINGW_PRINTF_FORMAT
+#else
+# define ARGPAR_PRINTF_FORMAT printf
+#endif
+
+static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 0)))
 char *argpar_vasprintf(const char *fmt, va_list args)
 {
        int len1, len2;
@@ -60,11 +66,12 @@ char *argpar_vasprintf(const char *fmt, va_list args)
        ARGPAR_ASSERT(len1 == len2);
 
 end:
+       va_end(args2);
        return str;
 }
 
 
-static
+static __attribute__((format(ARGPAR_PRINTF_FORMAT, 1, 2)))
 char *argpar_asprintf(const char *fmt, ...)
 {
        va_list args;
@@ -77,7 +84,7 @@ char *argpar_asprintf(const char *fmt, ...)
        return str;
 }
 
-static
+static __attribute__((format(ARGPAR_PRINTF_FORMAT, 2, 3)))
 bool argpar_string_append_printf(char **str, const char *fmt, ...)
 {
        char *new_str = NULL;
@@ -446,6 +453,15 @@ enum parse_orig_arg_opt_ret parse_long_opt(const char * const long_opt_arg,
                        opt_arg = next_orig_arg;
                        *used_next_orig_arg = true;
                }
+       } else if (eq_pos) {
+               /*
+                * Unexpected `--opt=arg` style for a long option which
+                * doesn't accept an argument.
+                */
+               argpar_string_append_printf(&parse_ret->error,
+                       "Unexpected argument for option `--%s`",
+                       long_opt_name);
+               goto error;
        }
 
        /* Create and append option argument */
This page took 0.022353 seconds and 4 git commands to generate.