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

            Bug ID: 106333
           Summary: Required condition omitted from generated code
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: eran.kornblau at kaltura dot com
  Target Milestone: ---

Hi,

We bumped into a case where a required condition gets omitted from our code, we
validated the issue on versions 11.1.0, 11.2.0, 11.3.0, 12.1.0. 
Version 10.3.0 is working as expected.

The issue happens only when compiling with optimizations (I tested -O2), when
running without any -Oxxx flag, it works as expected.

The issue can be reproduced with these commands -
$ docker run -it gcc:12.1.0 /bin/sh

# cat > /tmp/x.c
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>

#define NGX_LIVE_INVALID_PTS                 (LLONG_MAX)

typedef struct {
    int64_t                 created;
    int64_t                 dts;
    uint32_t                flags;
    int32_t                 pts_delay;
} kmp_frame_t;

static int test(kmp_frame_t *frame)
{
    if (frame->dts >= NGX_LIVE_INVALID_PTS - frame->pts_delay &&
frame->pts_delay >= 0) {
        printf("bad!\n");

    } else {
        printf("good!\n");
    }
}

int main()
{
    kmp_frame_t  frame;

    memset(&frame, 0xff, sizeof(frame));
    frame.dts = 149201707622903;

    test(&frame);

    return 0;
}
^C
# gcc -O2 /tmp/x.c -o /tmp/x ; /tmp/x
bad!
# gcc /tmp/x.c -o /tmp/x ; /tmp/x
good!
#

Following the memset with 0xff, the value of frame.pts_delay is -1. Therefore,
the second part of the if condition (frame->pts_delay >= 0) is expected to be
false (the field is a signed int32_t), so we should get the string 'good!'
printed.
However, it seems the second part of the condition (following the &&) gets
optimized out, and we get 'bad!' printed out.

Thank you!

Eran

Reply via email to