diff --git a/libpcp/buffer.c b/libpcp/buffer.c index 5cae71e..1603380 100644 --- a/libpcp/buffer.c +++ b/libpcp/buffer.c @@ -19,6 +19,8 @@ You can contact me by mail: . */ +#define _GNU_SOURCE /* vasprintf() linux */ + #include "buffer.h" void buffer_init(Buffer *b, size_t blocksize, char *name) { @@ -88,11 +90,12 @@ void buffer_add_str(Buffer *b, const char * fmt, ...) { va_list ap; char *dst; va_start(ap, fmt); - vasprintf(&dst, fmt, ap); + if(vasprintf(&dst, fmt, ap) >= 0) { + if(b->end > 0) + b->end--; + buffer_add(b, dst, strlen(dst)+1); + } va_end(ap); - if(b->end > 0) - b->end--; - buffer_add(b, dst, strlen(dst)+1); free(dst); } diff --git a/libpcp/fatal.c b/libpcp/fatal.c index 8c84275..9614d2c 100644 --- a/libpcp/fatal.c +++ b/libpcp/fatal.c @@ -19,10 +19,12 @@ You can contact me by mail: . */ +#define _GNU_SOURCE /* vasprintf() linux */ #include "defines.h" #include "platform.h" + #include #include #include @@ -38,11 +40,14 @@ void fatal(const char * fmt, ...) { va_list ap; va_start(ap, fmt); - vasprintf(&PCP_ERR, fmt, ap); - - va_end(ap); - - PCP_ERRSET = 1; + if(vasprintf(&PCP_ERR, fmt, ap) >= 0) { + va_end(ap); + PCP_ERRSET = 1; + } + else { + fprintf(stderr, "Could not store fatal error message %s!\n", fmt); + PCP_ERRSET = 1; + } } void fatals_reset() { @@ -51,7 +56,7 @@ void fatals_reset() { void fatals_ifany() { if(PCP_ERRSET == 1) { - fprintf(stderr, PCP_ERR); + fprintf(stderr, "%s", PCP_ERR); if(errno) { fprintf(stderr, "Error: %s\n", strerror(errno)); } diff --git a/libpcp/pcpstream.c b/libpcp/pcpstream.c index 938e04f..361b14c 100644 --- a/libpcp/pcpstream.c +++ b/libpcp/pcpstream.c @@ -19,6 +19,7 @@ You can contact me by mail: . */ +#define _GNU_SOURCE /* vasprintf() linux */ #include "pcpstream.h" Pcpstream *ps_init(void) { @@ -618,17 +619,22 @@ size_t ps_print(Pcpstream *stream, const char * fmt, ...) { va_list ap; char *dst; va_start(ap, fmt); - vasprintf(&dst, fmt, ap); - va_end(ap); - size_t len = strlen(dst); + if(vasprintf(&dst, fmt, ap) >= 0) { + va_end(ap); + size_t len = strlen(dst); - if(stream->is_buffer) { - buffer_add(stream->b, dst, len); + if(stream->is_buffer) { + buffer_add(stream->b, dst, len); + } + else { + len = ps_write(stream, dst, len); + } + + free(dst); return len; } - else { - return ps_write(stream, dst, len); - } + va_end(ap); + return 0; } void ps_close(Pcpstream *stream) {