From d16385415c2f28cc66f30f7bdd4b85a4cb98c3ff Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Mon, 27 Apr 2020 23:14:56 +0200 Subject: [PATCH] Simplify va_start/_end handling and return ENOMEM if alloca() fails Be consistent with errno codes from libite, we already have other APIs that return ENOMEM if alloca() fails, so let's do the same for these new ones as well. Also, simplify va_start()/va_end() handling in these APIs, no point in the extra complexity before this patch. Signed-off-by: Joachim Nilsson --- src/fopenf.c | 14 ++++++++------ src/makepath.c | 4 +++- src/systemf.c | 3 +-- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/fopenf.c b/src/fopenf.c index a56a55b..e92995f 100644 --- a/src/fopenf.c +++ b/src/fopenf.c @@ -15,6 +15,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include #include #include #include @@ -34,7 +35,6 @@ FILE *fopenf(const char *mode, const char *fmt, ...) { va_list ap; - FILE *fp = NULL; char *file; int len; @@ -42,15 +42,17 @@ FILE *fopenf(const char *mode, const char *fmt, ...) len = vsnprintf(NULL, 0, fmt, ap); va_end(ap); - va_start(ap, fmt); file = alloca(len + 1); - if (file) { - vsnprintf(file, len + 1, fmt, ap); - fp = fopen(file, mode); + if (!file) { + errno = ENOMEM; + return NULL; } + + va_start(ap, fmt); + vsnprintf(file, len + 1, fmt, ap); va_end(ap); - return fp; + return fopen(file, mode); } /** diff --git a/src/makepath.c b/src/makepath.c index 810806e..8809653 100644 --- a/src/makepath.c +++ b/src/makepath.c @@ -70,8 +70,10 @@ int fmkpath(mode_t mode, const char *fmt, ...) va_end(ap); path = alloca(len + 1); - if (!path) + if (!path) { + errno = ENOMEM; return -1; + } va_start(ap, fmt); len = vsnprintf(path, len + 1, fmt, ap); diff --git a/src/systemf.c b/src/systemf.c index fbafe72..79ba38e 100644 --- a/src/systemf.c +++ b/src/systemf.c @@ -32,14 +32,13 @@ int systemf(const char *fmt, ...) len = vsnprintf(NULL, 0, fmt, ap); va_end(ap); - va_start(ap, fmt); cmd = alloca(++len); if (!cmd) { - va_end(ap); errno = ENOMEM; return -1; } + va_start(ap, fmt); vsnprintf(cmd, len, fmt, ap); va_end(ap);