From 12d3fa4b4648afbd41831eecf2e25fd118b484f5 Mon Sep 17 00:00:00 2001 From: ishaansehgal99 Date: Tue, 28 Jun 2022 23:29:31 +0000 Subject: [PATCH 1/2] String bug in strcmp --- hibernation-setup-tool.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/hibernation-setup-tool.c b/hibernation-setup-tool.c index 0dd7628..dc97810 100644 --- a/hibernation-setup-tool.c +++ b/hibernation-setup-tool.c @@ -258,17 +258,18 @@ static struct swap_file *find_swap_file(size_t needed_size) break; } - if (!strcmp(type, "file")) { + if (!strncmp(type, "file", sizeof("file") - 1)) { char *size = next_field(type); if (!size) log_fatal("Malformed line in /proc/swaps: can't find size column"); - size_t size_as_int = parse_size_or_die(size, ' ', NULL); - if (size_as_int < needed_size) + size_t size_as_int_kb = parse_size_or_die(size, ' ', NULL); + size_t size_as_int_b = size_as_int_kb * 1024; + if (size_as_int_b < needed_size) continue; - out = new_swap_file(filename, size_as_int); + out = new_swap_file(filename, size_as_int_b); break; } } From acf38b49ad95ad1452c8d0327b2cc53df855fc0a Mon Sep 17 00:00:00 2001 From: ishaansehgal99 Date: Mon, 11 Jul 2022 19:52:39 +0000 Subject: [PATCH 2/2] Added swap file name check before getting its size --- hibernation-setup-tool.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/hibernation-setup-tool.c b/hibernation-setup-tool.c index dc97810..ad19866 100644 --- a/hibernation-setup-tool.c +++ b/hibernation-setup-tool.c @@ -251,26 +251,28 @@ static struct swap_file *find_swap_file(size_t needed_size) while (fgets(buffer, sizeof(buffer), swaps)) { char *filename = buffer; - char *type = next_field(filename); + if (!strncmp(filename, swap_file_name, sizeof(swap_file_name) - 1)) { + char *type = next_field(filename); - if (!type) { - log_info("Couldn't get the second field while parsing /proc/swaps"); - break; - } + if (!type) { + log_info("Couldn't get the second field while parsing /proc/swaps"); + break; + } - if (!strncmp(type, "file", sizeof("file") - 1)) { - char *size = next_field(type); + if (!strncmp(type, "file", sizeof("file") - 1)) { + char *size = next_field(type); - if (!size) - log_fatal("Malformed line in /proc/swaps: can't find size column"); + if (!size) + log_fatal("Malformed line in /proc/swaps: can't find size column"); - size_t size_as_int_kb = parse_size_or_die(size, ' ', NULL); - size_t size_as_int_b = size_as_int_kb * 1024; - if (size_as_int_b < needed_size) - continue; + size_t size_as_int_kb = parse_size_or_die(size, ' ', NULL); + size_t size_as_int_b = size_as_int_kb * 1024; + if (size_as_int_b < needed_size) + continue; - out = new_swap_file(filename, size_as_int_b); - break; + out = new_swap_file(filename, size_as_int_b); + break; + } } }