From patchwork Mon Dec 12 08:57:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 32268 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp2125417wrr; Mon, 12 Dec 2022 00:58:15 -0800 (PST) X-Google-Smtp-Source: AA0mqf55WmR68Cu9bdioX31UMi+pBVNZpZp7ByQl4z0HGCv8nz6ysTDvgsRK87+lnjTJsvz0wHvR X-Received: by 2002:a17:907:6014:b0:7c0:eb35:b3f9 with SMTP id fs20-20020a170907601400b007c0eb35b3f9mr10055862ejc.30.1670835495521; Mon, 12 Dec 2022 00:58:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670835495; cv=none; d=google.com; s=arc-20160816; b=CAZUCT/mfPUgNmZ99Jjt01w31e6z1eNT5GrGW4m9FwefoePLejyAaBxQuIrOT5MWpK gY2BdrZosdGyIdSBGzMA6UfUyvusMAIwCNa3Srltkcb+sx19zCDI01RdDgBDZyfzX42X F1LynlW+s1ZffMRtHvGeL4QvkbZGZZMSB4FfunauQaeNT0AKRAGEr+wSVZHw/T1b+5sM hW+YeyquHuJ+Ytxkqry78f9ATjZG5LSJm//qW4kiKDI2b+byslvr1fYHRLwKAo3x79sq xtWFi6dpVIcaG3Imp+CQsKqL70inmA8RT5WwVeTpCjsP1SeIxFmLgpv/7iQJlnUF5s/J aoKA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=mKBA2nyW15kU9r+9l4yJKDMzj7vFz5fZ4poau/yplGs=; b=K7NdeUUaNYSTVwtCjbwHTpdXlC8Xk9WE8Nk+yF52lhWJqHv4FvCjT2tb6dj1TO86y7 1BOPvdoRnkSLXS/AOy2yizSAo4kDsW71+JkcJ1B1BI1gLZmGIP6HIMgGsV0WKq1pKiYH I+zfJbA2Sm/M/eIZtf+KdSgcn78v1t4YPCyXcEgRohWAHzOEbZfMqUuWywgMkDDeble+ xpjjKmp47USLY6DWhj7CclXr1OCYC3/+fqqCmZvI3SYyIfw2Y4YxJBqHFKKsuaz+Q0rS FBQ6Y/wtj8NDauhkFBktjYSqSZhWQizISqr3SZrWBsXWqGgDTn2xybf5BVdK0DyfzV3L tz+A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=pvy28GyS; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id qk20-20020a1709077f9400b007c0a7bc2854si1823432ejc.410.2022.12.12.00.58.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 00:58:15 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=pvy28GyS; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9AB553950C7C for ; Mon, 12 Dec 2022 08:58:13 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9AB553950C7C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670835493; bh=mKBA2nyW15kU9r+9l4yJKDMzj7vFz5fZ4poau/yplGs=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=pvy28GySv8bThgF9UydRwipjAOUbHN3dCMHUed4CW3Vs6+mFODgOWV5Lxv5ApRp4O pzMCoOrxfFmySH8ow1bGpcj5E9OllJHLUnqsa/zpIRaiiUw7969fVpTk74+jh3AAxN XdygZYKWDh9vdJAIdv9Jmi2V7GavTM9AHNkMPXYc= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pj1-x102e.google.com (mail-pj1-x102e.google.com [IPv6:2607:f8b0:4864:20::102e]) by sourceware.org (Postfix) with ESMTPS id B85473946DED for ; Mon, 12 Dec 2022 08:58:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B85473946DED Received: by mail-pj1-x102e.google.com with SMTP id n65-20020a17090a2cc700b0021bc5ef7a14so11510181pjd.0 for ; Mon, 12 Dec 2022 00:58:03 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mKBA2nyW15kU9r+9l4yJKDMzj7vFz5fZ4poau/yplGs=; b=OjUfZaxqtHx2yMR2B6veyU+TLRCsolCjXCVfJsV95q5WQoDfipfyozo3AUBbCrq/Ep L3jBTm9OBc8NfoPDB+s4M5EljZO2yVZy8LU3WcxqgKpReuY8kMEr8kR7tKcGNIIkp5gm BKLPMk2OpE8cYRVAgd0vli+YUv4ylkeu85aMHrBHNmTJPpZuyqAUPPxCaxYgMqF8E/QW RfaYTzeJmeH/pu+k0CXPRDLeVIqTcMhcSvgZIMiJDyjEi8CaNuM61klxm+w0ISYWsmwi NtIcr1tGpVvqVZOV1KeADMQbN/UPshbYGP+3TDdFECb+puoMC0A7B/q1kmCYxDURBjYL Vvxw== X-Gm-Message-State: ANoB5pkXxc5pzjE8p5C4ykmPsgIEWXqa7zG8UArxV2U/hf4skB1g9lEr ynvBuFx6fab+cTQjddViQDMB5nJ3awY= X-Received: by 2002:a17:902:7001:b0:189:760d:c5ed with SMTP id y1-20020a170902700100b00189760dc5edmr16207379plk.28.1670835482392; Mon, 12 Dec 2022 00:58:02 -0800 (PST) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id x3-20020a170902a38300b0018957322953sm5764484pla.45.2022.12.12.00.58.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Dec 2022 00:58:02 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 5ABF11142826; Mon, 12 Dec 2022 19:27:59 +1030 (ACDT) Date: Mon, 12 Dec 2022 19:27:59 +1030 To: binutils@sourceware.org Subject: PR29872, uninitialised value in display_debug_lines_decoded dwarf.c:5413 Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, 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: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751998000454134527?= X-GMAIL-MSGID: =?utf-8?q?1751998000454134527?= Plus segvs if the C-library doesn't handle printf %s of NULL. PR 29872 * dwarf.c (null_name): New function. (process_debug_info): Use it here.. (display_debug_lines_raw): ..and here.. (display_debug_lines_decoded): ..and here. xcalloc directory_table. Simplify xcalloc of file_table. diff --git a/binutils/dwarf.c b/binutils/dwarf.c index 6ec33725876..b3039151ff6 100644 --- a/binutils/dwarf.c +++ b/binutils/dwarf.c @@ -255,6 +255,14 @@ print_view (uint64_t value, unsigned num_bytes) value & ~(~(uint64_t) 0 << num_bytes * 4 << num_bytes * 4)); } +static const char * +null_name (const char *p) +{ + if (p == NULL) + p = _("unknown"); + return p; +} + /* Read in a LEB128 encoded value starting at address DATA. If SIGN is true, return a signed LEB128 value. If LENGTH_RETURN is not NULL, return in it the number of bytes read. @@ -3843,7 +3851,7 @@ process_debug_info (struct dwarf_section * section, const char *name = get_DW_UT_name (compunit.cu_unit_type); printf (_(" Unit Type: %s (%x)\n"), - name ? name : "???", + null_name (name), compunit.cu_unit_type); } printf (_(" Abbrev Offset: %#" PRIx64 "\n"), @@ -4855,7 +4863,7 @@ display_debug_lines_raw (struct dwarf_section * section, typedef struct { - unsigned char *name; + char *name; unsigned int directory_index; unsigned int modification_date; unsigned int length; @@ -4883,7 +4891,7 @@ display_debug_lines_decoded (struct dwarf_section * section, int i; File_Entry *file_table = NULL; unsigned int n_files = 0; - unsigned char **directory_table = NULL; + char **directory_table = NULL; uint64_t n_directories = 0; if (startswith (section->name, ".debug_line.") @@ -4961,12 +4969,12 @@ display_debug_lines_decoded (struct dwarf_section * section, if (n_directories == 0) directory_table = NULL; else - directory_table = (unsigned char **) - xmalloc (n_directories * sizeof (unsigned char *)); + directory_table = (char **) + xcalloc (n_directories, sizeof (unsigned char *)); for (entryi = 0; entryi < n_directories; entryi++) { - unsigned char **pathp = &directory_table[entryi]; + char **pathp = &directory_table[entryi]; format = format_start; for (formati = 0; formati < format_count; formati++) @@ -4987,13 +4995,13 @@ display_debug_lines_decoded (struct dwarf_section * section, switch (form) { case DW_FORM_string: - *pathp = data; + *pathp = (char *) data; break; case DW_FORM_line_strp: SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size, end); /* Remove const by the cast. */ - *pathp = (unsigned char *) + *pathp = (char *) fetch_indirect_line_string (uvalue); break; } @@ -5035,8 +5043,8 @@ display_debug_lines_decoded (struct dwarf_section * section, if (n_files == 0) file_table = NULL; else - file_table = (File_Entry *) xcalloc (1, n_files - * sizeof (File_Entry)); + file_table = (File_Entry *) xcalloc (n_files, + sizeof (File_Entry)); for (entryi = 0; entryi < n_files; entryi++) { @@ -5062,13 +5070,13 @@ display_debug_lines_decoded (struct dwarf_section * section, switch (form) { case DW_FORM_string: - file->name = data; + file->name = (char *) data; break; case DW_FORM_line_strp: SAFE_BYTE_GET (uvalue, data, linfo.li_offset_size, end); /* Remove const by the cast. */ - file->name = (unsigned char *) + file->name = (char *) fetch_indirect_line_string (uvalue); break; } @@ -5109,7 +5117,7 @@ display_debug_lines_decoded (struct dwarf_section * section, { if (*data != 0) { - unsigned char *ptr_directory_table = data; + char *ptr_directory_table = (char *) data; while (data < end && *data != 0) { @@ -5128,15 +5136,14 @@ display_debug_lines_decoded (struct dwarf_section * section, } /* Go through the directory table again to save the directories. */ - directory_table = (unsigned char **) + directory_table = (char **) xmalloc (n_directories * sizeof (unsigned char *)); i = 0; while (*ptr_directory_table != 0) { directory_table[i] = ptr_directory_table; - ptr_directory_table - += strlen ((char *) ptr_directory_table) + 1; + ptr_directory_table += strlen (ptr_directory_table) + 1; i++; } } @@ -5174,7 +5181,7 @@ display_debug_lines_decoded (struct dwarf_section * section, i = 0; while (*ptr_file_name_table != 0) { - file_table[i].name = ptr_file_name_table; + file_table[i].name = (char *) ptr_file_name_table; ptr_file_name_table += strlen ((char *) ptr_file_name_table) + 1; @@ -5198,7 +5205,7 @@ display_debug_lines_decoded (struct dwarf_section * section, if (file_table == NULL) printf (_("CU: No directory table\n")); else if (directory_table == NULL) - printf (_("CU: %s:\n"), file_table[0].name); + printf (_("CU: %s:\n"), null_name (file_table[0].name)); else { unsigned int ix = file_table[0].directory_index; @@ -5216,14 +5223,16 @@ display_debug_lines_decoded (struct dwarf_section * section, directory = _(""); } else if (linfo.li_version >= 5) - directory = (char *) directory_table[ix]; + directory = directory_table[ix]; else - directory = (char *) directory_table[ix - 1]; + directory = directory_table[ix - 1]; if (do_wide) - printf (_("CU: %s/%s:\n"), directory, file_table[0].name); + printf (_("CU: %s/%s:\n"), + null_name (directory), + null_name (file_table[0].name)); else - printf ("%s:\n", file_table[0].name); + printf ("%s:\n", null_name (file_table[0].name)); } if (n_files > 0) @@ -5316,7 +5325,7 @@ display_debug_lines_decoded (struct dwarf_section * section, ++state_machine_regs.last_file_entry; /* Source file name. */ - file_table[n_files].name = op_code_data; + file_table[n_files].name = (char *) op_code_data; op_code_data += strlen ((char *) op_code_data) + 1; /* Directory index. */ READ_ULEB (file_table[n_files].directory_index, @@ -5398,10 +5407,10 @@ display_debug_lines_decoded (struct dwarf_section * section, } else if ((dir = file_table[file].directory_index) == 0) /* If directory index is 0, that means current directory. */ - printf ("\n./%s:[++]\n", file_table[file].name); + printf ("\n./%s:[++]\n", null_name (file_table[file].name)); else if (directory_table == NULL || n_directories == 0) printf (_("\n [Use file %s in directory table entry %d]\n"), - file_table[file].name, dir); + null_name (file_table[file].name), dir); /* PR 20439 */ else if (dir > n_directories) { @@ -5412,11 +5421,13 @@ display_debug_lines_decoded (struct dwarf_section * section, else if (linfo.li_version >= 5) printf ("\n%s/%s:\n", /* The directory index starts counting at 0. */ - directory_table[dir], file_table[file].name); + null_name (directory_table[dir]), + null_name (file_table[file].name)); else printf ("\n%s/%s:\n", /* The directory index starts counting at 1. */ - directory_table[dir - 1], file_table[file].name); + null_name (directory_table[dir - 1]), + null_name (file_table[file].name)); } break;