gpt4 book ai didi

检查输入字符串是否超过缓冲区限制(崩溃)

转载 作者:太空宇宙 更新时间:2023-11-04 08:04:40 30 4
gpt4 key购买 nike

我正在创建一个函数,它将目录路径作为参数传递,或者如果它留空,则提示用户输入。

我已经设置了我的 PATH_MAX=100if 语句来检查 if ((strlen(folder path) + strlen(file path)) > PATH_MAX) 将要求用户再次输入。

然而,当我检查所有条件是否有效时(设置 PATH_MAX=20),如果 folder path 本身超过了 PATH_MAX,缓冲区由于大小不足而崩溃(L'Buffer is too small' &&0)。

有没有办法事先检查用户是否超过PATH_MAX并通知路径太长,以避免缓冲区崩溃?或者我应该只增加 PATH_MAX 的大小?

代码:

#define PATH_MAX 100
void CreateFiles(char folder[PATH_MAX])
{
char addrbook[PATH_MAX] = "caf-sorted.txt";
char path[PATH_MAX]="";

if ((strlen(folder)<4))
{
//User inputs directory
printf("Enter New Directory\n(!Do not enter filename!)\n");

if (NULL == fgets(path, sizeof path, stdin))
{//check if fgets fails
if (ferror(stdin))
{
folder="";
perror("fgets() failed");
CreateFiles(folder);
return;
}
}
}
else
memcpy(path, folder, strlen(folder));

path[strcspn(path, "\n\r")] = 0;

if (strlen(addrbook) > 0 && '\\' != path[strlen(path) - 1])
{
if (PATH_MAX < strlen(path))
{
errno = EINVAL;
perror("'path' too long");
folder="";
CreateFiles(folder);
return;
}

strcat(path, "\\");
}

if (PATH_MAX < (strlen(path) + strlen(addrbook)))
{
errno = EINVAL;
perror("'path\\filename' too long");
folder="";
CreateFiles(folder);
return;
}
}

最佳答案

您必须考虑终止空字符

if (!(strlen(path) < PATH_MAX))

确保路径中的字符数(不含空字符)始终至少比 PATH_MAX 少一个,这为终止空字符留出空间。

你必须考虑到你使用的每个 C 字符串,因为 strlen(char *string) 总是比你需要存储字符串的空间少一个,如果你希望能够以 null 终止它。

编辑:所以我至少研究了您函数的前几行,并尝试快速重新实现它们。它不漂亮,但它有效:

#include <stdio.h>
#include <string.h>
#include <strings.h>

#define PATH_MAX 100

void create_files (char *folder)
{
char addr_book[] = "caf-sorted.txt";
char path[PATH_MAX];


// Setting all bytes in *path to zero
bzero(path, PATH_MAX);



// If the folder name is too short, we ask for a new one
if (strlen(folder) < 4) {

char c; // This will store our input from stdin, one char at a time



// As long as the supplied path name is too short, we'll keep asking:

while (strlen(path) < 4) {
printf("Please enter a path (no filename!): ");


// We get one character at a time from stdin using getc(...):
// until we encounter a newline

for (int i = 0; (c = getc(stdin)) != '\n'; i++) {

if (i < PATH_MAX - 1) {

// As long as we have space for two more characters
// (the value of c plus a null character after it)
// We'll keep appending c:

path[i] = c;

} else if (i == PATH_MAX - 1) {

// If we get too many characters from stdin, we
// display an error message and reset our path to
// all null characters again, so the outermost loop
// will run again

fprintf(stderr, "Path is too long!\n");
bzero(path, PATH_MAX);

// Notice that we do not have a break statement
// here, we iterate through the input string from
// stdin until we encounter a newline character,
// so we don't have any superfluous characters
// that spill into the beginning ouf our freshly
// reset path string
}
}
}
} else {
// Or, you know, if the programmer specifies a proper value,
// Just do it the easy way and copy that into our path string
// (although this will truncate a folder name that is too long):

strncpy(path, folder, PATH_MAX - 1);
}
}

int main ()
{
create_files("");

return 0;
}

关于检查输入字符串是否超过缓冲区限制(崩溃),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43683696/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com