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
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);
}
char *rrd_get_error_r(
struct rrd_context *rrd_ctx)
{
- return (char *) rrd_ctx->rrd_error;
+ return rrd_ctx->rrd_error;
}
#endif
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);
}
}
#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 <stdarg.h> */
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;
}
{
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;
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;
}
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;