Bug 4220

Summary: macro parameter numbers have wrong offset - rpm-3.0.2
Product: [Retired] Red Hat Linux Reporter: benedict
Component: rpmAssignee: Jeff Johnson <jbj>
Status: CLOSED CURRENTRELEASE QA Contact:
Severity: medium Docs Contact:
Priority: medium    
Version: 6.0   
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 1999-07-30 21:20:01 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:

Description benedict 1999-07-27 12:54:59 UTC
The following snippet which uses macro parameters,
expands incorrectly, using the macro name as %1
in most cases ...

%prep

%define testmacro() echo %1 ; echo %*

%testmacro a b c

%testmacro a b

%testmacro a

%testmacro

exit 1

Here is the expansion as it is in the rpm-tmp.$$ file

echo testmacro ; echo testmacro a b c
echo testmacro ; echo testmacro a b
echo testmacro ; echo testmacro a
echo %1 ; echo

exit 1

Comment 1 Jeff Johnson 1999-07-27 15:35:59 UTC
This works for me. I'll put the spec file snippet and output
in attached email to avoid line wrap in bugzilla.

Comment 2 benedict 1999-07-27 15:43:59 UTC
On a linux box running rpm-3.0-6.0 the snippet works as intended.

However, on Solaris2.7, HPUX10.20, and Irix6.5 boxes, running
rpm-3.0.2, it fails.

Comment 3 Jeff Johnson 1999-07-27 15:46:59 UTC
What platform are you using rpm on?

Comment 4 benedict 1999-07-29 09:59:59 UTC
Try this:

%define testmacro() echo %0 ; echo %1 ; echo %* ; echo %#; echo %{#}

%testmacro a b c

%testmacro a b

%testmacro a

%testmacro

on a linux box, it expands to

echo testmacro ; echo a ; echo a b c ; echo #; echo 4
echo testmacro ; echo a ; echo a b ; echo #; echo 3
echo testmacro ; echo a ; echo a ; echo #; echo 2
echo %0 ; echo %1 ; echo  ; echo #; echo %{#}

That's hardly correct, either.

1) %# isn't expanded
2) %{#} is not 'the number of arguments' as docs/macros says
3) when no args are present, grabArgs isn't called at all and even %0
is not set.

The problem lies in the code in lib/macro.c assigning to optind
you are not supposed to do so.  The following patch makes my unix
boxes do the same as the linux box:

--- rpm-3.0.2.orig/lib/macro.c  Fri Jul  2 23:14:17 1999
+++ rpm-3.0.2/lib/macro.c       Thu Jul 29 11:57:29 1999
@@ -590,6 +590,7 @@
     const char **optv;
     int opte;
     int c;
+    int saveoptind;

     /* Copy macro name as argv[0] */
     argc = 0;
@@ -649,7 +650,7 @@
     opts = me->opts;

     /* First count number of options ... */
-    optind = 0;
+    saveoptind = optind;
     optc = 0;
     optc++;    /* XXX count argv[0] too */
     while((c = getopt(argc, (char **)argv, opts)) != -1) {
@@ -668,7 +669,7 @@
     optv[opte] = NULL;

     /* ... and finally define option macros. */
-    optind = 0;
+    optind = saveoptind;
     optc = 0;
     optc++;    /* XXX count optv[0] */
     while((c = getopt(argc, (char **)argv, opts)) != -1) {

Comment 5 Jeff Johnson 1999-07-29 12:40:59 UTC
Except for the expansion of %1 (which returns %1 not "") when
given no parameters, these problems are fixed. Thanks *very* much
for digging into the optind problem on non-linux platforms.

Comment 6 benedict 1999-07-30 08:17:59 UTC
Thanks for the fix.  I'm looking forward to get it in 3.0.3

As for your comment: I think %1 *should* return %1 when there
are no parameter, that is very logical.  Otherwise, by generalisation,
%4 should return "" when there are less than 4 parameters etc, and
that doesn't sound right to me.

BTW: I think bugzilla is good for tracking error reports, but I miss
this kind of information on the rpm-list - have you guys considered
making some kind of cross-posting, so everyone on the rpm-list are
aware of bugs posted in bugzilla?

Comment 7 Jeff Johnson 1999-07-30 21:20:59 UTC
Um, %1 is going to expand to the outre context's arg1 if ther
are nested conetxts's but I'll deal with that when the time comes.

Bugzilla is *way* too noisy to blindly copy onto rpm-list. Besides,
I'd rather leave rpm-list for discussions rather than bugs. You
can always add yourself to the CC list if you want notification
of bug changes. You might think about requesting automatic
notification for any rpm bug by filing a request for enhancement
against bugzilla. That feature has been discussed already ...

Thanks again for the patch.