]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
choom: fix negative adjust score usage
authorKarel Zak <kzak@redhat.com>
Mon, 10 Dec 2018 13:26:04 +0000 (14:26 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 10 Dec 2018 13:26:04 +0000 (14:26 +0100)
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 <kzak@redhat.com>
include/path.h
lib/path.c
sys-utils/choom.c

index b34aa366ef051df80fb15cc188dcde1908c23b83..8dadef1d049d60fc8515ca0abf0c08700553694c 100644 (file)
@@ -110,6 +110,7 @@ int ul_path_write_string(struct path_cxt *pc, const char *str, const char *path)
 int ul_path_writef_string(struct path_cxt *pc, const char *str, const char *path, ...)
                                __attribute__ ((__format__ (__printf__, 3, 4)));
 
+int ul_path_write_s64(struct path_cxt *pc, int64_t num, const char *path);
 int ul_path_write_u64(struct path_cxt *pc, uint64_t num, const char *path);
 int ul_path_writef_u64(struct path_cxt *pc, uint64_t num, const char *path, ...)
                                __attribute__ ((__format__ (__printf__, 3, 4)));
index d36fe41e8d9f68d0c09a7168b842cf5b35fc2f3b..9dfdc94f86bbfd4b504b499d95def5e9ea5c40ea 100644 (file)
@@ -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))];
index 6895aef4332af2aa488c4e8ea69620d41e72a9c6..eff95b6bf718e75af008f63bb7e4142695670a5a 100644 (file)
@@ -74,7 +74,7 @@ static int get_score_adj(struct path_cxt *pc)
 
 static int set_score_adj(struct path_cxt *pc, int adj)
 {
-       return ul_path_write_u64(pc, adj, "oom_score_adj");
+       return ul_path_write_s64(pc, adj, "oom_score_adj");
 }
 
 int main(int argc, char **argv)