a19bc6
From 38d00b8a0453d38aecb725342ddd89a7c3dcb134 Mon Sep 17 00:00:00 2001
ee0952
From: Michal Sekletar <msekleta@redhat.com>
ee0952
Date: Fri, 18 Nov 2016 14:00:57 +0100
ee0952
Subject: [PATCH] load-fragment: fix parsing values in bytes and prevent
ee0952
 returning -ERANGE incorrectly
ee0952
ee0952
We didn't port our code base to use uint64_t instead of off_t as
ee0952
upstream did. RLIMIT_INIFINITY is -1ULL and if we cast to off_t (64 bit
ee0952
signed int on arches we support) then we get -1 and that is always
ee0952
smaller than correct value returned by parse_size().
ee0952
ee0952
To make code changes as minimal as possible (i.e. not port everything
ee0952
to uint64_t) let's cast off_t to uint64_t and not the other way
ee0952
around.
ee0952
ee0952
RHEL-only
ee0952
ee0952
Resolves: #1396277
ee0952
---
ee0952
 src/core/load-fragment.c  | 2 +-
ee0952
 src/test/test-unit-file.c | 4 ++++
ee0952
 2 files changed, 5 insertions(+), 1 deletion(-)
ee0952
ee0952
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
c62b8e
index 2f6209e053..83b6e7efca 100644
ee0952
--- a/src/core/load-fragment.c
ee0952
+++ b/src/core/load-fragment.c
ee0952
@@ -1105,7 +1105,7 @@ static int rlim_parse_size(const char *val, rlim_t *res) {
ee0952
                 off_t u;
ee0952
 
ee0952
                 r = parse_size(val, 1024, &u);
ee0952
-                if (r >= 0 && u >= (off_t) RLIM_INFINITY)
ee0952
+                if (r >= 0 && (uint64_t) u >= RLIM_INFINITY)
ee0952
                         r = -ERANGE;
ee0952
                 if (r == 0)
ee0952
                         *res = (rlim_t) u;
ee0952
diff --git a/src/test/test-unit-file.c b/src/test/test-unit-file.c
c62b8e
index 8acf071ff3..0384305051 100644
ee0952
--- a/src/test/test-unit-file.c
ee0952
+++ b/src/test/test-unit-file.c
ee0952
@@ -554,6 +554,10 @@ static void test_config_parse_rlimit(void) {
ee0952
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == 55);
ee0952
         assert_se(rl[RLIMIT_NOFILE]->rlim_cur == rl[RLIMIT_NOFILE]->rlim_max);
ee0952
 
ee0952
+        assert_se(config_parse_bytes_limit(NULL, "fake", 1, "section", 1, "LimitSTACK", RLIMIT_STACK, "55", rl, NULL) >= 0);
ee0952
+        assert_se(rl[RLIMIT_STACK]);
ee0952
+        assert_se(rl[RLIMIT_STACK]->rlim_cur == 55);
ee0952
+        assert_se(rl[RLIMIT_STACK]->rlim_cur == rl[RLIMIT_STACK]->rlim_max);
ee0952
 
ee0952
         assert_se(config_parse_limit(NULL, "fake", 1, "section", 1, "LimitNOFILE", RLIMIT_NOFILE, "55:66", rl, NULL) >= 0);
ee0952
         assert_se(rl[RLIMIT_NOFILE]);