ucode: check for errors in ftruncate()
[project/udebug.git] / lib-ucode.c
index 8e042e3f99f36b8b4f97b2eae0ae0a3a573259d1..c2ce3b24ea0c3d9763cfa4760d334b7b9df5e04b 100644 (file)
@@ -325,9 +325,13 @@ uc_udebug_pcap_file(uc_vm_t *vm, size_t nargs)
        uc_value_t *args = uc_fn_arg(1);
        int fd = -1;
 
-       if (ucv_type(file) == UC_STRING)
+       if (ucv_type(file) == UC_STRING) {
                fd = open(ucv_string_get(file), O_WRONLY | O_CREAT, 0644);
-       else if (!file)
+               if (ftruncate(fd, 0) < 0) {
+                       close(fd);
+                       return NULL;
+               }
+       } else if (!file)
                fd = STDOUT_FILENO;
 
        return uc_debug_pcap_init(fd, args);