public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libquadmath/114533] libquadmath: printf: fix misaligned access on args
Date: Sun, 21 Apr 2024 04:08:43 +0000	[thread overview]
Message-ID: <bug-114533-4-CG9suQ9P3s@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114533-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #13 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Jakub Jelinek
<jakub@gcc.gnu.org>:

https://gcc.gnu.org/g:cc39bd532d4de1ba0b2785246fb6fdd63ec2e92c

commit r13-8625-gcc39bd532d4de1ba0b2785246fb6fdd63ec2e92c
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Apr 3 10:02:35 2024 +0200

    libquadmath: Don't assume the storage for __float128 arguments is aligned
[PR114533]

    With the
register_printf_type/register_printf_modifier/register_printf_specifier
    APIs the C library is just told the size of the argument and is provided
with
    a callback to fetch the argument from va_list using va_arg into C library
provided
    memory.  The C library isn't told what alignment requirement it has, but we
were
    using direct load of a __float128 value from that memory which assumes
    __alignof (__float128) alignment.

    The following patch fixes that by using memcpy instead.

    I haven't been able to reproduce an actual crash, tried
     #include <quadmath.h>
     #include <stdlib.h>
     #include <stdio.h>

    int main ()
    {
      __float128 r;
      int prec = 20;
      int width = 46;
      char buf[128];

      r = 2.0q;
      r = sqrtq (r);
      int n = quadmath_snprintf (buf, sizeof buf, "%+-#*.20Qe", width, r);
      if ((size_t) n < sizeof buf)
        printf ("%s\n", buf);
        /* Prints: +1.41421356237309504880e+00 */
      quadmath_snprintf (buf, sizeof buf, "%Qa", r);
      if ((size_t) n < sizeof buf)
        printf ("%s\n", buf);
        /* Prints: 0x1.6a09e667f3bcc908b2fb1366ea96p+0 */
      n = quadmath_snprintf (NULL, 0, "%+-#46.*Qe", prec, r);
      if (n > -1)
        {
          char *str = malloc (n + 1);
          if (str)
            {
              quadmath_snprintf (str, n + 1, "%+-#46.*Qe", prec, r);
              printf ("%s\n", str);
              /* Prints: +1.41421356237309504880e+00 */
            }
          free (str);
        }
      printf ("%+-#*.20Qe\n", width, r);
      printf ("%Qa\n", r);
      printf ("%+-#46.*Qe\n", prec, r);
      printf ("%d %Qe %d %Qe %d %Qe\n", 1, r, 2, r, 3, r);
      return 0;
    }
    In any case, I think memcpy for loading from it is right.

    2024-04-03  Simon Chopin  <simon.chopin@canonical.com>
                Jakub Jelinek  <jakub@redhat.com>

            PR libquadmath/114533
            * printf/printf_fp.c (__quadmath_printf_fp): Use memcpy to copy
            __float128 out of args.
            * printf/printf_fphex.c (__quadmath_printf_fphex): Likewise.

    Signed-off-by: Simon Chopin <simon.chopin@canonical.com>
    (cherry picked from commit 8455d6f6cd43b7b143ab9ee19437452fceba9cc9)

  parent reply	other threads:[~2024-04-21  4:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-30 12:48 [Bug libquadmath/114533] New: " doko at gcc dot gnu.org
2024-04-02 11:29 ` [Bug libquadmath/114533] " rguenth at gcc dot gnu.org
2024-04-02 11:45 ` jakub at gcc dot gnu.org
2024-04-02 12:04 ` schwab@linux-m68k.org
2024-04-02 12:36 ` fw at gcc dot gnu.org
2024-04-02 13:21 ` schwab@linux-m68k.org
2024-04-02 13:41 ` schwab@linux-m68k.org
2024-04-02 17:02 ` jsm28 at gcc dot gnu.org
2024-04-02 17:08 ` jakub at gcc dot gnu.org
2024-04-02 17:30 ` jvdelisle at gcc dot gnu.org
2024-04-02 17:34 ` jakub at gcc dot gnu.org
2024-04-02 18:26 ` doko at gcc dot gnu.org
2024-04-03  8:14 ` cvs-commit at gcc dot gnu.org
2024-04-21  4:08 ` cvs-commit at gcc dot gnu.org [this message]
2024-04-23  6:45 ` jakub 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-114533-4-CG9suQ9P3s@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).