https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116356

            Bug ID: 116356
           Summary: signed overflow warning has misplaced line number
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rsaxvc at gmail dot com
  Target Milestone: ---

The following snippet is from a now deleted stack-overflow question. When
compiled with G++ 14.1 on a 64-bit target, "g++ main.cpp -o test
-Wstrict-overflow=5 -Werror -O1" warns:

<source>: In function 'int main()':
<source>:39:1: warning: assuming signed overflow does not occur when changing X
+- C1 cmp C2 to X cmp C2 -+ C1 [-Wstrict-overflow]
   39 | }
      | ^

Where '39' is the line number of the closing of the last scope in the file,
rather than the line number of the signed overflow. In the original linked code
from stack-overflow, it's line 56. I've trimmed the excerpt below.

Impacts at least versions 12.2.0 to 14.2.


###BEGIN ORIGINAL EXCERPT FROM https://godbolt.org/z/W5er718fK ###

#include <ctime>
#include <iostream>

double operator-(const timespec& lhs, const timespec& rhs);

int main(){
    struct timespec start_time{}, end_time{}, curr_time{};
    clock_gettime(CLOCK_REALTIME, &start_time);
    end_time.tv_sec = start_time.tv_sec + 10;   // Run for 10 seconds
    end_time.tv_nsec = 0;
    curr_time = start_time;

    while(curr_time <= end_time){
        std::cout << curr_time - start_time << std::endl;
        clock_gettime(CLOCK_REALTIME, &curr_time);
        curr_time.tv_nsec += 1000000ll * 250; // Every 250 ms sample the sensor
        if(curr_time.tv_nsec > 1000000000ll){
            curr_time.tv_nsec -= 1000000000ll;
            curr_time.tv_sec += 1;
        }
        clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &curr_time, nullptr);
    }
    return 0;
}

double operator-(const timespec& lhs, const timespec& rhs)
{
    const auto diff_seconds = (static_cast<double>(lhs.tv_sec) -
static_cast<double>(rhs.tv_sec));
    const auto diff_nseconds = (static_cast<double>(lhs.tv_nsec) -
static_cast<double>(rhs.tv_nsec));

    const auto diff_sec_to_ms = diff_seconds * 1000.0;
    const auto diff_nsec_to_ms = diff_nseconds / 1000000.0;

    return diff_sec_to_ms + diff_nsec_to_ms;
}

Reply via email to