From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id ECHcGj1FlGFVPAAAWB0awg (envelope-from ) for ; Tue, 16 Nov 2021 18:56:45 -0500 Received: by simark.ca (Postfix, from userid 112) id 6B7821F0C1; Tue, 16 Nov 2021 18:56:45 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI,RDNS_DYNAMIC,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id 03F601E940 for ; Tue, 16 Nov 2021 18:56:45 -0500 (EST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3EBD53858410 for ; Tue, 16 Nov 2021 23:56:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3EBD53858410 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1637107004; bh=nopeDWSF9if0dgtaHD6KLLl1bh1ed/y9jnO7zYE8XQs=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=JYiCZGZGVPTtG1GI6I8oczrXPSvvexDZm2RicQDsSoDsFWB0J0/WZYJZSQ8Q/yE+L m379Y+eLZG+BFkEk/FpnIc0WYE375xMQIQKS4RbbGI2yLCvhr6yprzuN8JuARbvC7c FsBgy+5yME8d5NhLNajHKYMUe/00dVyj3PuJZ6Hw= Received: from mail-wr1-x433.google.com (mail-wr1-x433.google.com [IPv6:2a00:1450:4864:20::433]) by sourceware.org (Postfix) with ESMTPS id 255B83858410; Tue, 16 Nov 2021 23:55:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 255B83858410 Received: by mail-wr1-x433.google.com with SMTP id u18so970655wrg.5; Tue, 16 Nov 2021 15:55:25 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=nopeDWSF9if0dgtaHD6KLLl1bh1ed/y9jnO7zYE8XQs=; b=qX2NB1ZTCj/SJngX2kf/e1THNQkhfUAqFRTqYpnxSPCDSIOJg0Rycb8sWPj8IO5M7d 5SweBrewbDwJfHH42GtoYCLMS0CsromkXTrOzAgNfS16LX792uhomwgQDMOOwJ6htzAW qaYOsexn8ITbiz4iCGgs3o5FCGerUmg9JoF6KHSn/WOszONGrj8oW+Rr6O5Ci2jSaVxC LukshQaykq17MAZt3TcDCwLP7ROQR6x+B6it6MxPqWDBuTnN7lgNuvZqztxkssg9ZFqD +Vj6CwzOE5ok/2xxe35E+TajdX0+BU0hFDlm7nwV3xGH8wiQkiBBmDaU3BVpxXtUbWZZ VMdQ== X-Gm-Message-State: AOAM532XuN5yEh+omD/hxlPP6irjZCLGjH+JAQojrl8zAIq8ygBguNZk GiBCJOU5CRqe9zjTeONdxx4= X-Google-Smtp-Source: ABdhPJztOQfJ3XFABVIz2OnbnwFDLDVdHqhfyUC6eWzOk+2T0Pd94BJu3YapVjd0NxZKcItI1KId2A== X-Received: by 2002:a5d:4cd1:: with SMTP id c17mr14488451wrt.31.1637106924121; Tue, 16 Nov 2021 15:55:24 -0800 (PST) Received: from nz.home (host81-147-8-167.range81-147.btcentralplus.com. [81.147.8.167]) by smtp.gmail.com with ESMTPSA id n15sm4164569wmq.38.2021.11.16.15.55.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 Nov 2021 15:55:23 -0800 (PST) Received: by nz.home (Postfix, from userid 1000) id 6643EF3C3628; Tue, 16 Nov 2021 23:55:22 +0000 (GMT) To: binutils@sourceware.org Subject: [PATCH v2] gdb/nat/linux-osdata.c: fix build on gcc-12 (string overfow) Date: Tue, 16 Nov 2021 23:55:21 +0000 Message-Id: <20211116235521.223574-1-slyich@gmail.com> X-Mailer: git-send-email 2.33.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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: , From: Sergei Trofimovich via Gdb-patches Reply-To: Sergei Trofimovich Cc: Sergei Trofimovich , gdb-patches@sourceware.org Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" From: Sergei Trofimovich On gcc-12 build fails as: ../../gdbserver/../gdb/nat/linux-osdata.c: In function 'void linux_xfer_osdata_processes(buffer*)': ../../gdbserver/../gdb/nat/linux-osdata.c:330:39: error: '__builtin___sprintf_chk' may write a terminating nul past the end of the destination [-Werror=format-overflow=] 330 | sprintf (core_str, "%d", i); | ^ It's an off-by-one case in an infeasible scenario for negative huge core count. The change switches to std::string for memory handling. Tested by running 'info os processes' and checking CPU cores column. --- gdb/nat/linux-osdata.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 9746d1210fe..91bbe10e515 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -302,7 +302,7 @@ linux_xfer_osdata_processes (struct buffer *buffer) char *command_line; int *cores; int task_count; - char *cores_str; + std::string cores_str; int i; if (!isdigit (dp->d_name[0]) @@ -320,19 +320,15 @@ linux_xfer_osdata_processes (struct buffer *buffer) /* Find CPU cores used by the process. */ cores = XCNEWVEC (int, num_cores); task_count = get_cores_used_by_process (pid, cores, num_cores); - cores_str = (char *) xcalloc (task_count, sizeof ("4294967295") + 1); for (i = 0; i < num_cores && task_count > 0; ++i) if (cores[i]) { - char core_str[sizeof ("4294967295")]; - - sprintf (core_str, "%d", i); - strcat (cores_str, core_str); + string_appendf (cores_str, "%d", i); task_count -= cores[i]; if (task_count > 0) - strcat (cores_str, ","); + cores_str += ","; } xfree (cores); @@ -348,10 +344,9 @@ linux_xfer_osdata_processes (struct buffer *buffer) pid, user, command_line ? command_line : "", - cores_str); + cores_str.c_str()); xfree (command_line); - xfree (cores_str); } closedir (dirp); -- 2.33.1