daandemeyer / rpms / systemd

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