LibC: Handle negative size in fgets

This commit is contained in:
2026-05-15 14:28:54 +03:00
parent f0c5fb3a87
commit 6a2f041858

View File

@@ -277,11 +277,11 @@ int fgetpos(FILE* file, fpos_t* pos)
char* fgets(char* str, int size, FILE* file)
{
if (size == 0)
if (size <= 0)
return nullptr;
ScopeLock _(file);
int i = 0;
for (; i < size - 1; i++)
while (i < size - 1)
{
int c = getc_unlocked(file);
if (c == EOF)
@@ -290,13 +290,10 @@ char* fgets(char* str, int size, FILE* file)
return nullptr;
break;
}
str[i] = c;
str[i++] = c;
if (c == '\n')
{
i++;
break;
}
}
str[i] = '\0';
return str;
}