ref: 85b514f5c1368e93ba0f368366fd68db0a4eb3d6
parent: e417ad93949172889df996396f7e46560cc81d9d
author: Roberto E. Vargas Caballero <[email protected]>
date: Mon Mar 5 16:52:30 EST 2018
[nm] Extrace correct name of archive members
--- a/inc/ar.h
+++ b/inc/ar.h
@@ -2,9 +2,11 @@
#define ARMAG "!<arch>\n" /* ar "magic number" */
#define SARMAG 8 /* strlen(ARMAG); */
#define ARFMAG "`\n"
+#define SARNAM 16
+
struct ar_hdr {
- char ar_name[16]; /* name */
+ char ar_name[SARNAM]; /* name */
char ar_date[12]; /* modification time */
char ar_uid[6]; /* user id */
char ar_gid[6]; /* group id */
--- a/nm/main.c
+++ b/nm/main.c
@@ -39,11 +39,26 @@
return strncmp(magic, ARMAG, SARMAG) == 0;
}
-static void
+static char *
+getfname(struct ar_hdr *hdr, char *dst)
+{
+ char *p;
+
+ memcpy(dst, hdr->ar_name, SARNAM);
+ dst[SARNAM] = '\0';
+ if ((p = strchr(dst, ' ')) != NULL)
+ *p = '\0';
+ if ((p = strchr(dst, '/')) != NULL)
+ *p = '\0';
+ return (*p == '\0') ? NULL : dst;
+}
+
+static void
ar(char *fname, FILE *fp)
{
struct ar_hdr hdr;
long pos, siz;
+ char member[SARNAM+1];
arflag = 1;
fseek(fp, sizeof(struct ar_hdr), SEEK_CUR);
@@ -51,6 +66,9 @@
while (fread(&hdr, sizeof(hdr), 1, fp) == 1) {
pos = ftell(fp);
if (strncmp(hdr.ar_fmag, ARFMAG, strlen(ARFMAG)))
+ goto corrupted;
+
+ if (!getfname(&hdr, member))
goto corrupted;
siz = 0;