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

Fix read_lang() crash on malformed language file #1681

Open
wants to merge 1 commit into
base: develop
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
23 changes: 16 additions & 7 deletions src/language.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,10 @@ static void recheck_lang_sections(void)
static void read_lang(char *langfile)
{
FILE *FLANG;
char lbuf[512];
char lbuf[256];
char *ltext = NULL;
char *ctmp, *ctmp1;
int ltextsize = sizeof lbuf;
int lidx;
int lnew = 1;
int lline = 1;
Expand All @@ -228,7 +229,7 @@ static void read_lang(char *langfile)
}

for (*(ltext = nmalloc(sizeof lbuf)) = 0; fgets(lbuf, sizeof lbuf, FLANG);
ltext = nrealloc(ltext, strlen(ltext) + sizeof lbuf), lskip = 0) {
lskip = 0) {
if (lnew) {
if ((lbuf[0] == '#') || (sscanf(lbuf, "%s", ltext) == EOF))
lskip = 1;
Expand All @@ -238,20 +239,28 @@ static void read_lang(char *langfile)
lskip = 1;
}
if (lskip) {
while (!strchr(lbuf, '\n') && fgets(lbuf, 511, FLANG) != NULL) {
while (!strchr(lbuf, '\n') && fgets(lbuf, sizeof lbuf, FLANG) != NULL) {
lline++;
}
/* fgets == NULL means error or empty file, so check for error */
if (ferror(FLANG)) {
putlog(LOG_MISC, "*", "LANG: Error reading lang file.");
}
lline++;
lnew = 1;
continue;
}
strcpy(ltext, strchr(lbuf, ',') + 1);
} else
strcpy(strchr(ltext, 0), lbuf);
if ((ctmp = strchr(lbuf, ',')))
strcpy(ltext, strchr(lbuf, ',') + 1);
else
putlog(LOG_MISC, "*", "LANG: Malformed text line (missing ,) in %s at %d.",
langfile, lline);
} else {
if ((strlen(ltext) + strlen(lbuf) + 1) > ltextsize) {
ltextsize += sizeof lbuf;
ltext = nrealloc(ltext, ltextsize);
}
strcat(ltext, lbuf);
}
if ((ctmp = strchr(ltext, '\n'))) {
lline++;
*ctmp = 0;
Expand Down