Skip to content

Commit b9f8bfe

Browse files
committed
ImStr: Fix issues reported by PVS-Studio.
1 parent f2b81cd commit b9f8bfe

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

imgui.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,9 @@ char* ImStrdup(ImStr str)
12541254
size_t len = IM_IMSTR_LENGTH(str);
12551255
void* buf = IM_ALLOC(len + 1);
12561256
*((char*)buf + len) = 0; // str may not contain \0, it must be inserted manually.
1257-
return (char*)memcpy(buf, (const void*)str.Begin, len);
1257+
if (len > 0)
1258+
return (char*)memcpy(buf, (const void*)str.Begin, len);
1259+
return (char*)buf;
12581260
}
12591261

12601262
char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStr src)
@@ -1269,7 +1271,9 @@ char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStr src)
12691271
*p_dst_size = src_size;
12701272
}
12711273
dst[src_size - 1] = 0; // str may not contain \0, it must be inserted manually.
1272-
return (char*)memcpy(dst, (const void*)src.Begin, src_size - 1);
1274+
if (src_size > 1)
1275+
return (char*)memcpy(dst, (const void*)src.Begin, src_size - 1);
1276+
return dst;
12731277
}
12741278

12751279
char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src)
@@ -2120,7 +2124,8 @@ void ImGuiTextBuffer::append(ImStr str)
21202124
}
21212125

21222126
Buf.resize(needed_sz);
2123-
memcpy(&Buf[write_off - 1], str.Begin, (size_t)len);
2127+
if (len > 0)
2128+
memcpy(&Buf[write_off - 1], str.Begin, (size_t)len);
21242129
Buf[write_off - 1 + len] = 0;
21252130
}
21262131

@@ -3238,7 +3243,8 @@ void ImGui::SetClipboardText(ImStr text)
32383243
{
32393244
int len = (int)IM_IMSTR_LENGTH(text);
32403245
char* text_p = (char*)IM_ALLOC(len + 1);
3241-
memcpy(text_p, text.Begin, len);
3246+
if (len > 0)
3247+
memcpy(text_p, text.Begin, len);
32423248
text_p[len] = 0; // text may not contain \0, it must be inserted manually.
32433249
g.IO.SetClipboardTextFn(g.IO.ClipboardUserData, text_p);
32443250
IM_FREE(text_p);
@@ -9735,22 +9741,27 @@ void ImGui::MarkIniSettingsDirty(ImGuiWindow* window)
97359741
ImGuiWindowSettings* ImGui::CreateNewWindowSettings(ImStr name)
97369742
{
97379743
ImGuiContext& g = *GImGui;
9744+
const size_t name_len = IM_IMSTR_LENGTH(name);
9745+
if (!name_len)
9746+
{
9747+
IM_ASSERT(false && "Name must not be empty.");
9748+
return NULL;
9749+
}
97389750

97399751
#if !IMGUI_DEBUG_INI_SETTINGS
97409752
// Skip to the "###" marker if any. We don't skip past to match the behavior of GetID()
97419753
// Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier.
97429754
if (const char* p = ImStrstr(name, "###"))
97439755
name.Begin = p;
97449756
#endif
9745-
const size_t name_len = IM_IMSTR_LENGTH(name);
97469757

97479758
// Allocate chunk
97489759
const size_t chunk_size = sizeof(ImGuiWindowSettings) + name_len + 1;
97499760
ImGuiWindowSettings* settings = g.SettingsWindows.alloc_chunk(chunk_size);
97509761
IM_PLACEMENT_NEW(settings) ImGuiWindowSettings();
97519762
settings->ID = ImHashStr(name);
97529763
memcpy(settings->GetName(), name.Begin, name_len);
9753-
settings->GetName()[name_len] = 0; // name may not contain \0, it must be inserted manually.
9764+
settings->GetName()[name_len] = 0; // name may not contain \0, it must be inserted manually.
97549765

97559766
return settings;
97569767
}

imgui.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct ImVec4
229229
#endif
230230
};
231231

232-
#define IM_IMSTR_LENGTH(s) (size_t)(s.Begin ? (s.End ? s.End - s.Begin : strlen(s.Begin)) : 0)
232+
#define IM_IMSTR_LENGTH(s) (s.Begin ? (s.End ? (size_t)(s.End - s.Begin) : strlen(s.Begin)) : 0)
233233
#define IM_IMSTR_ENSURE_HAS_END(s) if (s.End == NULL) s.End = s.Begin + strlen(s.Begin)
234234

235235
// String view class.

imgui_widgets.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3460,7 +3460,8 @@ void ImGuiInputTextCallbackData::InsertChars(int pos, ImStr new_text)
34603460

34613461
if (BufTextLen != pos)
34623462
memmove(Buf + pos + new_text_len, Buf + pos, (size_t)(BufTextLen - pos));
3463-
memcpy(Buf + pos, new_text.Begin, (size_t)new_text_len * sizeof(char));
3463+
if (new_text_len > 0)
3464+
memcpy(Buf + pos, new_text.Begin, (size_t)new_text_len * sizeof(char));
34643465
Buf[BufTextLen + new_text_len] = '\0';
34653466

34663467
if (CursorPos >= pos)

0 commit comments

Comments
 (0)