Compare commits

..

2 Commits

Author SHA1 Message Date
165352cdca
Bounds checking :) 2022-12-30 11:13:55 +01:00
d018d128a0
Optionally ignore unsupported types instead of panicking 2022-12-30 11:13:21 +01:00
3 changed files with 13 additions and 1 deletions

View File

@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.8..3.22)
project(minitar LANGUAGES C VERSION 1.3.0) project(minitar LANGUAGES C VERSION 1.3.0)
option(MINITAR_IGNORE_UNSUPPORTED_TYPES "Skip past entries that have unsupported types instead of panicking" OFF)
set(SOURCES set(SOURCES
src/tar.c src/tar.c
src/util.c src/util.c
@ -9,6 +11,10 @@ set(SOURCES
add_library(minitar STATIC ${SOURCES}) add_library(minitar STATIC ${SOURCES})
if(MINITAR_IGNORE_UNSUPPORTED_TYPES)
target_compile_definitions(minitar PRIVATE MINITAR_IGNORE_UNSUPPORTED_TYPES)
endif()
target_include_directories(minitar PUBLIC ${CMAKE_CURRENT_LIST_DIR}) # for minitar.h target_include_directories(minitar PUBLIC ${CMAKE_CURRENT_LIST_DIR}) # for minitar.h
set_target_properties(minitar PROPERTIES OUTPUT_NAME mtar) set_target_properties(minitar PROPERTIES OUTPUT_NAME mtar)

View File

@ -118,7 +118,7 @@ This enum lists all supported file types:
`MTAR_DIRECTORY`: Directories `MTAR_DIRECTORY`: Directories
Other file types supported in tar archives, such as block/character devices, FIFOs, or symlinks, are not supported and minitar will throw an error when encountering one of them. Other file types supported in tar archives, such as block/character devices, FIFOs, or symlinks, are not supported and minitar will throw an error when encountering one of them. This behavior can be controlled by passing `-DMINITAR_IGNORE_UNSUPPORTED_TYPES=ON` to CMake when configuring, which will make minitar silently ignore such entries instead of panicking.
### minitar_entry_metadata ### minitar_entry_metadata
`struct minitar_entry_metadata` `struct minitar_entry_metadata`

View File

@ -1,5 +1,6 @@
#include "minitar.h" #include "minitar.h"
#include "tar.h" #include "tar.h"
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -93,6 +94,7 @@ static size_t minitar_align_down_to_block_size(size_t size)
static char* minitar_static_dup(const char* str, size_t size) static char* minitar_static_dup(const char* str, size_t size)
{ {
static char result[1024]; static char result[1024];
assert(size < 1024);
memcpy(result, str, size); memcpy(result, str, size);
result[size] = 0; result[size] = 0;
return result; return result;
@ -168,9 +170,13 @@ void minitar_parse_metadata_from_tar_header(const struct tar_header* hdr, struct
int minitar_validate_header(const struct tar_header* hdr) int minitar_validate_header(const struct tar_header* hdr)
{ {
#ifdef MINITAR_IGNORE_UNSUPPORTED_TYPES
if (hdr->typeflag != '\0' && hdr->typeflag != '0' && hdr->typeflag != '5') return 0;
#else
if (hdr->typeflag != '\0' && hdr->typeflag != '0' && hdr->typeflag != '1' && hdr->typeflag != '2' && if (hdr->typeflag != '\0' && hdr->typeflag != '0' && hdr->typeflag != '1' && hdr->typeflag != '2' &&
hdr->typeflag != '3' && hdr->typeflag != '4' && hdr->typeflag != '5' && hdr->typeflag != '6') hdr->typeflag != '3' && hdr->typeflag != '4' && hdr->typeflag != '5' && hdr->typeflag != '6')
return 0; return 0;
#endif
return !strncmp(hdr->magic, "ustar", 5); return !strncmp(hdr->magic, "ustar", 5);
} }