mirror of
https://github.com/kanjitalk755/macemu.git
synced 2025-02-08 10:30:45 +00:00
Prefs: Fix buffer overrun
Prevent intermittent crashing when prefs contains empty lines or lines with no spaces.
This commit is contained in:
parent
46be4955ae
commit
8204105c41
@ -382,12 +382,15 @@ void PrefsRemoveItem(const char *name, int index)
|
|||||||
void LoadPrefsFromStream(FILE *f)
|
void LoadPrefsFromStream(FILE *f)
|
||||||
{
|
{
|
||||||
char line[256];
|
char line[256];
|
||||||
while(fgets(line, 255, f)) {
|
while(fgets(line, sizeof(line), f)) {
|
||||||
// Read line
|
// Remove newline, if present
|
||||||
int len = strlen(line);
|
int len = strlen(line);
|
||||||
|
if (len > 0 && line[len-1] == '\n') {
|
||||||
|
line[len-1] = '\0';
|
||||||
|
len--;
|
||||||
|
}
|
||||||
if (len == 0)
|
if (len == 0)
|
||||||
continue;
|
continue;
|
||||||
line[len-1] = 0;
|
|
||||||
|
|
||||||
// Comments begin with "#" or ";"
|
// Comments begin with "#" or ";"
|
||||||
if (line[0] == '#' || line[0] == ';')
|
if (line[0] == '#' || line[0] == ';')
|
||||||
@ -395,11 +398,12 @@ void LoadPrefsFromStream(FILE *f)
|
|||||||
|
|
||||||
// Terminate string after keyword
|
// Terminate string after keyword
|
||||||
char *p = line;
|
char *p = line;
|
||||||
while (!isspace(*p)) p++;
|
while (*p && !isspace(*p)) p++;
|
||||||
*p++ = 0;
|
if (*p != '\0')
|
||||||
|
*p++ = 0;
|
||||||
|
|
||||||
// Skip whitespace until value
|
// Skip whitespace until value
|
||||||
while (isspace(*p)) p++;
|
while (*p && isspace(*p)) p++;
|
||||||
char *keyword = line;
|
char *keyword = line;
|
||||||
char *value = p;
|
char *value = p;
|
||||||
int32 i = atol(value);
|
int32 i = atol(value);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user