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

Allow an empty input file to be parsed correctly #79

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
18 changes: 7 additions & 11 deletions utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,18 +697,14 @@ char *get_file_contents (const char *filename) {


// If there is a UTF-8 endian marker at the beginning of the file, skip it.
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the implementation of this is the issue, I think the problem can be fixed more easily by changing this only.

Something like..

unsigned char file_head[3];
read_size = fread(file_head, 1, 3, file);
if (read_size == 3 && memcmp(file_head, utf8_endian_mark, sizeof(file_head)) == 0) {
    // Skip UTF-8 BOM
    size -= sizeof(file_head);
} else {
    // Read data needs to be retained
    rewind(file);
}

Copy link
Author

Choose a reason for hiding this comment

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

Thanks, I will incorporate that into a new commit.

const unsigned char utf8_endian_mark[] = {0xEF, 0xBB, 0xBF};
bool matched = true;
for (unsigned i = 0; matched && i < sizeof(utf8_endian_mark); i++) {
int c = fgetc(file);
if (c == EOF) {
return NULL;
}
matched &= (unsigned char)c == utf8_endian_mark[i];
}
if (matched) {
const unsigned char utf8_endian_mark[] = {0xEF, 0xBB, 0xBF};
unsigned char file_head[3];
read_size = fread (file_head, 1, 3, file);

if (read_size == 3 && memcmp(file_head, utf8_endian_mark, sizeof(utf8_endian_mark)) == 0)
// Skip UTF-8 BOM
size -= sizeof(utf8_endian_mark);
} else {
else {
rewind(file);
}

Expand Down