Description of Problem: rpm segfaults when a macro with parameters is used. Please find attached a patch that fixes the problem. How Reproducible: see below Steps to Reproduce: 1. Put the following macro in your ~/.rpmmacros %my_macro(ab) %0 %** [%*] -a: (%{-a}) -b: (%{-b}) 2. Run: rpm --eval '%my_macro -a -b arg1 arg2' Actual Results: Segmentation fault (core dumped) Expected Results: Text output: "my_macro -a -b arg1 arg2 [arg1 arg2] -a: (-a) -b: (-b)" Additional Information: I fixed this with the following patch: --- rpmio/macro.c.orig Fri Jan 19 02:47:25 2001 +++ rpmio/macro.c Thu Jun 21 17:39:30 2001 @@ -759,7 +759,7 @@ b = be = stpcpy(buf, me->name); addMacro(mb->mc, "0", NULL, buf, mb->depth); - + argc = 1; /* XXX count argv[0] */ /* Copy args into buf until lastc */ @@ -801,7 +801,7 @@ /* Build argv array */ argv = (const char **) alloca((argc + 1) * sizeof(char *)); be[-1] = ' '; /* be - 1 == b + strlen(b) == buf + strlen(buf) */ - buf[0] = '\0'; + be[0] = '\0'; b = buf; for (c = 0; c < argc; c++) { argv[c] = b; @@ -818,6 +818,11 @@ if (c == '?' || (o = strchr(opts, c)) == NULL) { rpmError(RPMERR_BADSPEC, _("Unknown option %c in %s(%s)\n"), c, me->name, opts); + /* Reset getopt() state */ + optind = 0; + optarg = NULL; + argv[1] = NULL; + getopt(1,(char **)argv,"xyz"); return se; } *be++ = '-'; @@ -845,12 +850,19 @@ for (c = optind; c < argc; c++) { sprintf(aname, "%d", (c - optind + 1)); addMacro(mb->mc, aname, NULL, argv[c], mb->depth); - *be++ = ' '; be = stpcpy(be, argv[c]); + *be++ = ' '; } - + if (be > b) + be[-1] = '\0'; /* Add unexpanded args as macro. */ addMacro(mb->mc, "*", NULL, b, mb->depth); + + /* Reset getopt() state */ + optind = 0; + optarg = NULL; + argv[1] = NULL; + getopt(1,(char **)argv,"xyz"); return se; }
Created attachment 21526 [details] Patch for rpmio/macro.c (against rpm-4.0.2)
Please close this bug, it was opened against the wrong version of Red Hat Linux (correct version is 7.1)
OK, closed. FWIW, there was a simpler patch recently added to rpm-4.0.3 that sets optind =1 on glibc systems. That appears to be the same problem that you were experiencing. Please reopen this bug if your problem persists.