From 8b4396763fa313733e45a83b8293c9d230558e85 Mon Sep 17 00:00:00 2001 From: wjc <2821296948@qq.com> Date: Mon, 12 Oct 2026 20:54:52 +0800 Subject: [PATCH] fix: bound lexicographic permutation input Co-Authored-By: chaogeshuodiannao --- misc/lexicographic_permutations.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/misc/lexicographic_permutations.c b/misc/lexicographic_permutations.c index 10f8cedff7..145aa8d058 100644 --- a/misc/lexicographic_permutations.c +++ b/misc/lexicographic_permutations.c @@ -57,8 +57,20 @@ int main() perror("Input number out of range: >0 and <1000\n"); return -1; } - char *str = (char *)malloc(n * sizeof(char)); - scanf("%s", str); + char *str = malloc((n + 1) * sizeof(char)); + if (str == NULL) + { + return -1; + } + + char input_format[16]; + snprintf(input_format, sizeof(input_format), "%%%ds", n); + if (scanf(input_format, str) != 1) + { + free(str); + return -1; + } + PrintSortedPermutations(str); free(str); return 0;