Commit Graph

291 Commits

Author SHA1 Message Date
Panu Matilainen 2986e7659d Remove redundant max_macro_depth initialization
Oldies but goodies: the max depth is already initialized in declaration
to _MAX_MACRO_DEPTH which has been the same 16 since late nineties. This
wasn't always the case of course...
2017-08-18 17:12:47 +03:00
Pavlina Moravcova Varekova 94e8cd6058 Make macro %{error:...} cause an actual failure 2017-08-17 16:16:23 +03:00
Pavlina Moravcova Varekova a3b153eb47 Unified messages printed by %{echo:...}, %{warn:...}, and %{error:...}
Send all through rpmlog(), unify formatting - previously echo and
warn were missing the trailing newline. The other change is that
echo now goes to stdout.
2017-08-17 16:15:39 +03:00
Panu Matilainen a3c1f733e0 Revert "Support quoting of macro arguments (#222)"
This simply breaks too many things - whole macro ecosystems exist based
on the assumption that quotes in arguments will pass to macros
untouched. Macro argument quoting support is necessary but it'll
need some entirely different approach that is either opt-in or
based on a different syntax altogether.

This reverts commit 47f0a899ef.
2017-08-14 14:28:10 +03:00
Jeff Johnson 3684424fe2 Add support for zstd compressed payload
v2 (Igor Gnatenko):
    * Switch off from RPM_CHECK_LIB
    * Reference zstd from rpm.pc
    * Link rpmio with zstd
  v3 (Florian Festi):
    * move changes to cvtfmode into separate patches
    * do not error out on wrong compression levels
    * ifdef out zstdio
    Closes: https://github.com/rpm-software-management/rpm/issues/256
    Closes: https://github.com/rpm-software-management/rpm/issues/297
    Signed-off-by: Igor Gnatenko <i.gnatenko.brain@gmail.com>
2017-08-09 17:41:03 +02:00
Pavlina Moravcova Varekova c4d41c30ce Shift macro scoping level values as they are defined
Some of scoping level values are defined in rpmmacros.h:
 #define RMIL_DEFAULT    -15
 #define RMIL_MACROFILES -13
...
 #define RMIL_GLOBAL     0

Before this patch RPM set these values decreased of 1 (for all macros).
Because of the (level - 1) in
    pushMacro(dst, me->name, me->opts, me->body, (level - 1), me->flags);
in function copyMacros scoping level for some CMDLINE macros was decreased of 1.
Because of the (level - 1) in
    pushMacro(mb->mc, n, o, b, (level - 1), ME_NONE);
in function doDefine scoping level values of all other macro types and
some CMDLINE macros was decreased of 1.
The patch shifts scoping level values back to the numbers defined
in rpmmacros.h.

This was spotted when investigating a regression introduced in
commit 237f35f16f where locally scoped
scoped macros did not get deleted on scope closing.
This also fixes the regression in question.

The first test in this patch tests automatic deleting of %defined macros.
The second test checks whether the behavior changed by
237f35f16f is still working correctly.

Thanks to Panu Matilainen for investigation and proposal of the main part
of the patch!
2017-08-09 14:36:15 +03:00
Pavlina Moravcova Varekova 6ca89685e3 Distinguish %define and %global in the illegal macro name error message 2017-08-09 13:31:18 +03:00
Pavlina Moravcova Varekova 47f0a899ef Support quoting of macro arguments (#222)
Single or double quotes can be used in macro arguments
as expected - they denote the begin and the end of the argument.

Example with 2-nd and 3-rd argument in double quotes:
./rpm --define "%foo() 1:%1 2:%2 3:%3" \
         --eval '%foo Next_argument_will_be_empty "" "Last argument"'
1:Next_argument_will_be_empty 2: 3:Last argument

Example with all arguments in single quotes,
without spaces between arguments:

./rpm --define "%foo() 1:%1 2:%2 3:%3" \
         --eval "%foo 'Next argument will be empty''''Last argument'"
1:Next argument will be empty 2: 3:Last argument
2017-08-09 13:02:54 +03:00
Per Øyvind Karlsen 7fe183d660 Add support for %{shrink:} (ported from rpm5.org)
Shrinks body by removing all leading and trailing whitespaces and
reducing intermediate whitespaces to a single space character.
2017-08-09 12:57:22 +03:00
Ville Skyttä 2901e265a0 Spelling fixes 2017-06-27 14:42:21 +02:00
Panu Matilainen 85953c0d72 Revert "Expand parameterized macro arguments one by one after splitting"
This essentially reverts commit 9ae7d1df31.
Leaving the testcase in place, just adjusting it to the expected
behavior where arguments that expand to empty are eaten away.

This actually matches what the shell does, so it follows the path of
least surprise in that sense - we emulate shell behavior with the
arguments etc already in many ways. What the shell has and rpm
has not is a way to pass those empty arguments by quoting. So
now we need a way to quote arguments - not just for passing empty
arguments but arguments containing whitespaces too.
2017-05-23 11:15:31 +03:00
Panu Matilainen 767d61ca3d Preserve %%-escapes when expanding parametric macro arguments (#217)
There needs to be a way to pass escaped macros names (and other strings
containging %%) as parametric macro arguments. Add an internal mode
to preserve %% when expanding the arguments. Fixes a regression introduced
in commit 5adc56897b.

Thanks to Michael Schroeder for pointing this out!
2017-05-18 13:47:21 +03:00
Panu Matilainen 9ae7d1df31 Expand parameterized macro arguments one by one after splitting
This makes makes the behavior much more sensible eg in case an
argument expands to an empty strings, such as:
    $ rpm --define '%foo() 1:%1 2:%2' --eval '%foo %nil bar'
After commit 5adc56897b rpm was
evaluating that to "1:bar 2:%2" which is hardly what you'd expect.

Thanks to Michael Schroeder for pointing this out! (#217)
2017-05-18 12:48:14 +03:00
Panu Matilainen 117b337fb2 Remember flags passed to rpmExpandMacros() in the expansion state buffer
Of course right now there are no implemented flags so this doesn't
actually do anything.
2017-05-18 11:19:13 +03:00
Panu Matilainen 5adc56897b Expand parametric macro arguments before processing (#127, RhBug:1397209)
This too is quite a fundamental change for macros: up to now, arguments
to parametric macros have not been expanded. It doesn't sound so bad
until you consider something like the case in RhBug:1397209:

        %global rev 133
        ...
        %setup %{?rev:-c}
        %autosetup %{?rev:-c}

One would expect %setup and %autosetup behave the same when you replace
one with the other, but no.  %setup gets "called" with -c, %autosetup
does not. Instead %autosetup receives a completely useless, literal
"%{?rev:-c}" as its argument.  That's just brain-meltingly non-sensical.

This certainly has the potential to break advanced macro constructs,
but OTOH what breaks might well be only written that way in order to
work around the former behavior.

There are some funny subtleties involved as the argument expansion
must occur in the callers scope, ie before we create any of the
automatic macros. For example, Fedora's font packages break if only
this or the macro scope visibility enforcement is applied but start
working again once both are present.
2017-03-07 14:26:45 +02:00
Panu Matilainen 307627993e Enforce visibility scoping for automatic macros
When a parametric macro "calls" another parametric macro with fewer
arguments than it received, the inner macro would see the %<n>
macros of the outer call which is an obvious scoping violation
and quirky behavior, making macro writing harder than it needs be.

Similar scoping issues exist for manually defined macros but those
have further complications (because of %undefine semantics)  that we
sheepishly avoid here by limiting the visibility enforcing to automatic
macros only (for now at least).
2017-03-07 14:06:55 +02:00
Panu Matilainen 237f35f16f Change macro scoping to be global / local to parametric macros
This changes the macro scoping rules quite fundamentally: macro
definitions are local to the parametric macro they were defined in,
and everything else is global. Among other things, this makes this
common spec idiom (RhBug:552944, RhBug:551971 etc) behave
deterministically because "foo" is placed into global scope:

        %{?!foo: %define foo bar}

In theory it's certainly possible that this breaks somebodys carefully
crafted advanced macros but it seems quite unlikely, considering how
broken the alleged block-scoping has been. OTOH for macros defined
within parametric macros, nothing actually changes as that scoping has
always been enforced by rpm. The non-global define tracking is also
no longer needed for emitting warnings, because the case where it
was needed simply no longer exists.

Note that macro recursion depth is a different thing and still needs
to be tracked separately.
2017-03-07 14:03:12 +02:00
Panu Matilainen e2f4347356 Consolidate all macro argument setup to grabArgs()
Since we actually setup all the same automatic macros whether there
are arguments or not, doing it centrally only makes sense. Shuffle
things around a bit in preparation for the next steps.
2017-03-07 13:58:56 +02:00
Panu Matilainen 3b1f4b0c6c Cosmetics: if, while and switch are followed by a space
The missing space style-error has been recently coming common enough
somebody might think that IS the expected style, its not. Some of these
are actually very old, but fix across the board for consistency..

Strictly white-space only change.
2017-02-27 17:41:37 +02:00
Peter Jones e6f346ead4 Don't nerf "rpmspec --eval '%trace' -P foo.spec" output with ellipses.
The whole point of %trace is that some macro is being evaluated in a
surprising way, and you want to know what it is.  This is often the
result of an unfortunately complex macro with difficult to see
intermediate results.

Shortening the output because it doesn't fit past 61 or so characters
does not help, and we have big screens these days.  Don't do it.

Signed-off-by: Peter Jones <pjones@redhat.com>
2017-02-24 13:23:16 +01:00
Peter Jones bf248badd3 Bounds check strings to print correctly in %trace mode.
In %trace mode, evaluating a macro which is undefined causes an invalid
read of 1 byte when searching for the end of the string:

trillian:~$ valgrind rpmspec --eval '%trace' --eval '%{?myUndefinedMacro}'
==21534== Memcheck, a memory error detector
==21534== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==21534== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==21534== Command: rpmspec --trace --eval %{?myUndefinedMacro}
==21534==

  1>   %{?myUndefinedMacro}^==21534== Invalid read of size 1
==21534==    at 0x55018D4: printMacro (macro.c:296)
==21534==    by 0x5502DFC: expandMacro (macro.c:1077)
==21534==    by 0x5503710: doExpandMacros (macro.c:1280)
==21534==    by 0x5504AB6: rpmExpand (macro.c:1629)
==21534==    by 0x508F59A: rpmcliAllArgCallback (poptALL.c:120)
==21534==    by 0x6DAF71D: invokeCallbacksOPTION (popt.c:156)
==21534==    by 0x6DAF75B: invokeCallbacksOPTION (popt.c:139)
==21534==    by 0x6DB1428: poptGetNextOpt (popt.c:1515)
==21534==    by 0x508F912: rpmcliInit (poptALL.c:302)
==21534==    by 0x1095B2: main (rpmspec.c:63)
==21534==  Address 0x8a010f3 is 0 bytes after a block of size 19 alloc'd
==21534==    at 0x4C2DB9D: malloc (vg_replace_malloc.c:299)
==21534==    by 0x5507C17: rmalloc (rpmmalloc.c:44)
==21534==    by 0x5502788: expandMacro (macro.c:927)
==21534==    by 0x5503710: doExpandMacros (macro.c:1280)
==21534==    by 0x5504AB6: rpmExpand (macro.c:1629)
==21534==    by 0x508F59A: rpmcliAllArgCallback (poptALL.c:120)
==21534==    by 0x6DAF71D: invokeCallbacksOPTION (popt.c:156)
==21534==    by 0x6DAF75B: invokeCallbacksOPTION (popt.c:139)
==21534==    by 0x6DB1428: poptGetNextOpt (popt.c:1515)
==21534==    by 0x508F912: rpmcliInit (poptALL.c:302)
==21534==    by 0x1095B2: main (rpmspec.c:63)
==21534==

  1>   %{?_transaction_color}^
  1>   %{?_prefer_color}^
  1>   %{_netsharedpath}^
  1>   %{_install_langs}^
==21534==
==21534== HEAP SUMMARY:
==21534==     in use at exit: 7,183 bytes in 71 blocks
==21534==   total heap usage: 7,811 allocs, 7,740 frees, 3,500,361 bytes allocated
==21534==
==21534== LEAK SUMMARY:
==21534==    definitely lost: 19 bytes in 1 blocks
==21534==    indirectly lost: 0 bytes in 0 blocks
==21534==      possibly lost: 0 bytes in 0 blocks
==21534==    still reachable: 7,164 bytes in 70 blocks
==21534==         suppressed: 0 bytes in 0 blocks
==21534== Rerun with --leak-check=full to see details of leaked memory
==21534==
==21534== For counts of detected and suppressed errors, rerun with: -v
==21534== ERROR SUMMARY: 5 errors from 1 contexts (suppressed: 0 from 0)
trillian:~$

This can easily be avoided by checking the first byte as well as the
second for our sentinal value (NUL).

Signed-off-by: Peter Jones <pjones@redhat.com>
2017-02-24 13:23:16 +01:00
Panu Matilainen 1767bc4fd8 Preserve macro scoping level on re-entry from Lua
When a %{lua:...} macro calls rpm.expand(), it re-enters the macro
environment at depth zero regardless of what the "calling" nesting
level was. This could lead to wrong macros getting mopped out on
complex, nested lua + native macro calls.

Store the depth in global macro context and pick up the initial value
from there to keep the nesting level increasing on when entering %{lua:...}
space.
2017-01-20 15:23:01 +02:00
Panu Matilainen f95825ef1b Add return code to rpmPushMacro() and rpmPopMacro()
These haven't been in any public release yet so there's time to
fiddle with stuff like this. Both always succeed currently so no
change in that respect but it's better to have room for expansion...
2017-01-19 16:03:15 +02:00
Panu Matilainen 31bc44c5a9 Actually return errors from macro definition 2017-01-19 14:24:26 +02:00
Panu Matilainen bd942d5fee Raise actual error on unknown option to parametric macro 2017-01-19 14:18:29 +02:00
Panu Matilainen 02dea59e3a Flag real errors from macro define and undefine errors
This WILL cause behavior changes in cases where there have been
illegal macro names etc which have previously been more or less
ignored with an logged error, but now they will actually abort.
2017-01-19 14:15:38 +02:00
Panu Matilainen 8f86427d8a Pass macro buf, not context, to doUndefine()
No functional changes, just for symmetry with doDefine()
2017-01-19 14:13:15 +02:00
Panu Matilainen 7d1ceefe9e Store errors during macro expansion centrally
With direct rc assignments there's a risk that a previously
flagged error is overwritten by a success, only flagging errors
specifically avoids that. It also makes things simpler in some ways.

This probably changes *some* behavior wrt errors, in that errors
might actually get counted as such.
2017-01-19 14:08:28 +02:00
Panu Matilainen b298e93686 Arrange single point of exit in expandMacro() 2017-01-19 13:41:42 +02:00
Panu Matilainen 1b4744e1c6 Refactor %{lua:...} macro execution to a helper function
expandMacro() is big enough without this kind of easily separable
material...
2017-01-19 12:09:10 +02:00
Panu Matilainen e3e99e929a Reindent expandMacro() to common rpm style
This huge and scary looking diff is strictly whitespace-only change.
2017-01-19 11:28:40 +02:00
Panu Matilainen 5517d26061 Rename addMacro() and delMacro() to rpmPushMacro() and rpmPopMacro()
These are not deprecated at all no matter what the header has been
saying for the past 15+ years, they're used by rpm itself all over
the place as rpmDefineMacro() serves a slightly different purpose
and there's no rpmUndefineMacro() anyway.

Lets make 'em into proper citizens and move them into rpm namespace,
and while at it, call the operations push and pop since that's much
closer to what actually happens.

Finally, add simple wrapper macros to keep external code compilable
while getting the non-namespaced stuff out of ABI.
2016-10-24 13:09:38 +03:00
Panu Matilainen 34e839b845 Eliminate expandMacros() from the API + ABI
This allegedly been deprecated for no less than sixteen years,
but until very recently it's been in use by rpm itself and only
now with rpmExpandMacros() its possible to eliminate this one.
All sane external callers have been using rpmExpand() anyway
since expandMacros() is "buffer limited".
2016-10-24 12:51:13 +03:00
Lubos Kardos ddf9ec7bef rpmExpandMacros() is modified to be able to return more return codes
Now rpmExpandMacros() returns integer as a return value. Negative return
value implies failure. Return values can be more utilized in the future.
E. g. return value could specify how many macros were expanded in given
string.
2015-11-26 15:54:27 +01:00
Tom Hughes aee8446eb4 Rename expandMacrosU to rpmExpandMacros
Address review issues from #32 by renaming the function and
cleaning up the comment and parameter list.
2015-11-26 15:54:27 +01:00
Tom Hughes 61838b0fda Remove size limit when expanding macros
This removes a seemingly undocumented, and not even well defined, limit
on the size of a macro expansion when parsing a spec file.

[lkardos@redhat.com: created new funtion expandMacrosU() (Unlimited)
instead of modifying expandMacros() in order not to change API/ABI]

Signed-off-by: Lubos Kardos <lkardos@redhat.com>
2015-11-19 12:42:59 +01:00
Lubos Kardos 54f24ec548 Fix reading a memory right after the end of an allocated area.
The problem evinced itself when somebody tried to use the macro
expansion on the string "%!". The problem was revealed by compiling
with "--fsanitize=memory" (rhbz:#1260248).
2015-09-21 11:34:42 +02:00
Florian Festi b151b5297b Fix doxygen warnings
Make shure parameter descriptions in doc strings match the functions
2015-02-17 15:31:22 +01:00
Panu Matilainen ace7fe9d58 Enable the unused macro warning again, now without performance penalty
- Track non-global macro definitions in a helper variable, this
  eliminates the need for costly macro table walking at end of each
  expansion cycle as we only bother doing it if non-global macros
  were defined during a given expansion cycle.
2014-09-02 11:50:41 +03:00
Panu Matilainen d4ab1d82d7 Macro-expand %{load:...} argument to make the thing more useful... 2014-07-01 12:05:51 +03:00
Panu Matilainen 817959609b Handle line continuation in grabArgs() (related to RhBug:1045723)
- Commit 1bdcd05008 to fix RhBug:1045723
  broke some funky java macros in Fedora which include line continuation
  in the argument (comments 6-7 in the bug). That it ever worked seems
  far more like luck than by design but since this seems to fix it...
2014-06-25 11:32:24 +03:00
Panu Matilainen eb62542695 Fix ancient buffer overflow on unterminated macro options
- Test for terminating ')' existence before copying, otherwise we'll
  end up walking over the edge of the world.
- Return address from doDefine() on error will likely differ after
  this, whether that actually affects anything remains to be seen...
2014-06-11 15:40:00 +03:00
Panu Matilainen 4089316531 Fix ancient buffer overflows on macro define and undefine (RhBug:1087000)
- Both doDefine() and doUndefine() assumed the macro string would
  always fit into MACROBUFSIZ, which of course is true for any
  normal use but we cant make such assumptions.
- In the case of %define/%global there are various other overrun-issues
  that need further changes to fix.
2014-06-09 14:35:19 +03:00
Panu Matilainen f07490f5dc Only enable the unused macro warning on errors and when tracing
- In the current macro implementation the check is simply far too
  expensive to leave on always. Its useful though, so enable it
  when tracing macro expansion, in which case you're probably
  troubleshooting some macro-issues...
2014-03-26 11:43:41 +02:00
Panu Matilainen b19348342b And finally, actually enable the unused macro warning.
- Reword the message to something hopefully little more understandable,
  change from error to a warning (only matters for output)
- Check unused macros at end of every scope, but unlike with parametrized
  macros, dont actually delete
- Only whine once per unused macro due to the above to avoid spurious
  output due to above
- This catches the common error in specs where %define is used in a scope
  unintentionally, eg "%{!?foo: %define foo 1}" where the just defined
  macro should actually fall out of scope upon the closing }, but
  only gets erased if any parametrized macro is "called" and causes
  insane behavior such as described in RhBug:552944. So at least we'll
  warn on these situations.
2014-02-28 16:45:33 +02:00
Panu Matilainen f9a6855faa Consider macro used if we've so much as looked it up 2014-02-28 16:38:56 +02:00
Panu Matilainen e65e27ab25 Make macro deletion in freeArgs() optional
- Not used yet, but using this for issuing warnings only
2014-02-28 16:12:33 +02:00
Panu Matilainen ae58c96248 Move macro usage tracking to flags
- We dont really care how many times a macro has been used, just
  whether it has been used or not...
2014-02-28 16:10:14 +02:00
Panu Matilainen fae7325f8d Add + use a flag for identifying automatic macros
- In practise doesn't change anything visible as the "macro not used"
  message is disabled, but tracking a flag bit is saner than
  strlen() + strchr() + bunch of other stuff which isn't even correct:
  prior to this %## would've triggered "unused" errors which is plain
  wrong, and complaining about unused %1 %2 ... isn't really right
  either.
2014-02-28 15:57:09 +02:00
Panu Matilainen 647f0d488f Revert stricter macro substitution syntax entirely, for now
- Revert back to pre commit c22d5b1299
  state wrt macro substitution. The commit does have fixes we want
  but it also breaks valid cases which is not okay. We really need
  a far more thorough macro test suite before these kind of changes,
  and also need to do the changes in more controlled pieces.
2014-02-25 16:17:37 +02:00