Skip to content

Commit b56d093

Browse files
P-E-PCohenArthur
authored andcommitted
gccrs: lex: Prevent directories in RAIIFile
RAIIFile constructor was accepting directory filename. This lead to unattended directory opening in some part of the code (load_file_bytes) wich resulted in ice. Since RAIIFile are used for the lexer, removing the ability to open directories with RAIIFile fixes those issues and prevent future mistakes. gcc/rust/ChangeLog: * lex/rust-lex.h: Add file type check. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
1 parent e81f5be commit b56d093

1 file changed

Lines changed: 26 additions & 2 deletions

File tree

gcc/rust/lex/rust-lex.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,37 @@ struct RAIIFile
3838
fclose (file);
3939
}
4040

41+
static bool allowed_filetype (const struct stat &statbuf)
42+
{
43+
// The file could be either
44+
// - a regular file
45+
// - a char device (/dev/null...)
46+
return S_ISREG (statbuf.st_mode) || S_ISCHR (statbuf.st_mode);
47+
}
48+
4149
public:
4250
RAIIFile (const char *filename) : filename (filename)
4351
{
4452
if (strcmp (filename, "-") == 0)
45-
file = stdin;
53+
{
54+
file = stdin;
55+
}
4656
else
47-
file = fopen (filename, "r");
57+
{
58+
struct stat statbuf;
59+
if (!(file = fopen (filename, "r")))
60+
{
61+
return;
62+
}
63+
64+
if (-1 == fstat (fileno (file), &statbuf)
65+
|| !allowed_filetype (statbuf))
66+
{
67+
fclose (file);
68+
file = nullptr;
69+
errno = EISDIR;
70+
}
71+
}
4872
}
4973

5074
/**

0 commit comments

Comments
 (0)