From c455cdb30d212b6488d3e9f2f663fcc1f6ad267c Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 10 Dec 2018 14:26:04 +0100 Subject: choom: fix negative adjust score usage It's really bad idea to use uint64_t (ul_path_write_u64(()) when write signed number. Addresses: https://github.com/karelzak/util-linux/issues/723 Signed-off-by: Karel Zak --- lib/path.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'lib') diff --git a/lib/path.c b/lib/path.c index d36fe41e8..9dfdc94f8 100644 --- a/lib/path.c +++ b/lib/path.c @@ -850,6 +850,28 @@ int ul_path_writef_string(struct path_cxt *pc, const char *str, const char *path return ul_path_write_string(pc, str, p); } +int ul_path_write_s64(struct path_cxt *pc, int64_t num, const char *path) +{ + char buf[sizeof(stringify_value(LLONG_MAX))]; + int rc, errsv; + int fd, len; + + fd = ul_path_open(pc, O_WRONLY|O_CLOEXEC, path); + if (fd < 0) + return -errno; + + len = snprintf(buf, sizeof(buf), "%" PRId64, num); + if (len < 0 || (size_t) len >= sizeof(buf)) + rc = len < 0 ? -errno : -E2BIG; + else + rc = write_all(fd, buf, len); + + errsv = errno; + close(fd); + errno = errsv; + return rc; +} + int ul_path_write_u64(struct path_cxt *pc, uint64_t num, const char *path) { char buf[sizeof(stringify_value(ULLONG_MAX))]; -- cgit v1.2.3-55-g7522