public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "baiwfg2 at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/110399] New: pointer substraction causes coredump with ftrapv on edge case
Date: Sun, 25 Jun 2023 01:49:33 +0000	[thread overview]
Message-ID: <bug-110399-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 110399
           Summary: pointer substraction causes coredump with ftrapv on
                    edge case
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: baiwfg2 at gmail dot com
  Target Milestone: ---

The demo code is :

```c
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <assert.h>

int main() {
    {
        char *p = (char *)0x80000001;
        char *q = (char *)0x7fffffff;
        uint32_t w = p - q;
        printf("32 bit, w1=%u\n", w);
    }

    {
        char *p = (char *)0x7fffffffffffffff;
        char *q = (char *)0x7ffffffffffffffd;
        uint32_t w2 = p - q;
        printf("w2=%u\n", w2);
    }

    {
        char *p = (char *)0x8000000000000003;
        char *q = (char *)0x8000000000000001;
        uint32_t w3 = p - q;
        printf("w3=%u\n", w3);
    }

    {
        char *p = (char *)0x8000000000000001;
        char *q = (char *)0x0000000000000001;
        uint32_t w4 = p - q;
        printf("w4=%u\n", w4); // ans is 0, not crash under -ftrapv
    }

    {
        char *p = (char *)0x8000000000000001;
        char *q = (char *)0x7fffffffffffffff;
        uint32_t w5 = (uintptr_t)p - (uintptr_t)q;
        printf("w5=%u\n", w5);
    }

    {
        char *p = (char *)0x8000000000000001; // use uint8_t also crash
        char *q = (char *)0x7fffffffffffffff; // use smaller num
0x0000000000000011, also crash
        uint32_t w6 = p - q;
        printf("w6=%u\n", w6); // crash under gcc -ftrapv, not crash under
clang -ftrapv
    }

    return 0;
}
```

The statement w6 = p - q cause coredump. But what program actually means do
pointer unsigned arithmetic operation. How can I make it right(that is, output
2) with ftrapv option ? I find it's ok with clang -ftrapv .

This happens on many gcc versions.

             reply	other threads:[~2023-06-25  1:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-25  1:49 baiwfg2 at gmail dot com [this message]
2023-06-25  1:59 ` [Bug middle-end/110399] " pinskia at gcc dot gnu.org
2023-06-25  2:01 ` pinskia at gcc dot gnu.org
2023-06-29  9:33 ` baiwfg2 at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110399-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).