]> git.ipfire.org Git - thirdparty/rrdtool-1.x.git/commitdiff
Avoid using VLA in optparse.c under Windows (MSC) 822/head
authorWolfgang Stöggl <c72578@yahoo.de>
Tue, 19 Sep 2017 14:48:39 +0000 (16:48 +0200)
committerWolfgang Stöggl <c72578@yahoo.de>
Tue, 19 Sep 2017 14:48:39 +0000 (16:48 +0200)
- Fixes compilation errors, when building under Windows (e.g. VS2015):
  ..\src\optparse.c(195): error C2057: expected constant expression
  ..\src\optparse.c(195): error C2466: cannot allocate an array of
  constant size 0
  ..\src\optparse.c(195): error C2133: 'optstring': unknown size
- Variable length arrays are not currently supported in Visual Studio
- Use malloc() instead of variable length array in case of MSC

src/optparse.c

index 35391648e2c95aab6d42f304b6f60b7a577d3c77..9040ba8b240d30a63db851c4d3c0ef2069ce08da 100644 (file)
@@ -1,4 +1,7 @@
 #include <stdio.h>
+#ifdef _MSC_VER
+#include <stdlib.h>     /* for malloc(), free() */
+#endif
 #include "optparse.h"
 
 #define opterror(options, format, ...) \
@@ -192,7 +195,12 @@ long_fallback(struct optparse *options,
               const struct optparse_long *longopts,
               int *longindex)
 {
+#ifdef _MSC_VER
+    /* Variable length arrays are not currently supported in Visual Studio */
+    char *optstring = malloc(optstring_length(longopts));
+#else
     char optstring[optstring_length(longopts)];
+#endif
     optstring_from_long(longopts, optstring);
     int result = optparse(options, optstring);
     if (longindex != NULL) {
@@ -202,6 +210,9 @@ long_fallback(struct optparse *options,
                 if (longopts[i].shortname == options->optopt)
                     *longindex = i;
     }
+#ifdef _MSC_VER
+    free(optstring);
+#endif
     return result;
 }