From 1becad43298e74ba73bc66f9d44523e5d121c667 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Mon, 20 May 2024 14:48:35 +0200 Subject: Add vasprintf/asprintf The only hardening being done here is to set the char** parameter to thos functions to NULL in case of an error, to prevent it from being used should people forget to check return values. This is already done on some BSD, as well as in Rocky Linux. --- tests/test_vasprintf.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 tests/test_vasprintf.c (limited to 'tests/test_vasprintf.c') diff --git a/tests/test_vasprintf.c b/tests/test_vasprintf.c new file mode 100644 index 0000000..2f71714 --- /dev/null +++ b/tests/test_vasprintf.c @@ -0,0 +1,36 @@ +#define _GNU_SOURCE +#include "common.h" + +#include +#include +#include +#include + +void test(const char *fmt, ...) +{ + char* buf; + va_list args; + va_start(args, fmt); + vasprintf(&buf, fmt, args); + va_end(args); + puts(buf); + free(buf); +} + +void test2(const char *fmt, ...) +{ + char* buf; + va_list args; + va_start(args, fmt); + vasprintf(&buf, fmt, args); + va_end(args); + assert(buf == NULL); +} + +int main(int argc, char** argv) { + test("Total: %d+%d=%d", 1, 2, 3); +#ifndef __clang__ + test2("Total: %", 1, 2, 3); +#endif + return 0; +} -- cgit v1.3