Parcourir la source

[bugfix] 32b failure due to uninitialized var

B Thibault il y a 9 ans
Parent
commit
f5ebf651ed
3 fichiers modifiés avec 21 ajouts et 1 suppressions
  1. 6 1
      src/ovr_syscall/ovr_open.c
  2. 10 0
      src/pathutil.c
  3. 5 0
      src/sandbox.h

+ 6 - 1
src/ovr_syscall/ovr_open.c

@@ -28,6 +28,7 @@ int ovr_open(struct s_sandboxenv *env)
 {
     char *pathname = getMem(env, (size_t) env->syscall_args[0], NULL);
     int flags = (int) env->syscall_args[1];
+    size_t fsize;
     mode_t mode = (mode_t) env->syscall_args[2];
     int ro = !(flags & (O_RDWR | O_WRONLY | O_TRUNC));
     t_fileinfo *fileentry;
@@ -36,7 +37,7 @@ int ovr_open(struct s_sandboxenv *env)
     if (*pathname != '/')
         find_fullpath(&pathname, 1);
     fileentry = get_fileinfo(env, pathname);
-    if (!fileentry && ro && (flags & O_CREAT) && !file_exists(pathname))
+    if (!fileentry && ro && (flags & O_CREAT) && !file_exists_getFilesize(pathname, &fsize))
         ro = 0;
     if (!fileentry && ro)
     {
@@ -52,6 +53,10 @@ int ovr_open(struct s_sandboxenv *env)
     fd->fd = (int) env->syscall_no.syscall_return;
     fd->flags = flags;
     fd->mod = mode;
+    if (flags & O_APPEND)
+        fd->offset = fsize;
+    else
+        fd->offset = 0;
     sllist_pushback(fileentry->fds, fd);
 
     free(pathname);

+ 10 - 0
src/pathutil.c

@@ -54,6 +54,16 @@ t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd)
     return sllist_at(env->filetable, i);
 }
 
+int file_exists_getFilesize(const char*path, size_t *size)
+{
+    struct stat buf;
+
+    if (stat(path, &buf) == -1 && errno == ENOENT)
+        return 0;
+    *size = buf.st_size;
+    return 1;
+}
+
 int file_exists(const char*path)
 {
     struct stat buf;

+ 5 - 0
src/sandbox.h

@@ -97,6 +97,11 @@ t_fileinfo *get_fileinfo(const struct s_sandboxenv *env, const char *filename);
 t_fileinfo *get_fileinfo_fd(const struct s_sandboxenv *env, const int fd);
 unsigned long long int hash_path(const char *filename);
 int file_exists(const char *filename);
+/**
+ * Check if file exists, and return its size in size
+ * @return 1 if file exists, 0 otherwise
+**/
+int file_exists_getFilesize(const char *filename, size_t *size);
 int fileinfo_compare_tfd(const void *a, const void *b);
 int release_file(void **t_fileinfo, void *env);