Commit Graph

1533 Commits

Author SHA1 Message Date
Panu Matilainen d690ed7e48 Restore flagless variants of rpmExprBool() and rpmExprStr() for ABI compat
Commit cb4e5e755a added flags arguments
to rpmExprBool() and rpmExprStr(), but unfortunately rpm 4.15 sailed
with flagless versions them. It's extremely unlikely that anything out
there is actually using these, but then you never really know.
Rpm soname bumps are so inconvenient that we really do not want to do
that just for these two, so preserve binary compatibility and restore
flagless variants of both, adjust internal code to use flagged versions
always. If only we had symbol versioning, sigh.
2020-03-23 13:44:12 +02:00
Panu Matilainen 1d624239c4 Implement %{macrobody:...} built-in for retrieving the literal macro body
Fixes: #582
2020-02-19 13:09:21 +01:00
Panu Matilainen 9b37c4b349 Add APIs for testing whether a macro is defined and whether its parametric
Whether a macro is defined can be tested with eg rpmExpandNumeric()
but this is somewhat cumbersome and expensive for such a simple thing.
Prior to this, there was no way to find out whether a macro is
parametric (aka parameterized) or not.
2020-02-11 11:18:57 +01:00
Michael Schroeder 490c70d43c Use RPMMACRO_LITERAL/ME_LITERAL when defining some macros
It is used for setting __file_name, __file_lineno, buildsubdir.
We also use it for unmasking %license in the %files section.
2020-02-10 15:41:33 +02:00
Michael Schroeder cc66df4fa1 Add rpmPushMacroFlags function to allow the definition of literal macros
Literal macros are macros that do not expand their body when they
get expanded.
2020-02-10 15:41:33 +02:00
Michael Schroeder 07cc75f4d4 Add a ME_LITERAL macro flag that turns off body expansion
This is a much saner than to double the '%' characters after
macro argument expansion.
2020-02-10 15:41:33 +02:00
Michael Schroeder 7d3a87ca18 Remove duplicated code in macro expansion
The code for macro existence checking and flag processing
is exactly the same.
2020-02-10 15:41:33 +02:00
Panu Matilainen abcb0af241 Refactor %{verbose:...} handling out of doFoo()
%{verbose:...} is special due to conditional expansion so it makes
sense to have the logic separated. No functional changes.
2020-02-10 14:43:50 +02:00
Panu Matilainen 47dbb3e070 Don't double-expand arguments to built-in macros (#311)
Double-expanding the output makes it rather impossible to reliably
handle filenames (and other content) with % characters in them.

Fixes: #313
2020-02-10 14:43:50 +02:00
Panu Matilainen b551b256ea Refactor %{expand:...} handling to its own function
%{expand:...} is a rather special built-in as its *purpose* is to
double-expand its argument, so it makes sense to separate it from
the others.

No functional changes intended here.
2020-02-10 14:43:50 +02:00
Panu Matilainen 227cddca88 Fix %{uncompress:...} double-expanding arguments + other miscellania
%{uncompress:...} is fairly complicated as far as builtin macros go:
it needs to first expand its argument to discover the actual file
its supposed to look at, then determine whether the file exists and
what sort of compression to use, then determine the macro to use
for decompressing that kind of file, expand said macro and finally
catenate the expanded argument to the lot. That's a lot of goo to
do inline doFoo(), so refactor it into a separate function.

Up to now the last step was implemented by re-expanding the argument
too, which makes it impossible to reliably handle paths with percent
signs. Just expand the command, and catenate the argument as deity
intended.

Additionally make behavior with empty argument consistent with other
builtins: %{uncompress:} expands to nothing instead of printing out
an error message with an empty filename.
2020-02-10 14:43:50 +02:00
Panu Matilainen 94623389ba Fix buffer overflow in %{S:...} and %{P:...} macro primitives
We can't assume that a buffer allocated for one thing is sufficient
for another thing. The S and P macros share the same exact logic,
refactor to use common code to avoid having to fix twice, cleaning
up doFoo() a bit in the process.

Fixes: #1019
2020-02-10 14:43:50 +02:00
Michael Schroeder 8b1ea52de3 Double the '%' chars when splitting macro args
Before this commit, there was an "escape" flag that made the macro
expansion keep '%%' escapes. But this did not work for macros
that returned an '%' character by other means.

Remove the old escape mechanism and instead double the '%' characters
when the body is split into argument.

Fixes: #1055
2020-02-06 16:00:27 +02:00
Panu Matilainen da94ee8195 Drop useless %{F:..} built-in macro
I fail to see how this is useful functionality to anybody:

	$ rpm --eval "%{F:foo}"
	filefoo.file
2020-01-29 12:51:26 +01:00
Mikhail Novosyolov f3d476753b xzdio: Add line break to warning 2020-01-14 11:54:11 +02:00
Thierry Vignaud c464f1ece5 fix zstd magic
I spot it while adding support for zstd compressed metadata in
URPM/urpmi, which was broken by this typo

typo introduced in commit 3684424fe2
2020-01-02 10:52:19 +02:00
Ross Burton 61ea5a8ea6 rpmio: initialise libgcrypt
If we're using libgcrypt for hashing we need to initialise libgcrypt as
otherwise it is not thread-safe.  Without this it will crash when used
in parallel packaging runs.

Fixes #968
2019-12-05 12:53:29 +02:00
Panu Matilainen 6b18e76f3d Fix resource leaks on zstd open error paths
If zstd stream initialization fails, the opened fd and the stream
itself are leaked. Handle error exit in a central label.
2019-11-22 09:39:00 +02:00
Panu Matilainen 998b516b8b Replace use of obsolete ctime() with strftime()
POSIX.1-2008 marked ctime() as obsolete and recommends strftime() instead.
We get two flies on one stroke as ctime() is also classified "dangerous"
by LGTM due to not being thread-safe.

strftime(..., "%c"...) isn't exactly the same as ctime() but is consistent
with what we use elsewhere and sufficient for debug purposes anyway.
2019-10-31 11:59:20 +01:00
Panu Matilainen d609a426f6 Resurrect --prtpkts debug switch functionality
Move the _print_pkts global to librpmio where the two relevant users
can actually access it, and make them use it. This has been broken
for years...
2019-10-31 11:59:20 +01:00
Panu Matilainen 4cae7bc073 Drop bogus const from validName() return 2019-10-30 14:54:09 +01:00
Panu Matilainen 86fe9e431c Fix global variable hiding in Fdopen()
Harmless but also unnecessary obfuscation, when originally written the
thing we now know as zstd was 20 years in the future.
2019-10-30 14:54:09 +01:00
Panu Matilainen facee2c70a Avoid using types from Lua includes in rpmlua.h again
rpmlua.h was originally written in a way that allows it to be included
regardless of whether Lua is actually enabled in rpm or not, or where
Lua headers are, specifically to isolate the rest of rpm from these
details. That was changed in commit 62bd62286a
when <lauxlib.h> started getting included in rpmlua.h, which leaks to
places like librpmbuild which do not directly use Lua.

The way Lua typedef's the luaL_Reg struct to itself defies my C fu for
for handling this in some nicer typesafe way, fix this all by just using
a void pointer instead, this is just an internal API where buyer can be
expected to beware.

Fixes #888
2019-10-10 16:34:12 +03:00
Pavlina Moravcova Varekova a536e26061 Add missing position marker to a few expression error messages 2019-10-01 14:09:00 +03:00
Pavlina Moravcova Varekova ff4ce836e7 Add an error message when rpm fails to open a pipe for shell expansion 2019-09-26 11:37:24 +03:00
Michael Schroeder 82f89a3ce4 Change || and && operators to behave like perl/python/ruby
This makes the operators return the last evaluated term instead
of a boolean value. For example 2 || 3 will return 2.

We also allow strings by using boolifyValue(), allowing
expressions like "%{?foo}" || "default".
2019-09-24 14:35:48 +03:00
Michael Schroeder 4cff613a0b Use boolifyValue in the not operator
This can be used for expressions like !"%?foo"
2019-09-24 14:35:48 +03:00
Michael Schroeder 518633d26a Disallow ternary operator with different types on the rhs
I.e. 1 ? 2 : "foo" is no longer allowed
2019-09-24 14:35:48 +03:00
Michael Schroeder 803de56a44 Add boolifyValue helper and use it in rpmExprBool
No functual changes.
2019-09-24 14:35:48 +03:00
Panu Matilainen c5909bd869 Codify built-in macro argument acceptance
Built-in macros either take arguments via %{foo:...} or don't, raise
errors on unexpected and missing arguments.
2019-09-24 14:28:59 +03:00
Michael Schroeder 765fa386bd Free memory leak in unary op handling
Also reduce the number of alloc/free calls by adding ValueSetXXX()
variants that change a value.
2019-09-23 14:30:10 +03:00
Michael Schroeder 796104e68a Implement short-circuit for logical and ternary operators 2019-09-23 12:49:21 +03:00
Michael Schroeder cb4e5e755a Add support for primary expansion to the expression parser
And also wire it to %[ ... ] as new syntax to expand expressions.

We'll add short-circuit support in the next commit.
2019-09-23 12:49:21 +03:00
Michael Schroeder bebd15081a Add findMacroEnd() function to find the end of a macro call
Refactor expandMacro to use this function. We'll also make use
of it in the expression parser in a future commit.
2019-09-23 12:49:21 +03:00
Panu Matilainen f008f8c70a Ensure expression syntax errors get at least a generic error message 2019-09-20 13:04:32 +03:00
Panu Matilainen 968c53cd3d Always check for rdToken() return codes in expression parsing 2019-09-20 13:04:32 +03:00
Panu Matilainen c62a14e746 Fix double-free on ternary operator parsing error
v1 gets double-freed when there's an error parsing the result side
of the ternary operator. Add testcases to go.
2019-09-20 13:04:32 +03:00
Michael Schroeder 8bc90aa797 Support ternary operator in expression parser 2019-09-18 15:06:23 +03:00
Michael Schroeder 79977e91e3 Catch unterminated strings in expression parsing 2019-09-18 15:06:23 +03:00
Michael Schroeder 7659f40cfe Do not expand %{expr:} again after evaluating the expression 2019-09-18 15:06:23 +03:00
Pavlina Moravcova Varekova 715ce7ce2e Trap division by zero in expression parser 2019-09-18 14:20:12 +03:00
Michael Schroeder a079f750f5 Stop expanding strings in the expression parser
The %if lines in the specfile are expanded before the expressions
get evaluated, so the re-expansion of strings is surprising.
It's also not done for integers, which makes it inconsistent.

The original expression parser seems to have been written
without taking the upfront expansion into account, thus the
string expansion and the TOK_IDENTIFIER code.

As I can't imagine anybody using this feature lets just drop it.
2019-09-18 14:18:26 +03:00
Michael Schroeder 54acb0f102 Remove TOK_IDENTIFIER support from expression parsing
Identifier expansion got broken in 2000 when getMacroBody() was
removed from the API (commit 3ad99fcba5).

Nobody seemed to have noticied, so it's safe to say that there is
no one that used it the intented way.

With the bad commit there is an unintended use for it: it is
a way to specify a string without the otherwise needed quotes.

So this commit might break some existing spec files, but cleaning
those up is easy and makes the world a better place.
2019-09-18 13:16:43 +03:00
Panu Matilainen e79d1feb6d Fix type error introduced in commit 1d055ae1df
"state" is a pointer in this function, we don't want a pointer to it.
Fixes garbage getting printed in the error message. Add a testcase too.
2019-09-18 11:15:11 +03:00
Michael Schroeder c9e34b257e Drop slen from builtin macro parser function
The calculation of the slen parameter was not correct, as it didn't
account for already processed characters. The parameter itself was
also not used to limit the passed string, as the code assumed zero
termination. Thus we can as well simplify the code by using strlen()
on the passed string.
2019-09-18 11:11:31 +03:00
Michael Schroeder 1d055ae1df Print an error for expressions with missing operands
Expressions like '5 +' did not print an error message before.
2019-09-18 09:40:35 +03:00
Michael Schroeder 037106ecc8 Support libgrypt as crypto library 2019-09-10 09:55:41 +03:00
Pavlina Moravcova Varekova 076e3ea2a3 Disable marker on multiline expression error messages
If an expression multiline like:
    %{expr: 0 || 0 ||
            0 || 0 |o| 0 ||
            0 || 0 || 0 }
then it is better not to support marker pointing to the exact
place of the error.
2019-09-09 16:11:24 +03:00
Panu Matilainen 3a6a7cf691 Add %{expr:...} macro for parsing expressions
Supports the same expressions as spec %if conditions because, well,
it's the same expression parser. Only this returns the result as
a string instead of a boolean.
2019-08-21 15:39:40 +03:00
Panu Matilainen 4fc0370c56 Improve expression error messages
The error messages created by the parser have been truly lame and with
zero context. No to mention typos such as multiple instances of "suported".
Add an error reporting helper which uses the parse state to report not
only the expression where the error occurred but also pinpoints the exact
character in the cases where we easily can, eg:

	error: syntax error in expression:  (1 && 0)) + 5
	error:                                      ^
2019-08-21 15:39:40 +03:00