<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic No warning for unsigned / signed underflow in if statement in Intel® oneAPI DPC++/C++ Compiler</title>
    <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1646269#M4207</link>
    <description>&lt;DIV&gt;I got a strange bug using std::vector::size()&lt;BR /&gt;underflow for mixing signed and unsigned,&amp;nbsp;&lt;BR /&gt;The comparison then fails.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I know it is my fault for mixing unsigned and signed.&lt;BR /&gt;But std::vector returns a&amp;nbsp;size_type,&amp;nbsp;which in turn is a template of something. I struggled finding the definition, but I assume it is an unsigned of something.&lt;BR /&gt;&lt;BR /&gt;if((size() - 9) &amp;gt;= 0)&amp;nbsp; &amp;nbsp;// underflow&lt;BR /&gt;(-1) &amp;gt;= 0&amp;nbsp; &amp;nbsp;// TRUE ?!&lt;BR /&gt;&lt;BR /&gt;When I wrote the code I didnt think about it.&lt;BR /&gt;If I store the result temporarily in a signed int it works fine.&lt;BR /&gt;&lt;BR /&gt;Below is a PoC&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;void No_Unisgned_Overflow_Warning() {&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; std::vector&amp;lt;char&amp;gt; v(8);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; int adjustIndex = 9;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; if ((v.size() - adjustIndex) &amp;gt;= 0) {&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; printf("First!\n");&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; int index = v.size() - adjustIndex;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; if (index &amp;gt;= 0) {&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; printf("Second!\n");&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;}&lt;BR /&gt;&lt;BR /&gt;The result of the subtraction is probably expanded to the unsigned datatype.&lt;BR /&gt;But then the comparison &amp;gt;= doesn't make sense.&lt;BR /&gt;It is always true&lt;BR /&gt;&lt;BR /&gt;The string "First" is printed but not "Second".&lt;BR /&gt;I think it would help if we get a warning about signed/unsigned mismatch in the comparison, or always true condition.&amp;nbsp;I am compiling with Intel DPC++ 2025 on Windows.&lt;/DIV&gt;</description>
    <pubDate>Thu, 28 Nov 2024 09:55:04 GMT</pubDate>
    <dc:creator>mhogstrom</dc:creator>
    <dc:date>2024-11-28T09:55:04Z</dc:date>
    <item>
      <title>No warning for unsigned / signed underflow in if statement</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1646269#M4207</link>
      <description>&lt;DIV&gt;I got a strange bug using std::vector::size()&lt;BR /&gt;underflow for mixing signed and unsigned,&amp;nbsp;&lt;BR /&gt;The comparison then fails.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I know it is my fault for mixing unsigned and signed.&lt;BR /&gt;But std::vector returns a&amp;nbsp;size_type,&amp;nbsp;which in turn is a template of something. I struggled finding the definition, but I assume it is an unsigned of something.&lt;BR /&gt;&lt;BR /&gt;if((size() - 9) &amp;gt;= 0)&amp;nbsp; &amp;nbsp;// underflow&lt;BR /&gt;(-1) &amp;gt;= 0&amp;nbsp; &amp;nbsp;// TRUE ?!&lt;BR /&gt;&lt;BR /&gt;When I wrote the code I didnt think about it.&lt;BR /&gt;If I store the result temporarily in a signed int it works fine.&lt;BR /&gt;&lt;BR /&gt;Below is a PoC&lt;BR /&gt;&lt;BR /&gt;&lt;/DIV&gt;&lt;DIV&gt;void No_Unisgned_Overflow_Warning() {&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; std::vector&amp;lt;char&amp;gt; v(8);&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; int adjustIndex = 9;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; if ((v.size() - adjustIndex) &amp;gt;= 0) {&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; printf("First!\n");&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; int index = v.size() - adjustIndex;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; if (index &amp;gt;= 0) {&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; printf("Second!\n");&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; }&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;}&lt;BR /&gt;&lt;BR /&gt;The result of the subtraction is probably expanded to the unsigned datatype.&lt;BR /&gt;But then the comparison &amp;gt;= doesn't make sense.&lt;BR /&gt;It is always true&lt;BR /&gt;&lt;BR /&gt;The string "First" is printed but not "Second".&lt;BR /&gt;I think it would help if we get a warning about signed/unsigned mismatch in the comparison, or always true condition.&amp;nbsp;I am compiling with Intel DPC++ 2025 on Windows.&lt;/DIV&gt;</description>
      <pubDate>Thu, 28 Nov 2024 09:55:04 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1646269#M4207</guid>
      <dc:creator>mhogstrom</dc:creator>
      <dc:date>2024-11-28T09:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: No warning for unsigned / signed underflow in if statement</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647211#M4212</link>
      <description>&lt;P&gt;$icpx t12.cpp &amp;amp;&amp;amp;./a.out&lt;BR /&gt;First!&lt;/P&gt;
&lt;P&gt;$ icpx t12.cpp -Wsign-conversion&lt;BR /&gt;t12.cpp:8:19: warning: implicit conversion changes signedness: 'int' to 'size_type' (aka 'unsigned long') [-Wsign-conversion]&lt;BR /&gt;8 | if ((v.size() - adjustIndex) &amp;gt;= 0) {&lt;BR /&gt;| ~ ^~~~~~~~~~~&lt;BR /&gt;t12.cpp:11:26: warning: implicit conversion changes signedness: 'int' to 'size_type' (aka 'unsigned long') [-Wsign-conversion]&lt;BR /&gt;11 | int index = v.size() - adjustIndex;&lt;BR /&gt;| ~ ^~~~~~~~~~~&lt;BR /&gt;2 warnings generated.&lt;/P&gt;
&lt;P&gt;$ icpx t12.cpp -Wtautological-unsigned-zero-compare&lt;BR /&gt;t12.cpp:8:32: warning: result of comparison of unsigned expression &amp;gt;= 0 is always true [-Wtautological-unsigned-zero-compare]&lt;BR /&gt;8 | if ((v.size() - adjustIndex) &amp;gt;= 0) {&lt;BR /&gt;| ~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~&lt;BR /&gt;1 warning generated.&lt;BR /&gt;&lt;BR /&gt;$ cat t12.cpp&lt;BR /&gt;#include &amp;lt;iostream&amp;gt;&lt;BR /&gt;#include &amp;lt;vector&amp;gt;&lt;BR /&gt;using namespace std;&lt;BR /&gt;int main()&lt;BR /&gt;{&lt;BR /&gt;std::vector&amp;lt;char&amp;gt; v(8);&lt;BR /&gt;int adjustIndex = 9;&lt;BR /&gt;if ((v.size() - adjustIndex) &amp;gt;= 0) {&lt;BR /&gt;printf("First!\n");&lt;BR /&gt;}&lt;BR /&gt;int index = v.size() - adjustIndex;&lt;BR /&gt;if (index &amp;gt;= 0) {&lt;BR /&gt;printf("Second!\n");&lt;BR /&gt;}&lt;BR /&gt;return 0;&lt;BR /&gt;}&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 01:07:08 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647211#M4212</guid>
      <dc:creator>Viet_H_Intel</dc:creator>
      <dc:date>2024-12-04T01:07:08Z</dc:date>
    </item>
    <item>
      <title>Re: No warning for unsigned / signed underflow in if statement</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647331#M4215</link>
      <description>&lt;P&gt;We are not using the same compiler or platform. a.out is un*x filename&lt;BR /&gt;I am using Visual Studio 2022, DPC++, under Windows.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mhogstrom_0-1733312721105.png" style="width: 400px;"&gt;&lt;img src="https://community.intel.com/t5/image/serverpage/image-id/60800iF21447C4EE13CEA0/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400&amp;amp;whitelist-exif-data=Orientation%2CResolution%2COriginalDefaultFinalSize%2CCopyright" role="button" title="mhogstrom_0-1733312721105.png" alt="mhogstrom_0-1733312721105.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mhogstrom_1-1733312806829.png" style="width: 400px;"&gt;&lt;img src="https://community.intel.com/t5/image/serverpage/image-id/60801iF5DC1ED379EFD14F/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400&amp;amp;whitelist-exif-data=Orientation%2CResolution%2COriginalDefaultFinalSize%2CCopyright" role="button" title="mhogstrom_1-1733312806829.png" alt="mhogstrom_1-1733312806829.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Compiler is set to use Warning level 3 -W3,&amp;nbsp;-Wall doesn't detect it either&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 11:52:02 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647331#M4215</guid>
      <dc:creator>mhogstrom</dc:creator>
      <dc:date>2024-12-04T11:52:02Z</dc:date>
    </item>
    <item>
      <title>Re: No warning for unsigned / signed underflow in if statement</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647346#M4216</link>
      <description>&lt;P&gt;Can you try -Weverything? or -&lt;SPAN&gt;Wsign-conversion&amp;nbsp;-Wtautological-unsigned-zero-compare in VS&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Dec 2024 15:18:32 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647346#M4216</guid>
      <dc:creator>Viet_H_Intel</dc:creator>
      <dc:date>2024-12-04T15:18:32Z</dc:date>
    </item>
    <item>
      <title>Re: No warning for unsigned / signed underflow in if statement</title>
      <link>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647506#M4217</link>
      <description>&lt;P&gt;&lt;BR /&gt;Now we are talking&lt;BR /&gt;&lt;BR /&gt;It seems the compiler it is using is clang to compile.&lt;BR /&gt;"C:\\Program Files (x86)\\Intel\\oneAPI\\compiler\\2025.0\\bin\\compiler\\clang-cl.exe"&lt;BR /&gt;If I add "-Wno-error -Wsign-conversion -Wtautological-unsigned-zero-compare"&lt;BR /&gt;or -Weverything the warning rightfully appears.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="mhogstrom_1-1733391620568.png" style="width: 400px;"&gt;&lt;img src="https://community.intel.com/t5/image/serverpage/image-id/60816iD909B4A73CB703AA/image-size/medium/is-moderation-mode/true?v=v2&amp;amp;px=400&amp;amp;whitelist-exif-data=Orientation%2CResolution%2COriginalDefaultFinalSize%2CCopyright" role="button" title="mhogstrom_1-1733391620568.png" alt="mhogstrom_1-1733391620568.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;It seems that Visual Studio passes "-Wall" as option to clang too on the command line.&lt;BR /&gt;"-Wall" is usually enough for most development.&lt;BR /&gt;&lt;BR /&gt;Reading&amp;nbsp;&lt;A href="https://clang.llvm.org/docs/UsersManual.html#options-to-control-error-and-warning-messages" target="_blank" rel="noopener"&gt;https://clang.llvm.org/docs/UsersManual.html#options-to-control-error-and-warning-messages&lt;/A&gt;&lt;BR /&gt;&lt;SPAN&gt;"Since -Weverything&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;enables every diagnostic, we generally don’t recommend using it.&amp;nbsp;&lt;/SPAN&gt;-Wall&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;-Wextra&lt;SPAN&gt;&amp;nbsp;are a better choice for most projects.&amp;nbsp;"&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;Adding "-Wextra" also shows the warnings, but it seems that "-Wall" doesn't catch the error.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 10:11:21 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-oneAPI-DPC-C-Compiler/No-warning-for-unsigned-signed-underflow-in-if-statement/m-p/1647506#M4217</guid>
      <dc:creator>mhogstrom</dc:creator>
      <dc:date>2024-12-05T10:11:21Z</dc:date>
    </item>
  </channel>
</rss>

