Fix buffer overflow error in builtinHelperFileRead method

Signed-off-by: HyukWoo Park <hyukwoo.park@samsung.com>
This commit is contained in:
HyukWoo Park 2024-03-14 08:55:54 +09:00 committed by Patrick Kim
commit 5ee4a5bd4e
2 changed files with 18 additions and 8 deletions

View file

@ -251,12 +251,17 @@ static OptionalRef<StringRef> builtinHelperFileRead(OptionalRef<ExecutionStateRe
}
return src;
} else {
char msg[1024];
snprintf(msg, sizeof(msg), "GlobalObject.%s: cannot open file %s", builtinName, fileName);
if (state) {
state->throwException(URIErrorObjectRef::create(state.get(), StringRef::createFromUTF8(msg, strnlen(msg, sizeof msg))));
const size_t maxNameLength = 990;
if ((strnlen(builtinName, maxNameLength) + strnlen(fileName, maxNameLength)) < maxNameLength) {
char msg[1024];
snprintf(msg, sizeof(msg), "GlobalObject.%s: cannot open file %s", builtinName, fileName);
state->throwException(URIErrorObjectRef::create(state.get(), StringRef::createFromUTF8(msg, strnlen(msg, sizeof msg))));
} else {
state->throwException(URIErrorObjectRef::create(state.get(), StringRef::createFromASCII("invalid file name")));
}
} else {
puts(msg);
puts(fileName);
}
return nullptr;
}