From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x102e.google.com with SMTP id n65-20020a17090a2cc700b0021bc5ef7a14so11510181pjd.0 for ; Mon, 12 Dec 2022 00:58:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=mKBA2nyW15kU9r+9l4yJKDMzj7vFz5fZ4poau/yplGs=; b=LBMmhhTJJ7RkSBDTLwNqhwDUOyEvE/hROqKhojfKyTKBo6aeenp8SfTXJyXBTjDDCO Vx1c2S4XWJ58KWogOei7guayT921HkInt+JaZ2c96cKoHv7gU3/A0EooRhBOWKdDsB8Y StJg5ImV3RPbqIDWojN2AJMCmRZM315LqhY7hdbMa35r+No8arDA1V+8zhR9aPcsElLS +Rd4kb+i9VdDOBm08eXMjY0Dgvzu9dzBVYk2C62nGSbPb+GGh6ECiwpoRW0sxACiqCh3 R2b6MRVVnN7k8S1DV03U6O8A7mqZsfwiHQMgmaoZrqKV/f/yT1Uv5RO0jqFLsdGcNDTY ZMbw== 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-Google-Smtp-Source: AA0mqf4CIxCucY/ZpCd9zFRpJ85SuKPauUTwnEOC+OKZJmgs8zSlVz2Ei1izW3k9Wb9YEoFXZUDMIA== 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 From: Alan Modra To: binutils@sourceware.org Subject: PR29872, uninitialised value in display_debug_lines_decoded dwarf.c:5413 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii 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 List-Id: 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; -- Alan Modra Australia Development Lab, IBM