From patchwork Wed Nov 16 21:00:11 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 60718 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EBD23398641E for ; Wed, 16 Nov 2022 21:01:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EBD23398641E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1668632489; bh=65sr2flo3Dx+DtgjorCLgH1Zso5AeCG6ThOaJFLFEiE=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=fB3gV9GTmUQQtbF1VRekQPm4v9IrxtpGHQ+icWoahp8Vrxvnoj7iWuL8nwb0ROFJ2 KL986CT9tgzH7r2mK0G4INgCb1ApOFaldZE6xbsgNF/BqOx0rUBG8n0WGgZDHwMYHV qDUx6J5wihfL3+WU1GCqEKYiXejUNB4lj5nLBKAw= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-il1-x12c.google.com (mail-il1-x12c.google.com [IPv6:2607:f8b0:4864:20::12c]) by sourceware.org (Postfix) with ESMTPS id 79730396E468 for ; Wed, 16 Nov 2022 21:00:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 79730396E468 Received: by mail-il1-x12c.google.com with SMTP id 7so9793210ilg.11 for ; Wed, 16 Nov 2022 13:00:21 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=65sr2flo3Dx+DtgjorCLgH1Zso5AeCG6ThOaJFLFEiE=; b=a5Z6aHjH/mf6OPCDS8ycTuT3pR4V5SjV2mz6AEL938VfVwJCl8hVA+EwNUiXZ8syuZ zzvJHY+V0J25wxP0QzEWWJuAGsS/4WdEdWJ8YCJ4AdcOIcZp4siAuo9cpQUGZ1AJqVmw Kv+7pEmAJ+fWz2sL/f/k5b+2eupkpFuvHCuAb1i+K25KyUVlvtJnp7qjgNOQOJ4MuhyM qOPO1FX6cDxBWCbmvOXblLzKosInz+yIoUigUQz/uNPpDTD+mXfwoDJrniDAVqlTr7Ir 3/ToH04iSnb0+Ks4yhv3XJAS0mE9IAqaBbklBIJ6tsQEdNJ602b2ZVd3sewrC4i2UyPf JPCA== X-Gm-Message-State: ANoB5pmXQABPrvKAy4axSSmKOHYjhuKL79mETt0LARfZ39QMSFAfIhY3 ia5uXoR26QJySVZ0vJkhwancMyghGsdZJQ== X-Google-Smtp-Source: AA0mqf7AiEjNDAItLJaqqf+PhWemS4MhhHhuHTCydvKd3KWj+Bfs9Al09gI+ZZKEfquQ8MifOezLoA== X-Received: by 2002:a05:6e02:809:b0:302:4ad3:fd49 with SMTP id u9-20020a056e02080900b003024ad3fd49mr10166384ilm.271.1668632420738; Wed, 16 Nov 2022 13:00:20 -0800 (PST) Received: from localhost.localdomain (97-122-76-186.hlrn.qwest.net. [97.122.76.186]) by smtp.gmail.com with ESMTPSA id v7-20020a92d247000000b003024a44cd95sm5415519ilg.34.2022.11.16.13.00.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Nov 2022 13:00:20 -0800 (PST) To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Bounds check access to Ada task state names Date: Wed, 16 Nov 2022 14:00:11 -0700 Message-Id: <20221116210011.198094-1-tromey@adacore.com> X-Mailer: git-send-email 2.34.3 MIME-Version: 1.0 X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Tom Tromey via Gdb-patches From: Tom Tromey Reply-To: Tom Tromey Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" While looking into Ada tasking a little, I noticed that no bounds checking is done on accesses to the Ada task state names arrays. This isn't a problem currently, but if the runtime ever added numbers -- or if there was some kind of runtime corruption -- it could cause a gdb crash. This patch adds range checking. It also adds a missing _() call when printing from the 'task_states' array. --- gdb/ada-tasks.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/gdb/ada-tasks.c b/gdb/ada-tasks.c index 85d1aaccb06..fbf92041d94 100644 --- a/gdb/ada-tasks.c +++ b/gdb/ada-tasks.c @@ -85,6 +85,20 @@ static const char * const task_states[] = { N_("Selective Wait") }; +/* Return a string representing the task state. */ +static const char * +get_state (unsigned value) +{ + if (value >= 0 + && value <= ARRAY_SIZE (task_states) + && task_states[value][0] != '\0') + return _(task_states[value]); + + static char buffer[100]; + xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value); + return buffer; +} + /* A longer description corresponding to each possible task state. */ static const char * const long_task_states[] = { N_("Unactivated"), @@ -107,6 +121,21 @@ static const char * const long_task_states[] = { N_("Blocked in selective wait statement") }; +/* Return a string representing the task state. This uses the long + descriptions. */ +static const char * +get_long_state (unsigned value) +{ + if (value >= 0 + && value <= ARRAY_SIZE (long_task_states) + && long_task_states[value][0] != '\0') + return _(long_task_states[value]); + + static char buffer[100]; + xsnprintf (buffer, sizeof (buffer), _("Unknown task state: %d"), value); + return buffer; +} + /* The index of certain important fields in the Ada Task Control Block record and sub-records. */ @@ -1182,7 +1211,7 @@ print_ada_task_info (struct ui_out *uiout, get_task_number_from_id (task_info->called_task, inf)); else - uiout->field_string ("state", task_states[task_info->state]); + uiout->field_string ("state", get_state (task_info->state)); /* Finally, print the task name, without quotes around it, as mi like is not expecting quotes, and in non mi-like no need for quotes @@ -1276,7 +1305,7 @@ info_task (struct ui_out *uiout, const char *taskno_str, struct inferior *inf) target_taskno); } else - gdb_printf (_("State: %s"), _(long_task_states[task_info->state])); + gdb_printf (_("State: %s"), get_long_state (task_info->state)); if (target_taskno) {