From: Arran Cudbard-Bell Date: Mon, 16 Sep 2013 15:56:14 +0000 (+0100) Subject: Print VP \t \n as an atom to avoid issues when running with multipl... X-Git-Tag: release_3_0_0~49 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=aa0d069a90d41f1c5b0a831797bfb80e5b79406f;p=thirdparty%2Ffreeradius-server.git Print VP \t \n as an atom to avoid issues when running with multiple threads --- diff --git a/src/include/libradius.h b/src/include/libradius.h index b9c41a300d..d480b51ef3 100644 --- a/src/include/libradius.h +++ b/src/include/libradius.h @@ -348,7 +348,7 @@ int vp_prints_value_json(char *out, size_t outlen, VALUE_PAIR const *vp); size_t vp_print_name(char *buffer, size_t bufsize, unsigned int attr, unsigned int vendor); -int vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp); +size_t vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp); void vp_print(FILE *, VALUE_PAIR const *); void vp_printlist(FILE *, VALUE_PAIR const *); #define fprint_attr_val vp_print diff --git a/src/lib/print.c b/src/lib/print.c index 21ae1daded..b6e58b631d 100644 --- a/src/lib/print.c +++ b/src/lib/print.c @@ -790,7 +790,7 @@ size_t vp_print_name(char *buffer, size_t bufsize, /* * Print one attribute and value into a string. */ -int vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp) +size_t vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp) { size_t len; char const *token = NULL; @@ -825,23 +825,45 @@ int vp_prints(char *out, size_t outlen, VALUE_PAIR const *vp) } -/* - * Print one attribute and value. +/** Print one attribute and value. + * + * Complete string with '\n' and '\r' is written to buffer before printing to + * avoid issues when running with multiple threads. + * + * @param fp to output to. + * @param vp to print. */ void vp_print(FILE *fp, VALUE_PAIR const *vp) { char buf[1024]; + char *p = buf; + size_t ret; + + *p++ = '\t'; + ret = vp_prints(p, sizeof(buf) - 1, vp); + if (!ret) { + return; + } + p += ret; + + /* + * Deal with truncation gracefully + */ + if (((size_t) (p - buf)) >= sizeof(buf)) { + p = buf + (sizeof(buf) - 2); + } + + *p++ = '\n'; + *p = '\0'; - vp_prints(buf, sizeof(buf), vp); - fputc('\t', fp); fputs(buf, fp); - fputc('\n', fp); } -/* - * Print a whole list of attributes, indented by a TAB - * and with a newline at the end. +/** Print a list of attributes and values + * + * @param fp to output to. + * @param vp to print. */ void vp_printlist(FILE *fp, VALUE_PAIR const *vp) {