So basically I'm given a path to a directory and should return index.html
if it exists or index.php
if that file exists. The thing is that I don't know how to debug this function since I'm only implementing this function for a much larger program.
所以基本上我给了一个目录的路径,如果它存在则应该返回index.html或者如果该文件存在则返回index.php。问题是我不知道如何调试这个函数,因为我只是为一个更大的程序实现了这个函数。
char *indexes(const char *path) {
char *newPath = malloc(sizeof(char) * strlen(path));
strcpy(newPath, path);
if (access(path, F_OK)) {
if (access("index.html", F_OK)) {
strcat(newPath, "/index.html");
} else
if (access("index.php", F_OK)) {
strcat(newPath, "/index.php");
} else {
return NULL;
}
} else {
return NULL;
}
return newPath;
}
Does this look correct? When I run my program I get an error of 501 Not Implemented
这看起来是否正确?当我运行我的程序时,我收到501未实现的错误
1 个解决方案
#1
2
Since you are going to concatenate path
and /index.html
into the newly allocated newPath
, you must allocate it to at least the sum of lengths plus 1 extra byte for the null terminator:
由于您要将路径和/index.html连接到新分配的newPath,因此必须将其分配给至少长度之和加上空终止符的1个额外字节:
strlen(path) + strlen("/index.html") + 1;
"/index.php"
is shorter than the other string, so the buffer can handle the alternative concatenation.
“/index.php”比其他字符串短,因此缓冲区可以处理备用连接。
The current code causes a buffer overflow, invoking undefined behavior, potentially causing the observed behavior.
当前代码导致缓冲区溢出,调用未定义的行为,可能导致观察到的行为。
Note that your code cannot work as written: you should concatenate before checking access, otherwise you check access in the wrong directory. You should also free(newPath);
in case neither index file is found.
请注意,您的代码无法按照写入的方式工作:您应该在检查访问权限之前进行连接,否则您将检查错误目录中的访问权限。你也应该免费(newPath);如果没有找到索引文件。
Here is a corrected version:
这是一个更正版本:
char *indexes(const char *path) {
char *newPath = malloc(strlen(path) + strlen("/index.html") + 1);
if (newPath) {
strcpy(newPath, path);
strcat(newPath, "/index.html");
if (access(newPath, F_OK)) {
return newPath;
}
strcpy(newPath, path);
strcat(newPath, "/index.php");
if (access(newPath, F_OK)) {
return newPath;
}
free(newPath);
}
return NULL;
}
#1
2
Since you are going to concatenate path
and /index.html
into the newly allocated newPath
, you must allocate it to at least the sum of lengths plus 1 extra byte for the null terminator:
由于您要将路径和/index.html连接到新分配的newPath,因此必须将其分配给至少长度之和加上空终止符的1个额外字节:
strlen(path) + strlen("/index.html") + 1;
"/index.php"
is shorter than the other string, so the buffer can handle the alternative concatenation.
“/index.php”比其他字符串短,因此缓冲区可以处理备用连接。
The current code causes a buffer overflow, invoking undefined behavior, potentially causing the observed behavior.
当前代码导致缓冲区溢出,调用未定义的行为,可能导致观察到的行为。
Note that your code cannot work as written: you should concatenate before checking access, otherwise you check access in the wrong directory. You should also free(newPath);
in case neither index file is found.
请注意,您的代码无法按照写入的方式工作:您应该在检查访问权限之前进行连接,否则您将检查错误目录中的访问权限。你也应该免费(newPath);如果没有找到索引文件。
Here is a corrected version:
这是一个更正版本:
char *indexes(const char *path) {
char *newPath = malloc(strlen(path) + strlen("/index.html") + 1);
if (newPath) {
strcpy(newPath, path);
strcat(newPath, "/index.html");
if (access(newPath, F_OK)) {
return newPath;
}
strcpy(newPath, path);
strcat(newPath, "/index.php");
if (access(newPath, F_OK)) {
return newPath;
}
free(newPath);
}
return NULL;
}