From: Pedro Alves <pedro@palves.net>
To: gdb-patches@sourceware.org
Subject: Re: [PATCH 1/2] gdb: Avoid undefined shifts
Date: Mon, 4 Apr 2022 13:16:57 +0100 [thread overview]
Message-ID: <285aa468-ee11-b8d5-45f2-60c949a56d38@palves.net> (raw)
In-Reply-To: <20220401202251.423122-2-pedro@palves.net>
Reading this back, I noticed a few typos, pointed out below. I'll send a v2 shortly
with these fixed...
On 2022-04-01 21:22, Pedro Alves wrote:
> - Make GDB warn for the cases that are still undefined in C++20. The
> warnings' texts are same as what GCC prints.
"are the same"
> +# Test a print command that prints out RESULT_RE. If WARNING is
> +# non-empty, it is expected that GDB prints this warning before the
> +# print result. If WARNING is empty, it is expected that GDB prints
> +# text other than the print result.
"that GDB prints text other" -> "that GDB prints no text other"
> +proc test_shift {cmd result_re {warning ""}} {
> + if {$warning != ""} {
> + set warning_re "[string_to_regexp $warning]\r\n"
> + } else {
> + set warning_re ""
> + }
> +
> + set cmd_re [string_to_regexp $cmd]
> +
> + gdb_test_multiple $cmd "" {
> + -re -wrap "^$cmd_re\r\n$warning_re\\$$::decimal$result_re" {
> + pass $gdb_test_name
> + }
> + }
> +}
> +
> +# Test a print command that issues a warning about the right shift
> +# count being negative. The "ne" suffix stands for negative.
> +proc test_shift_ne {cmd result} {
> + test_shift $cmd $result "warning: right shift count is negative"
> +}
> +
> +# Test a print command that issues a warning about the right shift
> +# count being too large. The "lt" suffix stands for "too large".
Typo "lt" -> "tl"...
> +proc test_shift_tl {cmd result} {
> + test_shift $cmd $result "warning: right shift count >= width of type"
> +}
Meanwhile I realized it's simpler to store the warning texts in variables and
use those, instead of these wrapper procedures.
> +/* Check whether the the RHS value of a shift is valid. RHS_VAL is
Double "the the".
> + the shift amount, and RESULT_TYPE is result type. This is used to
"is the result"
> + avoid both negative or too-large shift amounts, which are
or -> and
> + undefined, and would crash a GDB built with UBSan. Warns and
> + returns false if not valid, and returns true if not valid. */
> +
> case BINOP_LSH:
> - v = v1 << v2;
> + if (!check_valid_shift_rhs (result_type, v2))
> + v = 0;
> + else
> + {
> + /* Cast to unsigned to avoid undefined behavior on
> + signed shift overflow (unless C++20 or later),
> + which would crash GDB when built with UBSan.
> + Note We don't warn on left signed shift overflow,
"Note We" -> "Note we"
> + because starting with C++20, that is actually
> + defined behavior. */
> + v = (ULONGEST) v1 << v2;
> + }
> break;
>
> case BINOP_RSH:
> - v = v1 >> v2;
> + if (!check_valid_shift_rhs (result_type, v2))
> + v = 0;
> + else
> + v = v1 >> v2;
> break;
>
> case BINOP_BITWISE_AND:
next prev parent reply other threads:[~2022-04-04 12:17 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-01 20:22 [PATCH 0/2] Fix a couple undefined behaviors flagged by UBSan Pedro Alves
2022-04-01 20:22 ` [PATCH 1/2] gdb: Avoid undefined shifts Pedro Alves
2022-04-04 12:16 ` Pedro Alves [this message]
2022-04-01 20:22 ` [PATCH 2/2] Avoid undefined behavior in gdbscm_make_breakpoint Pedro Alves
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=285aa468-ee11-b8d5-45f2-60c949a56d38@palves.net \
--to=pedro@palves.net \
--cc=gdb-patches@sourceware.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).