BR3028880: Fix NULL dereference on nonexistent environment variable
[nasm.git] / lib / vsnprintf.c
blobbc54f9496414d6d12d7a869912ff2559b0163a26
1 /*
2 * vsnprintf()
4 * Poor substitute for a real vsnprintf() function for systems
5 * that don't have them...
6 */
8 #include "compiler.h"
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdarg.h>
13 #include <string.h>
15 #include "nasmlib.h"
17 #define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
19 static char snprintf_buffer[BUFFER_SIZE];
21 int vsnprintf(char *str, size_t size, const char *format, va_list ap)
23 int rv, bytes;
25 if (size > BUFFER_SIZE) {
26 nasm_error(ERR_PANIC|ERR_NOFILE,
27 "vsnprintf: size (%d) > BUFFER_SIZE (%d)",
28 size, BUFFER_SIZE);
29 size = BUFFER_SIZE;
32 rv = vsprintf(snprintf_buffer, format, ap);
33 if (rv >= BUFFER_SIZE)
34 nasm_error(ERR_PANIC|ERR_NOFILE, "vsnprintf buffer overflow");
36 if (size > 0) {
37 if ((size_t)rv < size-1)
38 bytes = rv;
39 else
40 bytes = size-1;
41 memcpy(str, snprintf_buffer, bytes);
42 str[bytes] = '\0';
45 return rv;