X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=blobdiff_plain;f=src%2Frrd_error.c;h=e368ca755f86198426e83e50e626f8ad5489f2da;hp=33d7c2f0c849abbb37d8046cfc197751d5ccf8f4;hb=7bd35b8d099910071a57c66687f6ef79c5df9a5c;hpb=298b318acaf1147474ab9e97cac37c3608660efd diff --git a/src/rrd_error.c b/src/rrd_error.c index 33d7c2f..e368ca7 100644 --- a/src/rrd_error.c +++ b/src/rrd_error.c @@ -1,10 +1,14 @@ /***************************************************************************** - * RRDtool 1.1.x Copyright Tobias Oetiker, 1997 - 2002 + * RRDtool 1.3rc7 Copyright by Tobi Oetiker, 1997-2008 ***************************************************************************** * rrd_error.c Common Header File ***************************************************************************** * $Id$ * $Log$ + * Revision 1.4 2003/02/22 21:57:03 oetiker + * a patch to avoid a memory leak and a Makefile.am patch to + * distribute all required source files -- Peter Stamfest + * * Revision 1.3 2003/02/13 07:05:27 oetiker * Find attached the patch I promised to send to you. Please note that there * are three new source files (src/rrd_is_thread_safe.h, src/rrd_thread_safe.c @@ -33,32 +37,37 @@ #define ERRBUFLEN 256 #define CTX (rrd_get_context()) -void -rrd_set_error(char *fmt, ...) +void rrd_set_error( + char *fmt, + ...) { - va_list argp; + va_list argp; + rrd_clear_error(); va_start(argp, fmt); #ifdef HAVE_VSNPRINTF - vsnprintf(CTX->rrd_error, CTX->len, fmt, argp); + vsnprintf(CTX->rrd_error, sizeof(CTX->rrd_error), fmt, argp); #else vsprintf(CTX->rrd_error, fmt, argp); #endif va_end(argp); } -int -rrd_test_error(void) { +int rrd_test_error( + void) +{ return CTX->rrd_error[0] != '\0'; } -void -rrd_clear_error(void){ +void rrd_clear_error( + void) +{ CTX->rrd_error[0] = '\0'; } -char * -rrd_get_error(void){ +char *rrd_get_error( + void) +{ return CTX->rrd_error; } @@ -68,73 +77,74 @@ rrd_get_error(void){ operations on them... Then a single thread may use more than one context. Using these functions would require to change each and every function containing any of the non _r versions... */ -void -rrd_set_error_r(struct rrd_context *rrd_ctx, char *fmt, ...) +void rrd_set_error_r( + struct rrd_context *rrd_ctx, + char *fmt, + ...) { - va_list argp; + va_list argp; + rrd_clear_error_r(rrd_ctx); va_start(argp, fmt); #ifdef HAVE_VSNPRINTF - vsnprintf((char *)rrd_ctx->rrd_error, rrd_ctx->len, fmt, argp); + vsnprintf(rrd_ctx->rrd_error, sizeof(rrd_ctx->rrd_error), fmt, argp); + rrd_ctx->rrd_error[sizeof(rrd_ctx->rrd_error) - 1] = '\0'; #else - vsprintf((char *)rrd_ctx->rrd_error, fmt, argp); + vsprintf(rrd_ctx->rrd_error, fmt, argp); #endif va_end(argp); } -int -rrd_test_error_r(struct rrd_context *rrd_ctx) { +int rrd_test_error_r( + struct rrd_context *rrd_ctx) +{ return rrd_ctx->rrd_error[0] != '\0'; } -void -rrd_clear_error_r(struct rrd_context *rrd_ctx) { +void rrd_clear_error_r( + struct rrd_context *rrd_ctx) +{ rrd_ctx->rrd_error[0] = '\0'; } -char * -rrd_get_error_r(struct rrd_context *rrd_ctx) { - return (char *)rrd_ctx->rrd_error; +char *rrd_get_error_r( + struct rrd_context *rrd_ctx) +{ + return rrd_ctx->rrd_error; } #endif /* PS: Should we move this to some other file? It is not really error related. */ -struct rrd_context * -rrd_new_context(void) { - struct rrd_context *rrd_ctx = - (struct rrd_context *) malloc(sizeof(struct rrd_context)); +struct rrd_context *rrd_new_context( + void) +{ + struct rrd_context *rrd_ctx = + (struct rrd_context *) malloc(sizeof(struct rrd_context)); - if (rrd_ctx) { - rrd_ctx->len = 0; - rrd_ctx->rrd_error = malloc(MAXLEN); - rrd_ctx->lib_errstr = malloc(ERRBUFLEN); - if (rrd_ctx->rrd_error && rrd_ctx->lib_errstr) { - *rrd_ctx->rrd_error = 0; - *rrd_ctx->lib_errstr = 0; - rrd_ctx->len = MAXLEN; - rrd_ctx->errlen = ERRBUFLEN; - return rrd_ctx; - } - if (rrd_ctx->rrd_error) free(rrd_ctx->rrd_error); - if (rrd_ctx->lib_errstr) free(rrd_ctx->lib_errstr); - free(rrd_ctx); + if (!rrd_ctx) { + return NULL; } - return NULL; + + rrd_ctx->rrd_error[0] = '\0'; + rrd_ctx->lib_errstr[0] = '\0'; + return rrd_ctx; } -void -rrd_free_context(struct rrd_context *rrd_ctx) { +void rrd_free_context( + struct rrd_context *rrd_ctx) +{ if (rrd_ctx) { - if (rrd_ctx->rrd_error) free(rrd_ctx->rrd_error); - free(rrd_ctx); + free(rrd_ctx); } } #if 0 -void rrd_globalize_error(struct rrd_context *rrd_ctx) { +void rrd_globalize_error( + struct rrd_context *rrd_ctx) +{ if (rrd_ctx) { - rrd_set_error(rrd_ctx->rrd_error); + rrd_set_error(rrd_ctx->rrd_error); } } #endif