From 7e6e38060fffaeb47faf1415cfc55c29a7ddc620 Mon Sep 17 00:00:00 2001 From: Ralph Lange Date: Tue, 11 Sep 2012 16:58:43 +0200 Subject: [PATCH] catools: Change behaviour of camonitor when printing array of chars as string camonitor was using strlen() to find out the length of the array to print as %s string, which led to printing old buffer contents when the array-string was not null terminated. Now uses the minimum of strlen(), elements received, and elements requested. Suggested by Mark Rivers on tech-talk (11 Sep 2012) --- src/catools/tool_lib.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/catools/tool_lib.c b/src/catools/tool_lib.c index 13d6330cf..380457756 100644 --- a/src/catools/tool_lib.c +++ b/src/catools/tool_lib.c @@ -445,10 +445,13 @@ char *dbr2str (const void *value, unsigned type) \ if (charArrAsStr && dbr_type_is_CHAR(TYPE_ENUM) && (reqElems || pv->nElems > 1)) { \ dbr_char_t *s = (dbr_char_t*) dbr_value_ptr(pv->value, pv->dbrType); \ - int dlen = epicsStrnEscapedFromRawSize((char*)s, strlen((char*)s)); \ + size_t len = strlen((char*)s); \ + unsigned long elems = reqElems && (reqElems < pv->nElems) ? reqElems : pv->nElems; \ + if (len < elems) elems = len; \ + int dlen = epicsStrnEscapedFromRawSize((char*)s, elems); \ char *d = calloc(dlen+1, sizeof(char)); \ if(d) { \ - epicsStrnEscapedFromRaw(d, dlen+1, (char*)s, strlen((char*)s));\ + epicsStrnEscapedFromRaw(d, dlen+1, (char*)s, elems); \ printf("%c%s", fieldSeparator, d); \ free(d); \ } else { \