aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-02-11 17:41:33 +0100
committerdec05eba <dec05eba@protonmail.com>2022-02-11 17:41:33 +0100
commit6cad09ec9c801e90d41f53ebcd673ef89050cc86 (patch)
treea57b30c824c663ed7730a6df6f2478a4a5b500e8 /src
parent8d1532abb6f7441e00ffbc7ed4d0231e5023e19a (diff)
Check if file to read is really a fileHEADmaster
Diffstat (limited to 'src')
-rw-r--r--src/program.c6
-rw-r--r--src/std/file.c28
-rw-r--r--src/std/hash_map.c3
3 files changed, 22 insertions, 15 deletions
diff --git a/src/program.c b/src/program.c
index e53eb6b..e163b9b 100644
--- a/src/program.c
+++ b/src/program.c
@@ -871,12 +871,14 @@ int amal_program_save(amal_program *self, const char *filepath) {
int err = 0;
FILE *file = fopen(filepath, "wb");
if(!file) {
- err = -errno;
+ perror(filepath);
+ err = -1;
goto cleanup;
}
if(fwrite(self->data.data, 1, self->data.size, file) != self->data.size) {
- err = -errno;
+ fprintf(stderr, "Failed to write %zu bytes to %s\n", self->data.size, filepath);
+ err = -1;
goto cleanup;
}
diff --git a/src/std/file.c b/src/std/file.c
index 4542aba..d486843 100644
--- a/src/std/file.c
+++ b/src/std/file.c
@@ -28,10 +28,8 @@ static int scan_dir_recursive_internal(char *dir_path, int path_length, scan_dir
path_length_start = path_length;
if((dir = opendir(dir_path)) == NULL) {
- int error;
- error = errno;
amal_log_perror("scan_dir_recursive");
- return error;
+ return -1;
}
while((entry = readdir(dir)) != NULL) {
@@ -178,9 +176,16 @@ int read_whole_file(const char *filepath, Buffer *data_result) {
file = fopen(filepath, "rb");
if(!file) {
- int error = errno;
- amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(error));
- return error;
+ amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(errno));
+ return -1;
+ }
+
+ int fd = fileno(file);
+ struct stat s;
+ if(fstat(fd, &s) == -1 || !S_ISREG(s.st_mode)) {
+ amal_log_error("read_whole_file: %s failed, error: attempted to read a non-file", filepath);
+ fclose(file);
+ return -1;
}
result = fseek(file, 0, SEEK_END);
@@ -191,9 +196,8 @@ int read_whole_file(const char *filepath, Buffer *data_result) {
size = ftell(file);
if(size == -1) {
- int error = errno;
- result = error;
- amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(error));
+ result = -1;
+ amal_log_error("read_whole_file: %s failed, error: %s", filepath, strerror(errno));
goto cleanup;
}
@@ -223,10 +227,8 @@ int read_whole_file(const char *filepath, Buffer *data_result) {
int file_get_canonical_path(const char *filepath, char **result_path, usize *result_path_size) {
char result_path_tmp[PATH_MAX];
if(!realpath(filepath, result_path_tmp)) {
- int ret;
- ret = errno;
- amal_log_error("file_get_canonical_path: realpath failed for path %s, error: %s", filepath, strerror(ret));
- return ret;
+ amal_log_error("file_get_canonical_path: realpath failed for path %s, error: %s", filepath, strerror(errno));
+ return -1;
}
*result_path_size = strnlen(result_path_tmp, PATH_MAX);
return_if_error(am_malloc(*result_path_size + 1, (void**)result_path));
diff --git a/src/std/hash_map.c b/src/std/hash_map.c
index b4bd4c1..4bf55c6 100644
--- a/src/std/hash_map.c
+++ b/src/std/hash_map.c
@@ -224,6 +224,9 @@ bool hash_map_get_ref(HashMap *self, BufferView key, void **value) {
HashMapBucketNode *bucket_node;
bucket_size = buffer_get_size(&self->buckets, HashMapBucket);
+ if(bucket_size == 0)
+ return bool_false;
+
hash = self->hash_func((const u8*)key.data, key.size);
bucket_index = hash % bucket_size;