diff options
author | LAN-TW <lantw44@gmail.com> | 2014-01-07 02:22:05 +0800 |
---|---|---|
committer | LAN-TW <lantw44@gmail.com> | 2014-01-07 02:22:05 +0800 |
commit | 4831829a051e87baa0293ecc77e512594a4d03e6 (patch) | |
tree | 91ec2bf6169774d1757e84fe875facf99272e02f | |
parent | c0fa0b8b065ce06a8a315e0e52d04d4d12b53fde (diff) | |
download | l4basic-4831829a051e87baa0293ecc77e512594a4d03e6.tar.gz l4basic-4831829a051e87baa0293ecc77e512594a4d03e6.tar.zst l4basic-4831829a051e87baa0293ecc77e512594a4d03e6.zip |
Fallback to malloc if VLA is not available
-rw-r--r-- | l4arg.c | 28 |
1 files changed, 26 insertions, 2 deletions
@@ -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; } |