diff options
| author | Yaniv Kaul <ykaul@redhat.com> | 2018-08-21 18:25:33 +0300 | 
|---|---|---|
| committer | Amar Tumballi <amarts@redhat.com> | 2018-08-24 16:26:36 +0000 | 
| commit | 938849a417727c85f1925dde641b3c6c54c71275 (patch) | |
| tree | be05eab666860bd2fb486b5c51eb622e57275efd /cli/src/cli-xml-output.c | |
| parent | 036327d9e963c84a5222f8d1c7598ab36ad46a4b (diff) | |
{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 <ykaul@redhat.com>
Diffstat (limited to 'cli/src/cli-xml-output.c')
| -rw-r--r-- | cli/src/cli-xml-output.c | 6 | 
1 files changed, 4 insertions, 2 deletions
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;          /* <file> */          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);  | 
