From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1551) id EFD023858D37; Mon, 4 Apr 2022 19:50:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EFD023858D37 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Pedro Alves To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Avoid undefined behavior in gdbscm_make_breakpoint X-Act-Checkin: binutils-gdb X-Git-Author: Pedro Alves X-Git-Refname: refs/heads/master X-Git-Oldrev: d4da1b2c1b7b85968da608dde03e054cc0b1f7ca X-Git-Newrev: 4994e74b7abae108000cfab8fa621473f2b11cff Message-Id: <20220404195017.EFD023858D37@sourceware.org> Date: Mon, 4 Apr 2022 19:50:17 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Apr 2022 19:50:18 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D4994e74b7aba= e108000cfab8fa621473f2b11cff commit 4994e74b7abae108000cfab8fa621473f2b11cff Author: Pedro Alves Date: Fri Apr 1 14:31:18 2022 +0100 Avoid undefined behavior in gdbscm_make_breakpoint =20 Running gdb.guile/scm-breakpoint.exp against an --enable-ubsan build, we see: =20 UNRESOLVED: gdb.guile/scm-breakpoint.exp: test_watchpoints: create a b= reakpoint with an invalid type number ... guile (define wp2 (make-breakpoint "result" #:wp-class WP_WRITE #:type= 999)) ../../src/gdb/guile/scm-breakpoint.c:377:11: runtime error: load of va= lue 999, which is not a valid value for type 'bptype' ERROR: GDB process no longer exists =20 Fix this by parsing the user/guile input as plain int, and cast to internal type only after we know we have a number that would be valid. =20 Change-Id: I03578d07db00be01b610a8f5ce72e5521aea6a4b Diff: --- gdb/guile/scm-breakpoint.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/gdb/guile/scm-breakpoint.c b/gdb/guile/scm-breakpoint.c index 0069d3371ff..d6c89aa8c71 100644 --- a/gdb/guile/scm-breakpoint.c +++ b/gdb/guile/scm-breakpoint.c @@ -353,8 +353,8 @@ gdbscm_make_breakpoint (SCM location_scm, SCM rest) char *location; int type_arg_pos =3D -1, access_type_arg_pos =3D -1, internal_arg_pos =3D -1, temporary_arg_pos =3D -1; - enum bptype type =3D bp_breakpoint; - enum target_hw_bp_type access_type =3D hw_write; + int type =3D bp_breakpoint; + int access_type =3D hw_write; int internal =3D 0; int temporary =3D 0; SCM result; @@ -403,7 +403,7 @@ gdbscm_make_breakpoint (SCM location_scm, SCM rest) case bp_access_watchpoint: case bp_catchpoint: { - const char *type_name =3D bpscm_type_to_string (type); + const char *type_name =3D bpscm_type_to_string ((enum bptype) type); gdbscm_misc_error (FUNC_NAME, type_arg_pos, gdbscm_scm_from_c_string (type_name), _("unsupported breakpoint type")); @@ -417,8 +417,8 @@ gdbscm_make_breakpoint (SCM location_scm, SCM rest) =20 bp_smob->is_scheme_bkpt =3D 1; bp_smob->spec.location =3D location; - bp_smob->spec.type =3D type; - bp_smob->spec.access_type =3D access_type; + bp_smob->spec.type =3D (enum bptype) type; + bp_smob->spec.access_type =3D (enum target_hw_bp_type) access_type; bp_smob->spec.is_internal =3D internal; bp_smob->spec.is_temporary =3D temporary;