summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: b079c44)
raw | patch | inline | side by side (parent: b079c44)
author | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Sun, 1 Jun 2008 20:44:01 +0000 (20:44 +0000) | ||
committer | oetiker <oetiker@a5681a0c-68f1-0310-ab6d-d61299d08faa> | |
Sun, 1 Jun 2008 20:44:01 +0000 (20:44 +0000) |
diff --git a/src/rrd.h b/src/rrd.h
index cb9914cf90a5604fb25c19453cd3d73ab25f753c..5f4145bde9a5d81c32a0156e354e18b65d795a06 100644 (file)
--- a/src/rrd.h
+++ b/src/rrd.h
/* 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 ebc51f4c7b7ed4d44ffc02435c76a699397ce076..e64e2df1179b01a05a20b578132a07c3f9e54fef 100644 (file)
--- a/src/rrd_error.c
+++ b/src/rrd_error.c
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);
}
}
index c68a161f280ad7082b721a472924a45a7ae5f56d..b05ee1af64832217c84e424cea790d23b555f90a 100644 (file)
#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;
}
diff --git a/src/rrd_thread_safe.c b/src/rrd_thread_safe.c
index 5ef978b95e5c19a20cbf024797a11c105fc1691a..3138cc8529ecb6c549645beff2ce74c05d5944a9 100644 (file)
--- a/src/rrd_thread_safe.c
+++ b/src/rrd_thread_safe.c
{
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;
}
index dba1c3e0b7a16b2443be6c3fd5a5b797a8021171..899fe93db44cd5c0b8bada2537ab3a1962bfa187 100644 (file)
--- a/src/rrd_thread_safe_nt.c
+++ b/src/rrd_thread_safe_nt.c
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;