Bug 217583 - -Wsign-compare warnings in URL.cpp and URLParser.cpp
Summary: -Wsign-compare warnings in URL.cpp and URLParser.cpp
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Template Framework (show other bugs)
Version: WebKit Nightly Build
Hardware: PC Linux
: P2 Normal
Assignee: Michael Catanzaro
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2020-10-11 12:58 PDT by Michael Catanzaro
Modified: 2020-10-12 10:40 PDT (History)
8 users (show)

See Also:


Attachments
Patch (1.93 KB, patch)
2020-10-11 13:01 PDT, Michael Catanzaro
no flags Details | Formatted Diff | Diff
Patch (1.96 KB, patch)
2020-10-12 07:40 PDT, Michael Catanzaro
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Catanzaro 2020-10-11 12:58:51 PDT
Integer literals are signed, so anything + 1 is signed:

[205/6600] Building CXX object Source/WTF/wtf/CMakeFiles/WTF.dir/URL.cpp.o
../../Source/WTF/wtf/URL.cpp: In member function ‘unsigned int WTF::URL::pathStart() const’:
../../Source/WTF/wtf/URL.cpp:110:15: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
  110 |     if (start == m_schemeEnd + 1
      |         ~~~~~~^~~~~~~~~~~~~~~~~~

[206/6600] Building CXX object Source/WTF/wtf/CMakeFiles/WTF.dir/URLParser.cpp.o
../../Source/WTF/wtf/URLParser.cpp: In member function ‘bool WTF::URLParser::needsNonSpecialDotSlash() const’:
../../Source/WTF/wtf/URLParser.cpp:2501:22: warning: comparison of integer expressions of different signedness: ‘unsigned int’ and ‘int’ [-Wsign-compare]
 2501 |         && pathStart == m_url.m_schemeEnd + 1
      |            ~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~

We could use unsigned literals:

diff --git a/Source/WTF/wtf/URL.cpp b/Source/WTF/wtf/URL.cpp
index ae0324da9fc3..cf2d6bf3538c 100644
--- a/Source/WTF/wtf/URL.cpp
+++ b/Source/WTF/wtf/URL.cpp
@@ -107,7 +107,7 @@ bool URL::hasSpecialScheme() const
 unsigned URL::pathStart() const
 {
     unsigned start = m_hostEnd + m_portLength;
-    if (start == m_schemeEnd + 1
+    if (start == m_schemeEnd + 1u
         && start + 1 < m_string.length()
         && m_string[start] == '/' && m_string[start + 1] == '.')
         start += 2;
diff --git a/Source/WTF/wtf/URLParser.cpp b/Source/WTF/wtf/URLParser.cpp
index 8bbb2c0149ca..22db53f93761 100644
--- a/Source/WTF/wtf/URLParser.cpp
+++ b/Source/WTF/wtf/URLParser.cpp
@@ -2498,7 +2498,7 @@ bool URLParser::needsNonSpecialDotSlash() const
 {
     auto pathStart = m_url.m_hostEnd + m_url.m_portLength;
     return !m_urlIsSpecial
-        && pathStart == m_url.m_schemeEnd + 1
+        && pathStart == m_url.m_schemeEnd + 1u
         && pathStart + 1 < m_url.m_string.length()
         && m_url.m_string[pathStart] == '/'
         && m_url.m_string[pathStart + 1] == '/';

Or we could use casts:

diff --git a/Source/WTF/wtf/URL.cpp b/Source/WTF/wtf/URL.cpp
index ae0324da9fc3..b94e99759fb2 100644
--- a/Source/WTF/wtf/URL.cpp
+++ b/Source/WTF/wtf/URL.cpp
@@ -107,7 +107,7 @@ bool URL::hasSpecialScheme() const
 unsigned URL::pathStart() const
 {
     unsigned start = m_hostEnd + m_portLength;
-    if (start == m_schemeEnd + 1
+    if (start == static_cast<unsigned>(m_schemeEnd + 1)
         && start + 1 < m_string.length()
         && m_string[start] == '/' && m_string[start + 1] == '.')
         start += 2;
diff --git a/Source/WTF/wtf/URLParser.cpp b/Source/WTF/wtf/URLParser.cpp
index 8bbb2c0149ca..be7ab18c4ca3 100644
--- a/Source/WTF/wtf/URLParser.cpp
+++ b/Source/WTF/wtf/URLParser.cpp
@@ -2498,7 +2498,7 @@ bool URLParser::needsNonSpecialDotSlash() const
 {
     auto pathStart = m_url.m_hostEnd + m_url.m_portLength;
     return !m_urlIsSpecial
-        && pathStart == m_url.m_schemeEnd + 1
+        && pathStart == static_cast<unsigned>(m_url.m_schemeEnd + 1)
         && pathStart + 1 < m_url.m_string.length()
         && m_url.m_string[pathStart] == '/'
         && m_url.m_string[pathStart + 1] == '/';

No strong preference from me, but I'll attach a patch that uses casts since I think it's slightly more clear.
Comment 1 Michael Catanzaro 2020-10-11 13:01:24 PDT
Created attachment 411060 [details]
Patch
Comment 2 Darin Adler 2020-10-11 19:33:46 PDT
Comment on attachment 411060 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=411060&action=review

> Source/WTF/wtf/URL.cpp:110
> +    if (start == static_cast<unsigned>(m_schemeEnd + 1)

Doesn’t make sense to me. Isn’t m_schemeEnd an unsigned already? Is there something about being a bitfield that makes things become signed?
Comment 3 Darin Adler 2020-10-11 19:34:45 PDT
Comment on attachment 411060 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=411060&action=review

>> Source/WTF/wtf/URL.cpp:110
>> +    if (start == static_cast<unsigned>(m_schemeEnd + 1)
> 
> Doesn’t make sense to me. Isn’t m_schemeEnd an unsigned already? Is there something about being a bitfield that makes things become signed?

Oh, you said that using "1" rather than "1U" makes it signed.

I prefer 1U over 1u and certainly over static_cast<unsigned>.
Comment 4 Michael Catanzaro 2020-10-12 07:39:46 PDT
1U it is, then
Comment 5 Michael Catanzaro 2020-10-12 07:40:24 PDT
Created attachment 411112 [details]
Patch
Comment 6 Darin Adler 2020-10-12 10:11:53 PDT
Comment on attachment 411112 [details]
Patch

Thanks.
Comment 7 EWS 2020-10-12 10:39:56 PDT
Committed r268362: <https://trac.webkit.org/changeset/268362>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 411112 [details].
Comment 8 Radar WebKit Bug Importer 2020-10-12 10:40:17 PDT
<rdar://problem/70213551>