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) char* fgets(char* str, int size, FILE* file)
{ {
if (size == 0) if (size <= 0)
return nullptr; return nullptr;
ScopeLock _(file); ScopeLock _(file);
int i = 0; int i = 0;
for (; i < size - 1; i++) while (i < size - 1)
{ {
int c = getc_unlocked(file); int c = getc_unlocked(file);
if (c == EOF) if (c == EOF)
@@ -290,12 +290,9 @@ char* fgets(char* str, int size, FILE* file)
return nullptr; return nullptr;
break; break;
} }
str[i] = c; str[i++] = c;
if (c == '\n') if (c == '\n')
{
i++;
break; break;
}
} }
str[i] = '\0'; str[i] = '\0';
return str; return str;