Skip to content

Commit

Permalink
lib/limits.c: setrlimit_value(): Reimplement in terms of getnum()
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro Colomar <alx@kernel.org>
  • Loading branch information
alejandro-colomar committed Jan 16, 2024
1 parent a1d6452 commit 69b1fca
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions lib/limits.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <sys/resource.h>

#include "atoi/getlong.h"
#include "atoi/getnum.h"
#include "memzero.h"


Expand All @@ -49,28 +50,21 @@ static int setrlimit_value (unsigned int resource,
const char *value,
unsigned int multiplier)
{
char *end;
long l;
rlim_t limit;
rlim_t l, limit;
struct rlimit rlim;

/* The "-" is special, not belonging to a strange negative limit.
* It is infinity, in a controlled way.
*/
if ('-' == value[0]) {
limit = RLIM_INFINITY;
}
else {
/* We cannot use getl() here because it fails when there
* is more to the value than just this number!
* Also, we are limited to base 10 here (hex numbers will not
* work with the limit string parser as is anyway)
*/
errno = 0;
l = strtol(value, &end, 10);

if (value == end || errno != 0)
} else {
if (-1 == getnum(rlim_t, value, &l, NULL, 10, 0, type_max(rlim_t))
&& errno != ENOTSUP)
{
return 0; // FIXME: We could instead throw an error, though.
}

if (__builtin_mul_overflow(l, multiplier, &limit)) {
/* FIXME: Again, silent error handling...
Expand Down

0 comments on commit 69b1fca

Please sign in to comment.