1
0
Fork 0

Codefix: std::string_view::data() is not necessarily null terminated. (#13891)

pull/13862/head
frosch 2025-03-25 20:32:19 +01:00 committed by GitHub
parent 93016b9a92
commit 25005cff16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 10 additions and 9 deletions

View File

@ -108,9 +108,9 @@ const FiosItem *FileList::FindItem(const std::string_view file)
} }
/* If no name matches, try to parse it as number */ /* If no name matches, try to parse it as number */
char *endptr; int i;
int i = std::strtol(file.data(), &endptr, 10); const char *endptr = std::from_chars(file.data(), file.data() + file.size(), i, 10).ptr;
if (file.data() == endptr || *endptr != '\0') i = -1; if (file.data() == endptr || endptr != file.data() + file.size()) i = -1;
if (IsInsideMM(i, 0, this->size())) return &this->at(i); if (IsInsideMM(i, 0, this->size())) return &this->at(i);

View File

@ -51,7 +51,8 @@ static std::optional<std::string> FindGameManualFilePath(std::string_view filena
}; };
for (Searchpath sp : searchpaths) { for (Searchpath sp : searchpaths) {
auto file_path = FioGetDirectory(sp, subdir) + filename.data(); std::string file_path = FioGetDirectory(sp, subdir);
file_path.append(filename);
if (FioCheckFileExists(file_path, NO_DIRECTORY)) return file_path; if (FioCheckFileExists(file_path, NO_DIRECTORY)) return file_path;
} }

View File

@ -297,7 +297,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint first, uint last, int prop, Byt
* such as Cyrillic. Thus we will simply assume they're all UTF8. */ * such as Cyrillic. Thus we will simply assume they're all UTF8. */
char32_t c; char32_t c;
size_t len = Utf8Decode(&c, name.data()); size_t len = Utf8Decode(&c, name.data());
if (c == NFO_UTF8_IDENTIFIER) name = name.substr(len); if (len <= name.size() && c == NFO_UTF8_IDENTIFIER) name = name.substr(len);
LanguageMap::Mapping map; LanguageMap::Mapping map;
map.newgrf_id = newgrf_id; map.newgrf_id = newgrf_id;

View File

@ -494,7 +494,7 @@ static INT_PTR CALLBACK CrashDialogFunc(HWND wnd, UINT msg, WPARAM wParam, LPARA
char *crashlog_dos_nl = reinterpret_cast<char *>(filename_buf + filename_buf_length * filename_count); char *crashlog_dos_nl = reinterpret_cast<char *>(filename_buf + filename_buf_length * filename_count);
/* Convert unix -> dos newlines because the edit box only supports that properly. */ /* Convert unix -> dos newlines because the edit box only supports that properly. */
const char *crashlog_unix_nl = crashlog.data(); const char *crashlog_unix_nl = crashlog.c_str();
char *p = crashlog_dos_nl; char *p = crashlog_dos_nl;
char32_t c; char32_t c;
while ((c = Utf8Consume(&crashlog_unix_nl))) { while ((c = Utf8Consume(&crashlog_unix_nl))) {

View File

@ -128,7 +128,7 @@ bool ScriptInstance::LoadCompatibilityScript(std::string_view api_version, Subdi
bool ScriptInstance::LoadCompatibilityScripts(Subdirectory dir, std::span<const std::string_view> api_versions) bool ScriptInstance::LoadCompatibilityScripts(Subdirectory dir, std::span<const std::string_view> api_versions)
{ {
/* Don't try to load compatibility scripts for the current version. */ /* Don't try to load compatibility scripts for the current version. */
if (this->versionAPI == std::rbegin(api_versions)->data()) return true; if (this->versionAPI == api_versions.back()) return true;
ScriptLog::Info(fmt::format("Downgrading API to be compatible with version {}", this->versionAPI)); ScriptLog::Info(fmt::format("Downgrading API to be compatible with version {}", this->versionAPI));

View File

@ -444,7 +444,7 @@ void Textbuf::Assign(const std::string_view text)
*/ */
void Textbuf::UpdateSize() void Textbuf::UpdateSize()
{ {
this->chars = static_cast<uint16_t>(Utf8StringLength(this->buf.data()) + 1); // terminating zero this->chars = static_cast<uint16_t>(Utf8StringLength(this->buf.c_str()) + 1); // terminating zero
assert(this->buf.size() < this->max_bytes); assert(this->buf.size() < this->max_bytes);
assert(this->chars <= this->max_chars); assert(this->chars <= this->max_chars);

View File

@ -1699,7 +1699,7 @@ static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const
int child_idx = ps->first_child; int child_idx = ps->first_child;
while (child_idx >= 0) { while (child_idx >= 0) {
const ChildScreenSpriteToDraw *cs = csstdv->data() + child_idx; const ChildScreenSpriteToDraw *cs = &(*csstdv)[child_idx];
child_idx = cs->next; child_idx = cs->next;
if (cs->relative) { if (cs->relative) {
DrawSpriteViewport(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub); DrawSpriteViewport(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);