Skip to content

Commit

Permalink
Fix fcaseopen.c buffer overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
MegAmi24 committed Nov 14, 2024
1 parent 5d7c98e commit 2527059
Showing 1 changed file with 31 additions and 42 deletions.
73 changes: 31 additions & 42 deletions Nexus/fcaseopen.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,75 +30,68 @@ THE SOFTWARE.
#include <errno.h>
#include <unistd.h>

// r must have strlen(path) + 2 bytes
// r must have strlen(path) + 3 bytes
static int casepath(char const *path, char *r)
{
size_t l = strlen(path);
char *p = (char*)alloca(l + 1);
char *p = (char *)alloca(l + 1);
strcpy(p, path);
size_t rl = 0;

DIR *d;
if (p[0] == '/')
{
if (p[0] == '/') {
d = opendir("/");
p = p + 1;
}
else
{
d = opendir(".");
else {
d = opendir(".");
r[0] = '.';
r[1] = 0;
rl = 1;
rl = 1;
}

int last = 0;
char *c = strsep(&p, "/");
while (c)
{
if (!d)
{
char *c = strsep(&p, "/");
while (c) {
if (!d) {
return 0;
}

if (last)
{

if (last) {
closedir(d);
return 0;
}

r[rl] = '/';
rl += 1;
r[rl] = 0;

struct dirent *e = readdir(d);
while (e)
{
if (strcasecmp(c, e->d_name) == 0)
{
while (e) {
if (strcasecmp(c, e->d_name) == 0) {
strcpy(r + rl, e->d_name);
rl += strlen(e->d_name);

closedir(d);
d = opendir(r);

break;
}

e = readdir(d);
}

if (!e)
{

if (!e) {
strcpy(r + rl, c);
rl += strlen(c);
last = 1;
}

c = strsep(&p, "/");
}

if (d) closedir(d);

if (d)
closedir(d);
return 1;
}
#endif
Expand All @@ -107,11 +100,9 @@ FILE *fcaseopen(char const *path, char const *mode)
{
FILE *f = fopen(path, mode);
#if !defined(_WIN32)
if (!f)
{
char *r = (char*)alloca(strlen(path) + 2);
if (casepath(path, r))
{
if (!f) {
char *r = (char *)alloca(strlen(path) + 3);
if (casepath(path, r)) {
f = fopen(r, mode);
}
}
Expand All @@ -122,13 +113,11 @@ FILE *fcaseopen(char const *path, char const *mode)
void casechdir(char const *path)
{
#if !defined(_WIN32)
char *r = (char*)alloca(strlen(path) + 2);
if (casepath(path, r))
{
char *r = (char *)alloca(strlen(path) + 3);
if (casepath(path, r)) {
chdir(r);
}
else
{
else {
errno = ENOENT;
}
#else
Expand Down

0 comments on commit 2527059

Please sign in to comment.