diff options
Diffstat (limited to 'src/Common/libzip/zip_source_file_stdio_named.c')
-rw-r--r-- | src/Common/libzip/zip_source_file_stdio_named.c | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/src/Common/libzip/zip_source_file_stdio_named.c b/src/Common/libzip/zip_source_file_stdio_named.c index 92ae344a..51e06a91 100644 --- a/src/Common/libzip/zip_source_file_stdio_named.c +++ b/src/Common/libzip/zip_source_file_stdio_named.c @@ -66,6 +66,7 @@ static zip_int64_t _zip_stdio_op_remove(zip_source_file_context_t *ctx); static void _zip_stdio_op_rollback_write(zip_source_file_context_t *ctx); static char *_zip_stdio_op_strdup(zip_source_file_context_t *ctx, const char *string); static zip_int64_t _zip_stdio_op_write(zip_source_file_context_t *ctx, const void *data, zip_uint64_t len); +static FILE *_zip_fopen_close_on_exec(const char *name, bool writeable); /* clang-format off */ static zip_source_file_operations_t ops_stdio_named = { @@ -300,11 +301,12 @@ static int create_temp_file(zip_source_file_context_t *ctx, bool create_file) { mode = -1; } - if ((temp = (char *)malloc(strlen(ctx->fname) + 13)) == NULL) { + size_t temp_size = strlen(ctx->fname) + 13; + if ((temp = (char *)malloc(temp_size)) == NULL) { zip_error_set(&ctx->error, ZIP_ER_MEMORY, 0); return -1; } - sprintf(temp, "%s.XXXXXX.part", ctx->fname); + snprintf_s(temp, temp_size, "%s.XXXXXX.part", ctx->fname); end = temp + strlen(temp) - 5; start = end - 6; @@ -359,3 +361,32 @@ static int create_temp_file(zip_source_file_context_t *ctx, bool create_file) { return create_file ? fd : 0; } + + +/* + * fopen replacement that sets the close-on-exec flag + * some implementations support an fopen 'e' flag for that, + * but e.g. macOS doesn't. + */ +static FILE *_zip_fopen_close_on_exec(const char *name, bool writeable) { + int fd; + int flags; + FILE *fp; + + flags = O_CLOEXEC; + if (writeable) { + flags |= O_RDWR; + } + else { + flags |= O_RDONLY; + } + + /* mode argument needed on Windows */ + if ((fd = open(name, flags, 0666)) < 0) { + return NULL; + } + if ((fp = fdopen(fd, writeable ? "r+b" : "rb")) == NULL) { + return NULL; + } + return fp; +} |