From 938849a417727c85f1925dde641b3c6c54c71275 Mon Sep 17 00:00:00 2001 From: Yaniv Kaul Date: Tue, 21 Aug 2018 18:25:33 +0300 Subject: {cli-cmd-parser|cli-rpc-ops||cli-xml-output}.c: strncpy()->sprintf(), reduce strlen()'s strncpy may not be very efficient for short strings copied into a large buffer: If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written. Instead, use snprintf(). Also: - save the result of strlen() and re-use it when possible. - move from GF_CALLOC() to GF_MALLOC() for the strings. - move from strlen to sizeof() for const strings. Compile-tested only! Change-Id: I3cf49c5401ee100a5db6a4954c3d699ec1814c17 updates: bz#1193929 Signed-off-by: Yaniv Kaul --- cli/src/cli-xml-output.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'cli/src/cli-xml-output.c') diff --git a/cli/src/cli-xml-output.c b/cli/src/cli-xml-output.c index 78aa8439e9d..dcb1715e625 100644 --- a/cli/src/cli-xml-output.c +++ b/cli/src/cli-xml-output.c @@ -1746,6 +1746,7 @@ cli_xml_output_vol_top_rw_perf (xmlTextWriterPtr writer, dict_t *dict, long int time_usec = 0; char timestr[256] = {0,}; char key[1024] = {0,}; + int len; /* */ ret = xmlTextWriterStartElement (writer, (xmlChar *)"file"); @@ -1781,8 +1782,9 @@ cli_xml_output_vol_top_rw_perf (xmlTextWriterPtr writer, dict_t *dict, goto out; gf_time_fmt (timestr, sizeof timestr, time_sec, gf_timefmt_FT); - snprintf (timestr + strlen (timestr), - sizeof timestr - strlen (timestr), + len = strlen (timestr); + snprintf (timestr + len, + sizeof (timestr) - len, ".%"GF_PRI_SUSECONDS, time_usec); ret = xmlTextWriterWriteFormatElement (writer, (xmlChar *)"time", "%s", timestr); -- cgit