From: oetiker Date: Sun, 1 Jun 2008 20:44:01 +0000 (+0000) Subject: Patch for rrd_context system by sebastian harl, making it simpler and less error... X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=commitdiff_plain;h=b005d2cbe00924ddfcb8766d49201e4044abfc21 Patch for rrd_context system by sebastian harl, making it simpler and less error prone. Removing one buffer overflow in the process. git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1392 a5681a0c-68f1-0310-ab6d-d61299d08faa --- diff --git a/src/rrd.h b/src/rrd.h index cb9914c..5f4145b 100644 --- a/src/rrd.h +++ b/src/rrd.h @@ -231,10 +231,8 @@ extern "C" { /* END parsetime.h */ struct rrd_context { - int len; - int errlen; - char *lib_errstr; - char *rrd_error; + char lib_errstr[256]; + char rrd_error[4096]; }; /* returns the current per-thread rrd_context */ diff --git a/src/rrd_error.c b/src/rrd_error.c index ebc51f4..e64e2df 100644 --- a/src/rrd_error.c +++ b/src/rrd_error.c @@ -46,7 +46,7 @@ void rrd_set_error( 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 @@ -87,10 +87,10 @@ void rrd_set_error_r( rrd_clear_error_r(rrd_ctx); va_start(argp, fmt); #ifdef HAVE_VSNPRINTF - vsnprintf((char *) rrd_ctx->rrd_error, rrd_ctx->len, fmt, argp); - rrd_ctx->rrd_error[rrd_ctx->len] = '\0'; + 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); } @@ -110,7 +110,7 @@ void rrd_clear_error_r( char *rrd_get_error_r( struct rrd_context *rrd_ctx) { - return (char *) rrd_ctx->rrd_error; + return rrd_ctx->rrd_error; } #endif @@ -122,33 +122,19 @@ struct rrd_context *rrd_new_context( struct rrd_context *rrd_ctx = (struct rrd_context *) malloc(sizeof(struct rrd_context)); - if (rrd_ctx) { - rrd_ctx->rrd_error = malloc(MAXLEN + 10); - rrd_ctx->lib_errstr = malloc(ERRBUFLEN + 10); - 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) { if (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); } } diff --git a/src/rrd_not_thread_safe.c b/src/rrd_not_thread_safe.c index c68a161..b05ee1a 100644 --- a/src/rrd_not_thread_safe.c +++ b/src/rrd_not_thread_safe.c @@ -14,18 +14,12 @@ #define MAXLEN 4096 #define ERRBUFLEN 256 -static char rrd_error[MAXLEN + 10]; -static char rrd_liberror[ERRBUFLEN + 10]; -static int rrd_context_init = 0; - /* The global context is very useful in the transition period to even more thread-safe stuff, it can be used whereever we need a context and do not need to worry about concurrency. */ static struct rrd_context global_ctx = { - MAXLEN, - ERRBUFLEN, - rrd_error, - rrd_liberror + "", + "" }; /* #include */ @@ -33,11 +27,6 @@ static struct rrd_context global_ctx = { struct rrd_context *rrd_get_context( void) { - if (!rrd_context_init) { - rrd_context_init = 1; - global_ctx.rrd_error[0] = '\0'; - global_ctx.lib_errstr[0] = '\0'; - } return &global_ctx; } diff --git a/src/rrd_thread_safe.c b/src/rrd_thread_safe.c index 5ef978b..3138cc8 100644 --- a/src/rrd_thread_safe.c +++ b/src/rrd_thread_safe.c @@ -60,7 +60,7 @@ const char *rrd_strerror( { struct rrd_context *ctx = rrd_get_context(); - if (strerror_r(err, ctx->lib_errstr, ctx->errlen)) + if (strerror_r(err, ctx->lib_errstr, sizeof(ctx->lib_errstr))) return "strerror_r failed. sorry!"; else return ctx->lib_errstr; @@ -75,8 +75,8 @@ const char *rrd_strerror( ctx = rrd_get_context(); pthread_mutex_lock(&mtx); - strncpy(ctx->lib_errstr, strerror(err), ctx->errlen); - ctx->lib_errstr[ctx->errlen] = '\0'; + strncpy(ctx->lib_errstr, strerror(err), sizeof(ctx->lib_errstr)); + ctx->lib_errstr[sizeof(ctx->lib_errstr) - 1] = '\0'; pthread_mutex_unlock(&mtx); return ctx->lib_errstr; } diff --git a/src/rrd_thread_safe_nt.c b/src/rrd_thread_safe_nt.c index dba1c3e..899fe93 100644 --- a/src/rrd_thread_safe_nt.c +++ b/src/rrd_thread_safe_nt.c @@ -70,8 +70,8 @@ const char *rrd_strerror( ctx = rrd_get_context(); EnterCriticalSection(&CriticalSection); - strncpy(ctx->lib_errstr, strerror(err), ctx->errlen); - ctx->lib_errstr[ctx->errlen] = '\0'; + strncpy(ctx->lib_errstr, strerror(err), sizeof(ctx->lib_errstr)); + ctx->lib_errstr[sizeof(ctx->lib_errstr) - 1] = '\0'; LeaveCriticalSection(&CriticalSection); return ctx->lib_errstr;