aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLAN-TW <lantw44@gmail.com>2014-01-07 02:22:05 +0800
committerLAN-TW <lantw44@gmail.com>2014-01-07 02:22:05 +0800
commit4831829a051e87baa0293ecc77e512594a4d03e6 (patch)
tree91ec2bf6169774d1757e84fe875facf99272e02f
parentc0fa0b8b065ce06a8a315e0e52d04d4d12b53fde (diff)
downloadl4basic-4831829a051e87baa0293ecc77e512594a4d03e6.tar.gz
l4basic-4831829a051e87baa0293ecc77e512594a4d03e6.tar.zst
l4basic-4831829a051e87baa0293ecc77e512594a4d03e6.zip
Fallback to malloc if VLA is not available
-rw-r--r--l4arg.c28
1 files changed, 26 insertions, 2 deletions
diff --git a/l4arg.c b/l4arg.c
index 30b2b67..5e9f8b1 100644
--- a/l4arg.c
+++ b/l4arg.c
@@ -18,8 +18,24 @@ LbsStrv* lbs_arg_parse (const char* str, const char* delim,
for (qlen = 0; q[qlen].left != NULL && q[qlen].right != NULL; qlen++);
// qlen will not be too long, so we can use VLA
- int qllen[qlen <= 0 ? 1 : qlen]; // left quoting string length
- int qrlen[qlen <= 0 ? 1 : qlen]; // right quoting string length
+ int qlen_p = qlen <= 0 ? 1 : qlen;
+
+#if __STDC_NO_VLA__
+ int* qllen = malloc (sizeof (int) * qlen_p);
+ if (qllen == NULL) {
+ lbs_strv_unref (strv);
+ return NULL;
+ }
+ int* qrlen = malloc (sizeof (int) * qlen_p);
+ if (qrlen == NULL) {
+ lbs_strv_unref (strv);
+ return NULL;
+ }
+#else
+ int qllen[qlen_p]; // left quoting string length
+ int qrlen[qlen_p]; // right quoting string length
+#endif
+
for (int i = 0; i < qlen; i++) {
// empty strings are not allowed
qllen[i] = strlen (q[i].left);
@@ -130,6 +146,10 @@ loop_start:
*detail_ptr = detail;
}
+#if __STDC_NO_VLA__
+ free (qllen);
+ free (qrlen);
+#endif
return strv;
@@ -141,6 +161,10 @@ free_detail:
}
free_strv:
+#if __STDC_NO_VLA__
+ free (qllen);
+ free (qrlen);
+#endif
lbs_strv_unref (strv);
return NULL;
}