[ruby/prism] Stat file first to check directory

https://github.com/ruby/prism/commit/4ed7de537b
This commit is contained in:
Kevin Newton 2024-09-13 15:18:14 -04:00 committed by git
parent f85efc9748
commit ddbd644001
3 changed files with 25 additions and 16 deletions

View File

@ -254,38 +254,45 @@ pm_string_file_init(pm_string_t *string, const char *filepath) {
*string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = (size_t) file_size }; *string = (pm_string_t) { .type = PM_STRING_OWNED, .source = source, .length = (size_t) file_size };
return PM_STRING_INIT_SUCCESS; return PM_STRING_INIT_SUCCESS;
#elif defined(PRISM_HAS_FILESYSTEM) #elif defined(PRISM_HAS_FILESYSTEM)
FILE *file = fopen(filepath, "rb"); // Open the file for reading
if (file == NULL) { int fd = open(filepath, O_RDONLY);
if (fd == -1) {
return PM_STRING_INIT_ERROR_GENERIC; return PM_STRING_INIT_ERROR_GENERIC;
} }
fseek(file, 0, SEEK_END); // Stat the file to get the file size
long file_size = ftell(file); struct stat sb;
if (fstat(fd, &sb) == -1) {
if (file_size == -1) { close(fd);
fclose(file);
return PM_STRING_INIT_ERROR_GENERIC; return PM_STRING_INIT_ERROR_GENERIC;
} }
if (file_size == 0) { // Ensure it is a file and not a directory
fclose(file); if (S_ISDIR(sb.st_mode)) {
close(fd);
return PM_STRING_INIT_ERROR_DIRECTORY;
}
// Check the size to see if it's empty
size_t size = (size_t) sb.st_size;
if (size == 0) {
close(fd);
const uint8_t source[] = ""; const uint8_t source[] = "";
*string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 }; *string = (pm_string_t) { .type = PM_STRING_CONSTANT, .source = source, .length = 0 };
return PM_STRING_INIT_SUCCESS; return PM_STRING_INIT_SUCCESS;
} }
size_t length = (size_t) file_size; size_t length = (size_t) size;
uint8_t *source = xmalloc(length); uint8_t *source = xmalloc(length);
if (source == NULL) { if (source == NULL) {
fclose(file); close(fd);
return PM_STRING_INIT_ERROR_GENERIC; return PM_STRING_INIT_ERROR_GENERIC;
} }
fseek(file, 0, SEEK_SET); long bytes_read = (long) read(fd, source, length);
size_t bytes_read = fread(source, length, 1, file); close(fd);
fclose(file);
if (bytes_read != 1) { if (bytes_read == -1) {
xfree(source); xfree(source);
return PM_STRING_INIT_ERROR_GENERIC; return PM_STRING_INIT_ERROR_GENERIC;
} }

View File

@ -22,6 +22,9 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#elif defined(PRISM_HAS_FILESYSTEM)
#include <fcntl.h>
#include <sys/stat.h>
#endif #endif
/** /**

View File

@ -87,7 +87,6 @@ module Prism
rescue SystemCallError => error rescue SystemCallError => error
end end
return if error.nil? || error.is_a?(Errno::ENOMEM)
assert_kind_of Errno::EISDIR, error assert_kind_of Errno::EISDIR, error
end end