2014-10-12 20:38:29

by Rickard Strandqvist

[permalink] [raw]
Subject: [PATCH] tools: perf: util: sort.c: Changed from using strncat to strlcat

Changed from using strncat to strlcat to simplify the code

Signed-off-by: Rickard Strandqvist <[email protected]>
---
tools/perf/util/sort.c | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
index 14e5a03..6e242f0 100644
--- a/tools/perf/util/sort.c
+++ b/tools/perf/util/sort.c
@@ -759,8 +759,7 @@ static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
size_t size, unsigned int width)
{
char out[64];
- size_t sz = sizeof(out) - 1; /* -1 for null termination */
- size_t i, l = 0;
+ size_t i;
u64 m = PERF_MEM_SNOOP_NA;

out[0] = '\0';
@@ -771,12 +770,9 @@ static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
if (!(m & 0x1))
continue;
- if (l) {
- strcat(out, " or ");
- l += 4;
- }
- strncat(out, snoop_access[i], sz - l);
- l += strlen(snoop_access[i]);
+ if (*out != '\0')
+ strlcat(out, " or ", sizeof(out));
+ strlcat(out, snoop_access[i], sizeof(out));
}

if (*out == '\0')
--
1.7.10.4


2014-10-13 12:44:19

by Jiri Olsa

[permalink] [raw]
Subject: Re: [PATCH] tools: perf: util: sort.c: Changed from using strncat to strlcat

On Sun, Oct 12, 2014 at 10:40:35PM +0200, Rickard Strandqvist wrote:
> Changed from using strncat to strlcat to simplify the code
>
> Signed-off-by: Rickard Strandqvist <[email protected]>
> ---
> tools/perf/util/sort.c | 12 ++++--------
> 1 file changed, 4 insertions(+), 8 deletions(-)
>
> diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
> index 14e5a03..6e242f0 100644
> --- a/tools/perf/util/sort.c
> +++ b/tools/perf/util/sort.c
> @@ -759,8 +759,7 @@ static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
> size_t size, unsigned int width)
> {
> char out[64];
> - size_t sz = sizeof(out) - 1; /* -1 for null termination */
> - size_t i, l = 0;
> + size_t i;
> u64 m = PERF_MEM_SNOOP_NA;
>
> out[0] = '\0';
> @@ -771,12 +770,9 @@ static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
> for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
> if (!(m & 0x1))
> continue;
> - if (l) {
> - strcat(out, " or ");
> - l += 4;
> - }
> - strncat(out, snoop_access[i], sz - l);
> - l += strlen(snoop_access[i]);
> + if (*out != '\0')
> + strlcat(out, " or ", sizeof(out));
> + strlcat(out, snoop_access[i], sizeof(out));

I've got compile error..

[jolsa@krava perf]$ make JOBS=1
BUILD: Doing 'make -j1' parallel build
CC perf.o
CC builtin-annotate.o
CC builtin-bench.o
CC bench/sched-messaging.o
CC bench/sched-pipe.o
CC util/sort.o
util/sort.c: In function ‘hist_entry__snoop_snprintf’:
util/sort.c:778:4: error: implicit declaration of function ‘strlcat’ [-Werror=implicit-function-declaration]
strlcat(out, " or ", sizeof(out));
^
util/sort.c:778:4: error: nested extern declaration of ‘strlcat’ [-Werror=nested-externs]
cc1: all warnings being treated as errors
make[1]: *** [util/sort.o] Error 1
make: *** [all] Error 2

jirka

2014-10-13 20:54:00

by Rickard Strandqvist

[permalink] [raw]
Subject: Re: [PATCH] tools: perf: util: sort.c: Changed from using strncat to strlcat

2014-10-13 14:44 GMT+02:00 Jiri Olsa <[email protected]>:
> On Sun, Oct 12, 2014 at 10:40:35PM +0200, Rickard Strandqvist wrote:
>> Changed from using strncat to strlcat to simplify the code
>>
>> Signed-off-by: Rickard Strandqvist <[email protected]>
>> ---
>> tools/perf/util/sort.c | 12 ++++--------
>> 1 file changed, 4 insertions(+), 8 deletions(-)
>>
>> diff --git a/tools/perf/util/sort.c b/tools/perf/util/sort.c
>> index 14e5a03..6e242f0 100644
>> --- a/tools/perf/util/sort.c
>> +++ b/tools/perf/util/sort.c
>> @@ -759,8 +759,7 @@ static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
>> size_t size, unsigned int width)
>> {
>> char out[64];
>> - size_t sz = sizeof(out) - 1; /* -1 for null termination */
>> - size_t i, l = 0;
>> + size_t i;
>> u64 m = PERF_MEM_SNOOP_NA;
>>
>> out[0] = '\0';
>> @@ -771,12 +770,9 @@ static int hist_entry__snoop_snprintf(struct hist_entry *he, char *bf,
>> for (i = 0; m && i < NUM_SNOOP_ACCESS; i++, m >>= 1) {
>> if (!(m & 0x1))
>> continue;
>> - if (l) {
>> - strcat(out, " or ");
>> - l += 4;
>> - }
>> - strncat(out, snoop_access[i], sz - l);
>> - l += strlen(snoop_access[i]);
>> + if (*out != '\0')
>> + strlcat(out, " or ", sizeof(out));
>> + strlcat(out, snoop_access[i], sizeof(out));
>
> I've got compile error..
>
> [jolsa@krava perf]$ make JOBS=1
> BUILD: Doing 'make -j1' parallel build
> CC perf.o
> CC builtin-annotate.o
> CC builtin-bench.o
> CC bench/sched-messaging.o
> CC bench/sched-pipe.o
> CC util/sort.o
> util/sort.c: In function ‘hist_entry__snoop_snprintf’:
> util/sort.c:778:4: error: implicit declaration of function ‘strlcat’ [-Werror=implicit-function-declaration]
> strlcat(out, " or ", sizeof(out));
> ^
> util/sort.c:778:4: error: nested extern declaration of ‘strlcat’ [-Werror=nested-externs]
> cc1: all warnings being treated as errors
> make[1]: *** [util/sort.o] Error 1
> make: *** [all] Error 2


Hi

Sorry, I saw an improvement without thinking about that tools/ does
not have access to those functions :-(


Kind regards
Rickard Strandqvist