public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jiangchuanganghw at outlook dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug ipa/112783] core dump on libxo when function is inlined
Date: Fri, 05 Jan 2024 03:09:06 +0000	[thread overview]
Message-ID: <bug-112783-4-onewEgqHB9@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-112783-4@http.gcc.gnu.org/bugzilla/>

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

Jiang ChuanGang <jiangchuanganghw at outlook dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jiangchuanganghw at outlook dot co
                   |                            |m

--- Comment #9 from Jiang ChuanGang <jiangchuanganghw at outlook dot com> ---
(In reply to Andrew Pinski from comment #3)
> The problem is not in GCC but rather a bad assumption on the code part.
> 
> Basically we have:
> 
> memcpy(nbuf, name, nlen);
> ...
> 
> 
> if (!name)
> ...
> 
> 
> But the check after a memcpy can be removed as passing a null pointer to
> memcpy is undefined even if nlen is 0 here.
> 
> 
> This patch to the sources fixes the issue for me:
> ```
> diff --git a/libxo/libxo.c b/libxo/libxo.c
> index 916a111..ea71723 100644
> --- a/libxo/libxo.c
> +++ b/libxo/libxo.c
> @@ -4300,7 +4300,8 @@ xo_format_value (xo_handle_t *xop, const char *name,
> ssize_t nlen,
>         if ((xsp->xs_flags & (XSF_EMIT | XSF_EMIT_KEY))
>             || !(xsp->xs_flags & XSF_EMIT_LEAF_LIST)) {
>             char nbuf[nlen + 1];
> -           memcpy(nbuf, name, nlen);
> +            if (name)
> +             memcpy(nbuf, name, nlen);
>             nbuf[nlen] = '\0';
> 
>             ssize_t rc = xo_transition(xop, 0, nbuf, XSS_EMIT_LEAF_LIST);
> @@ -4324,7 +4325,8 @@ xo_format_value (xo_handle_t *xop, const char *name,
> ssize_t nlen,
> 
>         } else if (!(xsp->xs_flags & XSF_EMIT_KEY)) {
>             char nbuf[nlen + 1];
> -           memcpy(nbuf, name, nlen);
> +            if (name)
> +             memcpy(nbuf, name, nlen);
>             nbuf[nlen] = '\0';
> 
>             ssize_t rc = xo_transition(xop, 0, nbuf, XSS_EMIT);
> @@ -4342,7 +4344,8 @@ xo_format_value (xo_handle_t *xop, const char *name,
> ssize_t nlen,
>         if ((xsp->xs_flags & XSF_EMIT_LEAF_LIST)
>             || !(xsp->xs_flags & XSF_EMIT)) {
>             char nbuf[nlen + 1];
> -           memcpy(nbuf, name, nlen);
> +            if (name)
> +             memcpy(nbuf, name, nlen);
>             nbuf[nlen] = '\0';
> 
>             ssize_t rc = xo_transition(xop, 0, nbuf, XSS_EMIT);
> 
> ```

I've encountered the same bug, and your solution does fix it.
But strangely enough, I can't reproduce it with code like the following.
The inevitable condition of this bug still puzzles me. Do you have any thoughts
on this.


#include <stdio.h>
#include <string.h>

static const char *xo_xml_leader_len(const char *name, int len)
{
    if (name == NULL || name[0] == '_')
            return "";
        return "_";
}

void test()
{
    char *name = NULL;
        int len = 0;
        char mbuf[len + 1];
        memcpy(mbuf, name, len);
        mbuf[len] = '\0';
        const char *leader = xo_xml_leader_len(name, len);
        printf("leader: %s", leader);
}

int main()
{
    test();
        return 0;
}

  parent reply	other threads:[~2024-01-05  3:09 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-30 13:32 [Bug ipa/112783] New: " yancheng.li at foxmail dot com
2023-12-01  5:06 ` [Bug ipa/112783] " sjames at gcc dot gnu.org
2023-12-01 23:33 ` pinskia at gcc dot gnu.org
2023-12-01 23:53 ` pinskia at gcc dot gnu.org
2023-12-12  3:40 ` yancheng.li at foxmail dot com
2023-12-12  3:45 ` pinskia at gcc dot gnu.org
2023-12-12  3:45 ` pinskia at gcc dot gnu.org
2023-12-15 21:09 ` cvs-commit at gcc dot gnu.org
2023-12-15 21:15 ` jvdelisle at gcc dot gnu.org
2024-01-05  3:09 ` jiangchuanganghw at outlook dot com [this message]
2024-01-05 14:38 ` xry111 at gcc dot gnu.org

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-112783-4-onewEgqHB9@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).