Skip to content

Commit

Permalink
Make macros::expand_numeric return int64_t
Browse files Browse the repository at this point in the history
While we can't change the C API there is really no reason to add a new
32 bit API. Changing now while it is still internal.

Emit a warning if value doesn't fit into an int for the old API and clip
at INT_MIN and INT_MAX.
  • Loading branch information
ffesti committed Nov 18, 2024
1 parent 3006c84 commit 8fca2a2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
21 changes: 16 additions & 5 deletions rpmio/macro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <vector>
#include <stack>
#include <cinttypes>

#include <stdarg.h>
#include <errno.h>
Expand Down Expand Up @@ -1992,10 +1993,20 @@ rpmExpand(const char *arg, ...)
int
rpmExpandNumeric(const char *arg)
{
int res;
if (arg == NULL)
return 0;
auto [ ign, val ] = macros().expand_numeric(arg);
return val;
if (val > INT_MAX) {
rpmlog(RPMLOG_WARNING, _("Macro value too big for int: %" PRIu64 " Using %i instead.\n"), val, INT_MAX);
res = INT_MAX;
} else if (val < INT_MIN) {
rpmlog(RPMLOG_WARNING, _("Macro value too small for int: %" PRIu64 " Using %i instead.\n"), val, INT_MIN);
res = INT_MIN;
} else {
res = val;
}
return res;
}

void macros::clear()
Expand Down Expand Up @@ -2063,12 +2074,12 @@ macros::expand_this(const char *n, ARGV_const_t args, int flags)
return std::make_pair(rc, target);
}

std::pair<int,int>
std::pair<int,int64_t>
macros::expand_numeric(const std::string & src, int flags)
{
auto [ exrc, s ] = macros().expand(src, flags);
const char *val = s.c_str();
int rc = 0;
int64_t rc = 0;
if (!(val && *val != '%'))
rc = 0;
else if (*val == 'Y' || *val == 'y')
Expand All @@ -2077,14 +2088,14 @@ macros::expand_numeric(const std::string & src, int flags)
rc = 0;
else {
char *end;
rc = strtol(val, &end, 0);
rc = strtoll(val, &end, 0);
if (!(end && *end == '\0'))
rc = 0;
}
return std::make_pair(exrc, rc);
}

std::pair<int,int>
std::pair<int,int64_t>
macros::expand_numeric(const std::initializer_list<std::string> & src, int flags)
{
string buf;
Expand Down
4 changes: 2 additions & 2 deletions rpmio/rpmmacro_internal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public:
std::pair<int,std::string> expand_this(const char *n, ARGV_const_t args,
int flags = 0);
/* Expand macros to numeric value, with a return code (rc, number) */
std::pair<int,int> expand_numeric(const std::string & src, int flags = 0);
std::pair<int,int> expand_numeric(const std::initializer_list<std::string> & src,
std::pair<int,int64_t> expand_numeric(const std::string & src, int flags = 0);
std::pair<int,int64_t> expand_numeric(const std::initializer_list<std::string> & src,
int flags = 0);
void init(const char *macrofiles);
bool is_defined(const char *n);
Expand Down

0 comments on commit 8fca2a2

Please sign in to comment.