Skip to content

Commit af3b7d0

Browse files
Dan Carpenterakpm00
authored andcommitted
selftests/mm: compaction_test: fix off by one in check_compaction()
The "initial_nr_hugepages" variable is unsigned long so it takes up to 20 characters to print, plus 1 more character for the NUL terminator. Unfortunately, this buffer is not quite large enough for the terminator to fit. Also use snprintf() for a belt and suspenders approach. Link: https://lkml.kernel.org/r/87470c06-b45a-4e83-92ff-aac2e7b9c6ba@stanley.mountain Fixes: fb9293b ("selftests/mm: compaction_test: fix bogus test success and reduce probability of OOM-killer invocation") Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org> Cc: Shuah Khan <shuah@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent fd8c35a commit af3b7d0

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

tools/testing/selftests/mm/compaction_test.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,10 @@ int check_compaction(unsigned long mem_free, unsigned long hugepage_size,
8989
int fd, ret = -1;
9090
int compaction_index = 0;
9191
char nr_hugepages[20] = {0};
92-
char init_nr_hugepages[20] = {0};
92+
char init_nr_hugepages[24] = {0};
9393

94-
sprintf(init_nr_hugepages, "%lu", initial_nr_hugepages);
94+
snprintf(init_nr_hugepages, sizeof(init_nr_hugepages),
95+
"%lu", initial_nr_hugepages);
9596

9697
/* We want to test with 80% of available memory. Else, OOM killer comes
9798
in to play */

0 commit comments

Comments
 (0)