Skip to content

Commit c037889

Browse files
committed
Feat: Check for existence before opening file
1 parent b4f16a2 commit c037889

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

kernel/excel.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ PHP_METHOD(vtiful_xls, fileName)
295295
php_stat(ZSTR_VAL(Z_STR_P(dir_path)), strlen(ZSTR_VAL(Z_STR_P(dir_path))), FS_IS_DIR, &dir_exists);
296296

297297
if (Z_TYPE(dir_exists) == IS_FALSE) {
298+
zval_ptr_dtor(&dir_exists);
298299
zend_throw_exception(vtiful_exception_ce, "Configure 'path' directory does not exist", 121);
299300
}
300301

kernel/read.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "xlswriter.h"
1414
#include "ext/date/php_date.h"
1515
#include "ext/standard/php_math.h"
16+
#include "ext/standard/php_filestat.h"
1617

1718
/* {{{ */
1819
xlsxioreader file_open(const char *directory, const char *file_name) {
@@ -23,13 +24,23 @@ xlsxioreader file_open(const char *directory, const char *file_name) {
2324
strcat(path, "/");
2425
strcat(path, file_name);
2526

27+
zval file_exists;
28+
php_stat(path, strlen(path), FS_IS_FILE, &file_exists);
29+
30+
if (Z_TYPE(file_exists) == IS_FALSE) {
31+
zval_ptr_dtor(&file_exists);
32+
zend_throw_exception(vtiful_exception_ce, "File not found, please check the path in the config or file name", 121);
33+
}
34+
2635
if ((file = xlsxioread_open(path)) == NULL) {
2736
efree(path);
37+
zval_ptr_dtor(&file_exists);
2838
zend_throw_exception(vtiful_exception_ce, "Failed to open file", 100);
2939
return NULL;
3040
}
3141

3242
efree(path);
43+
zval_ptr_dtor(&file_exists);
3344
return file;
3445
}
3546
/* }}} */

0 commit comments

Comments
 (0)