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 <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson
2020-04-27 23:14:56 +02:00
parent d27518c5b8
commit d16385415c
3 changed files with 12 additions and 9 deletions
+8 -6
View File
@@ -15,6 +15,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -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);
}
/**
+3 -1
View File
@@ -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);
+1 -2
View File
@@ -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);