Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C89 support #1616

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ if(POLICY CMP0074)
endif()
#
PROJECT(libarchive C)
set(CMAKE_C_STANDARD 90)
#
SET(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/build/cmake")
if(NOT CMAKE_RUNTIME_OUTPUT_DIRECTORY)
Expand Down Expand Up @@ -130,6 +131,12 @@ IF (CMAKE_C_COMPILER_ID MATCHES "^Clang$")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wshadow")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wmissing-prototypes")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wcast-qual")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wformat=2")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -pedantic")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wno-long-long")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wno-extra-semi")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wno-language-extension-token")
SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wno-overlength-strings")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above three seem rather uncommon/new. I would suggest doing a compile-time check if they're present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't compile on strict C89 mode without these

ENDIF (CMAKE_C_COMPILER_ID MATCHES "^Clang$")
IF (CMAKE_C_COMPILER_ID MATCHES "^XL$")
SET(CMAKE_C_COMPILER "xlc_r")
Expand Down
2 changes: 1 addition & 1 deletion cpio/cpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ enum {
OPTION_QUIET,
OPTION_UUENCODE,
OPTION_VERSION,
OPTION_ZSTD,
OPTION_ZSTD
Copy link
Contributor

@evelikov evelikov Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave the trailing commas, unless your compiler really does not like them. As you saw with the MacOS build - they are very common.

};

int cpio_getopt(struct cpio *cpio);
Expand Down
52 changes: 27 additions & 25 deletions libarchive/archive_read_support_filter_lz4.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct private_data {
READ_DEFAULT_STREAM,
READ_DEFAULT_BLOCK,
READ_LEGACY_STREAM,
READ_LEGACY_BLOCK,
READ_LEGACY_BLOCK
} stage;
struct {
unsigned block_independence:1;
Expand Down Expand Up @@ -344,31 +344,33 @@ lz4_filter_read(struct archive_read_filter *self, const void **p)
state->eof = 1;
*p = NULL;
return (0);
}
uint32_t number = archive_le32dec(read_buf);
Copy link
Contributor

@evelikov evelikov Nov 23, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here and through the rest of the patch - do not re-indent hunks of code, just move the declaration further up. As-is it moves touches 400+ lines of code, where a mere 10-20 will be enough.

You'd want to enable -Wdeclaration-after-statement to catch these issues.

__archive_read_filter_consume(self->upstream, 4);
if (number == LZ4_MAGICNUMBER)
return lz4_filter_read_default_stream(self, p);
else if (number == LZ4_LEGACY)
return lz4_filter_read_legacy_stream(self, p);
else if ((number & ~0xF) == LZ4_SKIPPABLED) {
read_buf = __archive_read_filter_ahead(
self->upstream, 4, NULL);
if (read_buf == NULL) {
archive_set_error(
&self->archive->archive,
ARCHIVE_ERRNO_MISC,
"Malformed lz4 data");
return (ARCHIVE_FATAL);
}
uint32_t skip_bytes = archive_le32dec(read_buf);
__archive_read_filter_consume(self->upstream,
4 + skip_bytes);
} else {
/* Ignore following unrecognized data. */
state->eof = 1;
*p = NULL;
return (0);
uint32_t number = archive_le32dec(read_buf);
__archive_read_filter_consume(self->upstream, 4);
if (number == LZ4_MAGICNUMBER)
return lz4_filter_read_default_stream(self, p);
else if (number == LZ4_LEGACY)
return lz4_filter_read_legacy_stream(self, p);
else if ((number & ~0xF) == LZ4_SKIPPABLED) {
read_buf = __archive_read_filter_ahead(
self->upstream, 4, NULL);
if (read_buf == NULL) {
archive_set_error(
&self->archive->archive,
ARCHIVE_ERRNO_MISC,
"Malformed lz4 data");
return (ARCHIVE_FATAL);
} else {
const uint32_t skip_bytes = archive_le32dec(read_buf);
__archive_read_filter_consume(self->upstream,
4 + skip_bytes);
}
} else {
/* Ignore following unrecognized data. */
state->eof = 1;
*p = NULL;
return (0);
}
}
}
state->eof = 1;
Expand Down
4 changes: 3 additions & 1 deletion libarchive/archive_read_support_filter_zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,9 @@ zstd_filter_read(struct archive_read_filter *self, const void **p)

state = (struct private_data *)self->data;

out = (ZSTD_outBuffer) { state->out_block, state->out_block_size, 0 };
out.dst = state->out_block;
out.pos = state->out_block_size;
out.size = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code will always zero fill any struct fields that are not mentioned. I would use either out = {}, out = { 0 } or memset() - whichever is more common in the project.


/* Try to fill the output buffer. */
while (out.pos < out.size && !state->eof) {
Expand Down
83 changes: 42 additions & 41 deletions libarchive/archive_read_support_format_7zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -807,50 +807,51 @@ archive_read_format_7zip_read_data(struct archive_read *a,
*/
if (zip->end_of_entry)
return (ARCHIVE_EOF);

const uint64_t max_read_size = 16 * 1024 * 1024; // Don't try to read more than 16 MB at a time
size_t bytes_to_read = max_read_size;
if ((uint64_t)bytes_to_read > zip->entry_bytes_remaining) {
bytes_to_read = zip->entry_bytes_remaining;
}
bytes = read_stream(a, buff, bytes_to_read, 0);
if (bytes < 0)
return ((int)bytes);
if (bytes == 0) {
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated 7-Zip file body");
return (ARCHIVE_FATAL);
}
zip->entry_bytes_remaining -= bytes;
if (zip->entry_bytes_remaining == 0)
zip->end_of_entry = 1;

/* Update checksum */
if ((zip->entry->flg & CRC32_IS_SET) && bytes)
zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
(unsigned)bytes);

/* If we hit the end, swallow any end-of-data marker. */
if (zip->end_of_entry) {
/* Check computed CRC against file contents. */
if ((zip->entry->flg & CRC32_IS_SET) &&
zip->si.ss.digests[zip->entry->ssIndex] !=
zip->entry_crc32) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"7-Zip bad CRC: 0x%lx should be 0x%lx",
(unsigned long)zip->entry_crc32,
(unsigned long)zip->si.ss.digests[
zip->entry->ssIndex]);
ret = ARCHIVE_WARN;
else {
const uint64_t max_read_size = 16 * 1024 * 1024; // Don't try to read more than 16 MB at a time
size_t bytes_to_read = max_read_size;
if ((uint64_t) bytes_to_read > zip->entry_bytes_remaining) {
bytes_to_read = zip->entry_bytes_remaining;
}
bytes = read_stream(a, buff, bytes_to_read, 0);
if (bytes < 0)
return ((int) bytes);
if (bytes == 0) {
archive_set_error(&a->archive,
ARCHIVE_ERRNO_FILE_FORMAT,
"Truncated 7-Zip file body");
return (ARCHIVE_FATAL);
}
zip->entry_bytes_remaining -= bytes;
if (zip->entry_bytes_remaining == 0)
zip->end_of_entry = 1;

/* Update checksum */
if ((zip->entry->flg & CRC32_IS_SET) && bytes)
zip->entry_crc32 = crc32(zip->entry_crc32, *buff,
(unsigned) bytes);

/* If we hit the end, swallow any end-of-data marker. */
if (zip->end_of_entry) {
/* Check computed CRC against file contents. */
if ((zip->entry->flg & CRC32_IS_SET) &&
zip->si.ss.digests[zip->entry->ssIndex] !=
zip->entry_crc32) {
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
"7-Zip bad CRC: 0x%lx should be 0x%lx",
(unsigned long) zip->entry_crc32,
(unsigned long) zip->si.ss.digests[
zip->entry->ssIndex]);
ret = ARCHIVE_WARN;
}
}
}

*size = bytes;
*offset = zip->entry_offset;
zip->entry_offset += bytes;
*size = bytes;
*offset = zip->entry_offset;
zip->entry_offset += bytes;

return (ret);
return (ret);
}
}

static int
Expand Down
26 changes: 14 additions & 12 deletions libarchive/archive_read_support_format_lha.c
Original file line number Diff line number Diff line change
Expand Up @@ -1300,19 +1300,21 @@ lha_read_file_extended_header(struct archive_read *a, struct lha *lha,
else
dirSep = 0x002F;

/* UTF-16LE character */
uint16_t *utf16name =
(uint16_t *)lha->dirname.s;
for (i = 0; i < lha->dirname.length / 2; i++) {
if (utf16name[i] == 0xFFFF) {
utf16name[i] = dirSep;
{
/* UTF-16LE character */
uint16_t *utf16name =
(uint16_t *) lha->dirname.s;
for (i = 0; i < lha->dirname.length / 2; i++) {
if (utf16name[i] == 0xFFFF) {
utf16name[i] = dirSep;
}
}
/* Is last character directory separator? */
if (utf16name[lha->dirname.length / 2 - 1] !=
dirSep) {
/* invalid directory data */
goto invalid;
}
}
/* Is last character directory separator? */
if (utf16name[lha->dirname.length / 2 - 1] !=
dirSep) {
/* invalid directory data */
goto invalid;
}
}
break;
Expand Down
Loading