Blob Blame History Raw
From 9bcc23ef7cd264d67176037113d11d1ea24195a9 Mon Sep 17 00:00:00 2001
From: Lennart Poettering <lennart@poettering.net>
Date: Tue, 31 Jan 2012 20:53:34 +0100
Subject: [PATCH] load-fragment: properly parse size values denoted in bytes

(cherry picked from commit 9ba1a1598549148fdc9bd7e1b6b7c3b12b2ea958)

Conflicts:

	src/journal/journald-gperf.gperf
---
 src/conf-parser.c                |   39 ++++++++++++++++++++++++++++++++-----
 src/conf-parser.h                |    3 +-
 src/load-fragment-gperf.gperf.m4 |    6 ++--
 src/load-fragment.c              |    2 +-
 4 files changed, 39 insertions(+), 11 deletions(-)

diff --git a/src/conf-parser.c b/src/conf-parser.c
index a71dcd0..af34378 100644
--- a/src/conf-parser.c
+++ b/src/conf-parser.c
@@ -454,7 +454,7 @@ int config_parse_unsigned(
         return 0;
 }
 
-int config_parse_size(
+int config_parse_bytes_size(
                 const char *filename,
                 unsigned line,
                 const char *section,
@@ -465,20 +465,47 @@ int config_parse_size(
                 void *userdata) {
 
         size_t *sz = data;
-        unsigned u;
-        int r;
+        off_t o;
 
         assert(filename);
         assert(lvalue);
         assert(rvalue);
         assert(data);
 
-        if ((r = safe_atou(rvalue, &u)) < 0) {
-                log_error("[%s:%u] Failed to parse numeric value, ignoring: %s", filename, line, rvalue);
+        if (parse_bytes(rvalue, &o) < 0 || (off_t) (size_t) o != o) {
+                log_error("[%s:%u] Failed to parse byte value, ignoring: %s", filename, line, rvalue);
+                return 0;
+        }
+
+        *sz = (size_t) o;
+        return 0;
+}
+
+
+int config_parse_bytes_off(
+                const char *filename,
+                unsigned line,
+                const char *section,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+
+        off_t *bytes = data;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        assert_cc(sizeof(off_t) == sizeof(uint64_t));
+
+        if (parse_bytes(rvalue, bytes) < 0) {
+                log_error("[%s:%u] Failed to parse bytes value, ignoring: %s", filename, line, rvalue);
                 return 0;
         }
 
-        *sz = (size_t) u;
         return 0;
 }
 
diff --git a/src/conf-parser.h b/src/conf-parser.h
index cbb4235..2183dfd 100644
--- a/src/conf-parser.h
+++ b/src/conf-parser.h
@@ -93,7 +93,8 @@ int config_parse_int(const char *filename, unsigned line, const char *section, c
 int config_parse_unsigned(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_long(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_uint64(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
-int config_parse_size(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_bytes_size(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_bytes_off(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_bool(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_path(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
diff --git a/src/load-fragment-gperf.gperf.m4 b/src/load-fragment-gperf.gperf.m4
index 11e6324..6c764cb 100644
--- a/src/load-fragment-gperf.gperf.m4
+++ b/src/load-fragment-gperf.gperf.m4
@@ -170,12 +170,12 @@ Socket.Accept,                   config_parse_bool,                  0,
 Socket.MaxConnections,           config_parse_unsigned,              0,                             offsetof(Socket, max_connections)
 Socket.KeepAlive,                config_parse_bool,                  0,                             offsetof(Socket, keep_alive)
 Socket.Priority,                 config_parse_int,                   0,                             offsetof(Socket, priority)
-Socket.ReceiveBuffer,            config_parse_size,                  0,                             offsetof(Socket, receive_buffer)
-Socket.SendBuffer,               config_parse_size,                  0,                             offsetof(Socket, send_buffer)
+Socket.ReceiveBuffer,            config_parse_bytes_size,            0,                             offsetof(Socket, receive_buffer)
+Socket.SendBuffer,               config_parse_bytes_size,            0,                             offsetof(Socket, send_buffer)
 Socket.IPTOS,                    config_parse_ip_tos,                0,                             offsetof(Socket, ip_tos)
 Socket.IPTTL,                    config_parse_int,                   0,                             offsetof(Socket, ip_ttl)
 Socket.Mark,                     config_parse_int,                   0,                             offsetof(Socket, mark)
-Socket.PipeSize,                 config_parse_size,                  0,                             offsetof(Socket, pipe_size)
+Socket.PipeSize,                 config_parse_bytes_size,            0,                             offsetof(Socket, pipe_size)
 Socket.FreeBind,                 config_parse_bool,                  0,                             offsetof(Socket, free_bind)
 Socket.Transparent,              config_parse_bool,                  0,                             offsetof(Socket, transparent)
 Socket.Broadcast,                config_parse_bool,                  0,                             offsetof(Socket, broadcast)
diff --git a/src/load-fragment.c b/src/load-fragment.c
index ef5d192..ba28398 100644
--- a/src/load-fragment.c
+++ b/src/load-fragment.c
@@ -2337,7 +2337,7 @@ void unit_dump_config_items(FILE *f) {
         } table[] = {
                 { config_parse_int,                   "INTEGER" },
                 { config_parse_unsigned,              "UNSIGNED" },
-                { config_parse_size,                  "SIZE" },
+                { config_parse_bytes_size,            "SIZE" },
                 { config_parse_bool,                  "BOOLEAN" },
                 { config_parse_string,                "STRING" },
                 { config_parse_path,                  "PATH" },