perf: fix -Wstring-compare
PATCH 1/1 perf: fix -Wstring-compare
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:51: error: result of comparison against a string
literal is unspecified (use an explicit string comparison function
instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:18: error: result of comparison against a string
literal is unspecified (use an explicit string
comparison function instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:51: error: result of comparison against a string
literal is unspecified (use an explicit string comparison function
instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/map.c:434:15: error: result of comparison against a string literal
is unspecified (use an explicit string comparison function instead)
[-Werror,-Wstring-compare]
if (srcline != SRCLINE_UNKNOWN)
^ ~~~~~~~~~~~~~~~---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.
tools/perf/builtin-diff.c | 3 ++- tools/perf/util/block-info.c | 3 ++- tools/perf/util/map.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-diff.c +2 -1
@@ -1312,7 +1312,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s] %4ld" , start_line, end_line, block_he->diff.cycles); }else {
tools/perf/util/block-info.c +2 -1
@@ -295,7 +295,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s]" , start_line, end_line); }else {
tools/perf/util/map.c +1 -1
@@ -431,7 +431,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,if (map && map->dso) {char *srcline =map__srcline (map, addr,NULL ); -if (srcline !=SRCLINE_UNKNOWN ) +if (strncmp (srcline,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) ret =fprintf (fp,"%s%s" , prefix, srcline);free_srcline (srcline); }
Signature · 1 line
2.17.1
Ian Rogers
reviewerQuoted 29 lines · Nick Desaulniers
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/map.c:434:15: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (srcline != SRCLINE_UNKNOWN) ^ ~~~~~~~~~~~~~~~
Quoted 5 lines
Signed-off-by Nick Desaulniers <[email protected]>---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).
Quoted 53 lines
tools/perf/builtin-diff.c | 3 ++- tools/perf/util/block-info.c | 3 ++- tools/perf/util/map.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-)tools/perf/builtin-diff.c +2
-1@@ -1312,7 +1312,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s] %4ld" , start_line, end_line, block_he->diff.cycles); }else {tools/perf/util/block-info.c +2
-1@@ -295,7 +295,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s]" , start_line, end_line); }else {tools/perf/util/map.c +1
-2@@ -431,7 +431,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,if (map && map->dso) {char *srcline =map__srcline (map, addr,NULL ); -if (srcline !=SRCLINE_UNKNOWN ) +if (strncmp (srcline,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) ret =fprintf (fp,"%s%s" , prefix, srcline);free_srcline (srcline); } --2.17.1
--
You received this message because you are subscribed to the Google Groups "Clang Built Linux" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/clang-built-linux/20200223193456.25291-1-nick.desaulniers%40gmail.com.
From: Ian Rogers
Quoted 38 lines
Sent: 24 February 2020 05:56
Quoted 29 lines · Nick Desaulniers
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/map.c:434:15: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (srcline != SRCLINE_UNKNOWN) ^ ~~~~~~~~~~~~~~~Quoted 5 lines
Signed-off-by Nick Desaulniers <[email protected]>---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.
Quoted 6 lines
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).
Just change the (probable):
#define SRCLINE_UNKNOWN "unknown"
with
static const char SRC_LINE_UNKNOWN[] = "unk";
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 39 lines · David Laight
From: Ian Rogers
Quoted 37 lines
Sent: 24 February 2020 05:56
Quoted 34 lines · Nick Desaulniers
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/map.c:434:15: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (srcline != SRCLINE_UNKNOWN) ^ ~~~~~~~~~~~~~~~Signed-off-by Nick Desaulniers <[email protected]>---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.
Quoted 7 lines
Quoted 6 lines
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).
Quoted 6 lines
Just change the (probable):
#defineSRCLINE_UNKNOWN"unknown"
with
static const char SRC_LINE_UNKNOWN[] = "unk";David
The SRCLINE_UNKNOWN is used to convey information. Having multiple distinct pointers (static) would mean the compiler could likely remove all comparisons as the compiler could prove that pointer is never returned by a function - ie comparisons are either known to be true (!=) or false (==).
Thanks,
Ian
Quoted 3 lines
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 56 lines · Ian Rogers' via Clang Built Linux
Quoted 39 lines · David Laight
From: Ian Rogers
Quoted 37 lines
Sent: 24 February 2020 05:56
Quoted 34 lines · Nick Desaulniers
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/map.c:434:15: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (srcline != SRCLINE_UNKNOWN) ^ ~~~~~~~~~~~~~~~Signed-off-by Nick Desaulniers <[email protected]>---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.Reviewed-by Ian Rogers <[email protected]>Quoted 12 lines
Quoted 6 lines
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).Just change the (probable):
#defineSRCLINE_UNKNOWN"unknown"
with
static const char SRC_LINE_UNKNOWN[] = "unk";
Quoted 8 lines
Quoted 1 line
David
The
SRCLINE_UNKNOWNis used to convey information. Having multiple distinct pointers (static) would mean the compiler could likely remove all comparisons as the compiler could prove that pointer is never returned by a function - ie comparisons are either known to be true (!=) or false (==).
I wouldn't define a static string in a header. Though I could:
1. forward declare it in the header with extern linkage.
2. define it in *one* .c source file.
Thoughts on that vs the current approach?
Signature · 2 lines
Thanks, ~Nick Desaulniers
From: Nick Desaulniers
Quoted 68 lines
Sent: 24 February 2020 22:06
Quoted 56 lines · Ian Rogers' via Clang Built Linux
Quoted 39 lines · David Laight
From: Ian Rogers
Quoted 37 lines
Sent: 24 February 2020 05:56
Quoted 34 lines · Nick Desaulniers
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/map.c:434:15: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (srcline != SRCLINE_UNKNOWN) ^ ~~~~~~~~~~~~~~~Signed-off-by Nick Desaulniers <[email protected]>---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.Reviewed-by Ian Rogers <[email protected]>Quoted 12 lines
Quoted 6 lines
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).Just change the (probable):
#defineSRCLINE_UNKNOWN"unknown"
with
static const char SRC_LINE_UNKNOWN[] = "unk";Quoted 8 lines
Quoted 1 line
David
The
SRCLINE_UNKNOWNis used to convey information. Having multiple distinct pointers (static) would mean the compiler could likely remove all comparisons as the compiler could prove that pointer is never returned by a function - ie comparisons are either known to be true (!=) or false (==).
Quoted 5 lines
I wouldn't define a static string in a header. Though I could:
1. forward declare it in the header with extern linkage.
2. define it in *one* .c source file.Thoughts on that vs the current approach?
The string compares are just stupid.
If the 'fake' strings are not printed you could use:
#define SRCLINE_UNKNOWN ((const char *)1)
Otherwise defining the strings in one of the C files is better. Relying on the linker to merge the strings from different compilation units is so broken...
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)
Quoted 76 lines · David Laight
From: Nick Desaulniers
Quoted 68 lines
Sent: 24 February 2020 22:06
Quoted 56 lines · Ian Rogers' via Clang Built Linux
Quoted 39 lines · David Laight
From: Ian Rogers
Quoted 37 lines
Sent: 24 February 2020 05:56
Quoted 34 lines · Nick Desaulniers
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/map.c:434:15: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (srcline != SRCLINE_UNKNOWN) ^ ~~~~~~~~~~~~~~~Signed-off-by Nick Desaulniers <[email protected]>---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.Reviewed-by Ian Rogers <[email protected]>Quoted 12 lines
Quoted 6 lines
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).Just change the (probable):
#defineSRCLINE_UNKNOWN"unknown"
with
static const char SRC_LINE_UNKNOWN[] = "unk";Quoted 8 lines
Quoted 1 line
David
The
SRCLINE_UNKNOWNis used to convey information. Having multiple distinct pointers (static) would mean the compiler could likely remove all comparisons as the compiler could prove that pointer is never returned by a function - ie comparisons are either known to be true (!=) or false (==).Quoted 5 lines
I wouldn't define a static string in a header. Though I could:
1. forward declare it in the header with extern linkage.
2. define it in *one* .c source file.Thoughts on that vs the current approach?
Quoted 7 lines
The string compares are just stupid.
If the 'fake' strings are not printed you could use:
#defineSRCLINE_UNKNOWN((const char *)1)Otherwise defining the strings in one of the C files is better. Relying on the linker to merge the strings from different compilation units is so broken...
Note: it looks like free_srcline() already does strcmp, so my patch basically does a more consistent job for string comparisons. Forward declaring then defining in tools/perf/util/srcline.c involves changing the function signatures and struct members to const char* rather than char*, which is of questionable value. I wouldn't mind changing my patch to just use strcmp instead of strncmp, or convert free_srcline() to use strncmp instead, if folks felt strongly about being consistent. Otherwise I think my patch with Ian's Reviewed-by is the best approach.
Signature · 2 lines
Thanks, ~Nick Desaulniers
Em Tue, Feb 25, 2020 at 01:28:50PM -0800, Nick Desaulniers escreveu:
Quoted 85 lines · David Laight
Quoted 76 lines
From: Nick Desaulniers
Quoted 68 lines
Sent: 24 February 2020 22:06
Quoted 56 lines · Ian Rogers' via Clang Built Linux
Quoted 39 lines · David Laight
From: Ian Rogers
Quoted 37 lines
Sent: 24 February 2020 05:56
Quoted 34 lines · Nick Desaulniers
Clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/block-info.c:298:51: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) { ^ ~~~~~~~~~~~~~~~ util/map.c:434:15: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare] if (srcline != SRCLINE_UNKNOWN) ^ ~~~~~~~~~~~~~~~Signed-off-by Nick Desaulniers <[email protected]>---
Note: was generated off of mainline; can rebase on -next if it doesn't apply cleanly.Reviewed-by Ian Rogers <[email protected]>Quoted 12 lines
Quoted 6 lines
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).Just change the (probable):
#defineSRCLINE_UNKNOWN"unknown"
with
static const char SRC_LINE_UNKNOWN[] = "unk";Quoted 8 lines
Quoted 1 line
David
The
SRCLINE_UNKNOWNis used to convey information. Having multiple distinct pointers (static) would mean the compiler could likely remove all comparisons as the compiler could prove that pointer is never returned by a function - ie comparisons are either known to be true (!=) or false (==).Quoted 5 lines
I wouldn't define a static string in a header. Though I could:
1. forward declare it in the header with extern linkage.
2. define it in *one* .c source file.Thoughts on that vs the current approach?
Quoted 7 lines
The string compares are just stupid.
If the 'fake' strings are not printed you could use:
#defineSRCLINE_UNKNOWN((const char *)1)Otherwise defining the strings in one of the C files is better. Relying on the linker to merge the strings from different compilation units is so broken...
Quoted 9 lines
Note: it looks like
free_srcline()already does strcmp, so my patch basically does a more consistent job for string comparisons. Forward declaring then defining intools/perf/util/srcline.cinvolves changing the function signatures and struct members toconst char*rather thanchar*, which is of questionable value. I wouldn't mind changing my patch to just use strcmp instead of strncmp, or convertfree_srcline()to use strncmp instead, if folks felt strongly about being consistent. Otherwise I think my patch with Ian's Reviewed-by is the best approach.
Fair enough, applying it with Ian's reviewed-by,
- Arnaldo
[tip: perf/urgent] perf diff: Fix undefined string comparision spotted by clang's -Wstring-compare
In reply to Nick DesaulniersThe following commit has been merged into the perf/urgent branch of tip:
The following commit has been merged into the perf/urgent branch of tip:
Commit-ID: cfd3bc752a3f5529506d279deb42e3bc8055695b Gitweb: https://git.kernel.org/tip/cfd3bc752a3f5529506d279deb42e3bc8055695b Author: Nick Desaulniers <[email protected]> AuthorDate: Sun, 23 Feb 2020 11:34:49 -08:00 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitterDate: Fri, 06 Mar 2020 08:30:29 -03:00
perf diff: Fix undefined string comparision spotted by clang's -Wstring-compare
clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:51: error: result of comparison against a string
literal is unspecified (use an explicit string comparison function
instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:18: error: result of comparison against a string
literal is unspecified (use an explicit string
comparison function instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:51: error: result of comparison against a string
literal is unspecified (use an explicit string comparison function
instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/map.c:434:15: error: result of comparison against a string literal
is unspecified (use an explicit string comparison function instead)
[-Werror,-Wstring-compare]
if (srcline != SRCLINE_UNKNOWN)
^ ~~~~~~~~~~~~~~~Reviewer Notes:
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).
--- tools/perf/builtin-diff.c | 3 ++- tools/perf/util/block-info.c | 3 ++- tools/perf/util/map.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-diff.c +2 -1
@@ -1312,7 +1312,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s] %4ld" , start_line, end_line, block_he->diff.cycles); }else {
tools/perf/util/block-info.c +2 -1
@@ -295,7 +295,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s]" , start_line, end_line); }else {
tools/perf/util/map.c +1 -1
@@ -431,7 +431,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,if (map && map->dso) {char *srcline =map__srcline (map, addr,NULL ); -if (srcline !=SRCLINE_UNKNOWN ) +if (strncmp (srcline,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) ret =fprintf (fp,"%s%s" , prefix, srcline);free_srcline (srcline); }
[tip: perf/core] perf diff: Fix undefined string comparison spotted by clang's -Wstring-compare
In reply to Nick DesaulniersThe following commit has been merged into the perf/core branch of tip:
The following commit has been merged into the perf/core branch of tip:
Commit-ID: c395c3553d6870541f1c283479aea2a6f26364d5 Gitweb: https://git.kernel.org/tip/c395c3553d6870541f1c283479aea2a6f26364d5 Author: Nick Desaulniers <[email protected]> AuthorDate: Sun, 23 Feb 2020 11:34:49 -08:00 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitterDate: Wed, 04 Mar 2020 10:28:08 -03:00
perf diff: Fix undefined string comparison spotted by clang's -Wstring-compare
clang warns:
util/block-info.c:298:18: error: result of comparison against a string literal is unspecified (use an explicit string comparison function instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:51: error: result of comparison against a string
literal is unspecified (use an explicit string comparison function
instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:18: error: result of comparison against a string
literal is unspecified (use an explicit string
comparison function instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/block-info.c:298:51: error: result of comparison against a string
literal is unspecified (use an explicit string comparison function
instead) [-Werror,-Wstring-compare]
if ((start_line != SRCLINE_UNKNOWN) && (end_line != SRCLINE_UNKNOWN)) {
^ ~~~~~~~~~~~~~~~
util/map.c:434:15: error: result of comparison against a string literal
is unspecified (use an explicit string comparison function instead)
[-Werror,-Wstring-compare]
if (srcline != SRCLINE_UNKNOWN)
^ ~~~~~~~~~~~~~~~Reviewer Notes:
Looks good to me. Some more context:
https://clang.llvm.org/docs/DiagnosticsReference.html#wstring-compare
The spec says:
J.1 Unspecified behavior
The following are unspecified:
.. Whether two string literals result in distinct arrays (6.4.5).
--- tools/perf/builtin-diff.c | 3 ++- tools/perf/util/block-info.c | 3 ++- tools/perf/util/map.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-)
tools/perf/builtin-diff.c +2 -1
@@ -1312,7 +1312,8 @@ static int cycles_printf(struct hist_entry *he, struct hist_entry *pair, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s] %4ld" , start_line, end_line, block_he->diff.cycles); }else {
tools/perf/util/block-info.c +2 -1
@@ -295,7 +295,8 @@ static int block_range_entry(struct perf_hpp_fmt *fmt, struct perf_hpp *hpp, end_line =map__srcline (he->ms.map, bi->sym->start + bi->end, he->ms.sym); -if ((start_line !=SRCLINE_UNKNOWN ) && (end_line !=SRCLINE_UNKNOWN )) { +if ((strncmp (start_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) && + (strncmp (end_line,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 )) {scnprintf (buf,sizeof (buf),"[%s -> %s]" , start_line, end_line); }else {
tools/perf/util/map.c +1 -1
@@ -431,7 +431,7 @@ int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,if (map && map->dso) {char *srcline =map__srcline (map, addr,NULL ); -if (srcline !=SRCLINE_UNKNOWN ) +if (strncmp (srcline,SRCLINE_UNKNOWN ,strlen (SRCLINE_UNKNOWN )) !=0 ) ret =fprintf (fp,"%s%s" , prefix, srcline);free_srcline (srcline); }