From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id 6B9443858C2D for ; Thu, 12 May 2022 03:55:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 6B9443858C2D Received: by mail-pg1-x52b.google.com with SMTP id 202so3476857pgc.9 for ; Wed, 11 May 2022 20:55:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition; bh=v9U7WLMKqcBUwiG70IcRMZrbqK+kUlP3V8PbtB3GJE0=; b=D5onI3eesajpzuNc/hFsX5zenim9e8YddQdLDGyZ7V9QD3GB4xXfRgKqIY0gIpQxPc 96jVCcRupaQioJuqvl3k4qWUncl03Nr8MjAtyg9MMpV+Ta6TUrNfIEvFs4R0Y9OTuVix jLRk53qN+aDxyXyo0lPSEVM5Oyu16k4fPeRrQpsoZtf8MoL1B6zwfSBAgrrMqPWTSGQ5 o/n5bznMWg90Hlj2INKuIl1Ofm6n/681y8etlb8kW04pwmXbjTbFSsRuGIC3bJWZ9sf/ jrcX1Tdox26AX4iBVH1zjx4BKMd89DyOnmdtAgxAJOnckeHPPZcvy3O/Sw1spnlut475 dV9w== X-Gm-Message-State: AOAM533uiXRnO/iyxr7gXDnZ1UuMB9YOZrCHZk5Pa3o0vEA8mxs8/tdV tSWnSa0wMFqOrUNdRw1YysWiPjaQqp8= X-Google-Smtp-Source: ABdhPJw0vXf3BmpakfnDHmPuCVDYZ5ChAMrdEz5QN1rGeb7VZ5EL4Liyk8LJ0VwDFFUBLokLEO5hCQ== X-Received: by 2002:a63:4960:0:b0:3c6:b7cd:b7d0 with SMTP id y32-20020a634960000000b003c6b7cdb7d0mr15042890pgk.510.1652327704195; Wed, 11 May 2022 20:55:04 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:3a3c:2a34:5e7d:f59f]) by smtp.gmail.com with ESMTPSA id e4-20020a17090ada0400b001cd4989fecesm716511pjv.26.2022.05.11.20.55.03 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 20:55:03 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id E02A711403FE; Thu, 12 May 2022 13:25:00 +0930 (ACST) Date: Thu, 12 May 2022 13:25:00 +0930 From: Alan Modra To: binutils@sourceware.org Subject: PR29142, segv in ar with empty archive and libdeps specified Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3036.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Thu, 12 May 2022 03:55:06 -0000 PR 29142 * ar.c (main): Properly handle libdeps for zero file_count. diff --git a/binutils/ar.c b/binutils/ar.c index 0d4c7cf16a6..6f3f1d6946f 100644 --- a/binutils/ar.c +++ b/binutils/ar.c @@ -894,14 +894,16 @@ main (int argc, char **argv) being operated on. We shouldn't use 1st slot, but we want to avoid having to search all the way to the end of an archive with a large number of members at link time. */ - new_files = xmalloc ((file_count + 2) * sizeof (char *)); - new_files[0] = files[0]; - new_files[1] = LIBDEPS; - for (i = 1; i < file_count; i++) - new_files[i+1] = files[i]; - file_count = ++i; + new_files = xmalloc ((file_count + 2) * sizeof (*new_files)); + if (file_count) + { + new_files[0] = files[0]; + memcpy (new_files + 1, files, file_count * sizeof (*files)); + } + new_files[file_count != 0] = LIBDEPS; + file_count++; + new_files[file_count] = NULL; files = new_files; - files[i] = NULL; } switch (operation) -- Alan Modra Australia Development Lab, IBM