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

Fixed parsing issues on CNF files that cause infinite loops #1037

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 10 additions & 11 deletions src/ps2cnf.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@

#define CNF_LEN_MAX 1024

static const char *CNFGetToken(const char *cnf, const char *key)
static const char *CNFGetToken(const char *cnf, const char *end, const char *key)
{
for (; isspace((int)*cnf); cnf++) {
for (; isspace((int)*cnf) && cnf < end; cnf++) {
}

for (; *key != '\0'; key++, cnf++) {
// End of file
if (*cnf == '\0')
int key_length = strlen(key);
for (int i = 0; i < key_length; i++, cnf++) {
if (cnf >= end || *cnf == '\0')
return (const char *)-1;

if (*cnf != *key)
else if (*cnf != key[i])
return NULL; // Non-match
}

Expand All @@ -32,9 +31,9 @@ static const char *CNFGetToken(const char *cnf, const char *key)

static const char *CNFAdvanceLine(const char *start, const char *end)
{
for (; start <= end; start++) {
for (; start < end; start++) {
if (*start == '\n')
return start;
return start + 1 < end ? start + 1 : NULL;
}

return NULL;
Expand Down Expand Up @@ -99,7 +98,7 @@ int ps2cnfGetBootFile(const char *path, char *bootfile)

// Parse SYSTEM.CNF
cnf_start = system_cnf;
while ((pChar = CNFGetToken(cnf_start, "BOOT2")) == NULL) {
while ((pChar = CNFGetToken(cnf_start, cnf_end, "BOOT2")) == NULL) {
cnf_start = CNFAdvanceLine(cnf_start, cnf_end);
if (cnf_start == NULL)
return -1;
Expand All @@ -109,7 +108,7 @@ int ps2cnfGetBootFile(const char *path, char *bootfile)
return -1;
}

if ((pChar = CNFGetToken(pChar, "=")) == (const char *)-1) { // Unexpected EOF
if ((pChar = CNFGetToken(pChar, cnf_end, "=")) == (const char *)-1) { // Unexpected EOF
return -1;
}

Expand Down