Skip to content

Commit 4a5858d

Browse files
committed
ImStr: Fix issues reported by PVS-Studio.
1 parent ea1fcba commit 4a5858d

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

imgui.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,9 @@ char* ImStrdup(ImStr str)
13471347
size_t len = IM_IMSTR_LENGTH(str);
13481348
void* buf = IM_ALLOC(len + 1);
13491349
*((char*)buf + len) = 0; // str may not contain \0, it must be inserted manually.
1350-
return (char*)memcpy(buf, (const void*)str.Begin, len);
1350+
if (len > 0)
1351+
return (char*)memcpy(buf, (const void*)str.Begin, len);
1352+
return (char*)buf;
13511353
}
13521354

13531355
char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStr src)
@@ -1362,7 +1364,9 @@ char* ImStrdupcpy(char* dst, size_t* p_dst_size, ImStr src)
13621364
*p_dst_size = src_size;
13631365
}
13641366
dst[src_size - 1] = 0; // str may not contain \0, it must be inserted manually.
1365-
return (char*)memcpy(dst, (const void*)src.Begin, src_size - 1);
1367+
if (src_size > 1)
1368+
return (char*)memcpy(dst, (const void*)src.Begin, src_size - 1);
1369+
return dst;
13661370
}
13671371

13681372
char* ImStrdupcpy(char* dst, size_t* p_dst_size, const char* src)
@@ -2213,7 +2217,8 @@ void ImGuiTextBuffer::append(ImStr str)
22132217
}
22142218

22152219
Buf.resize(needed_sz);
2216-
memcpy(&Buf[write_off - 1], str.Begin, (size_t)len);
2220+
if (len > 0)
2221+
memcpy(&Buf[write_off - 1], str.Begin, (size_t)len);
22172222
Buf[write_off - 1 + len] = 0;
22182223
}
22192224

@@ -3289,7 +3294,8 @@ void ImGui::SetClipboardText(ImStr text)
32893294
{
32903295
int len = (int)IM_IMSTR_LENGTH(text);
32913296
char* text_p = (char*)IM_ALLOC(len + 1);
3292-
memcpy(text_p, text.Begin, len);
3297+
if (len > 0)
3298+
memcpy(text_p, text.Begin, len);
32933299
text_p[len] = 0; // text may not contain \0, it must be inserted manually.
32943300
g.IO.SetClipboardTextFn(g.IO.ClipboardUserData, text_p);
32953301
IM_FREE(text_p);
@@ -9630,22 +9636,27 @@ void ImGui::MarkIniSettingsDirty(ImGuiWindow* window)
96309636
ImGuiWindowSettings* ImGui::CreateNewWindowSettings(ImStr name)
96319637
{
96329638
ImGuiContext& g = *GImGui;
9639+
const size_t name_len = IM_IMSTR_LENGTH(name);
9640+
if (!name_len)
9641+
{
9642+
IM_ASSERT(false && "Name must not be empty.");
9643+
return NULL;
9644+
}
96339645

96349646
#if !IMGUI_DEBUG_INI_SETTINGS
96359647
// Skip to the "###" marker if any. We don't skip past to match the behavior of GetID()
96369648
// Preserve the full string when IMGUI_DEBUG_INI_SETTINGS is set to make .ini inspection easier.
96379649
if (const char* p = ImStrstr(name, "###"))
96389650
name.Begin = p;
96399651
#endif
9640-
const size_t name_len = IM_IMSTR_LENGTH(name);
96419652

96429653
// Allocate chunk
96439654
const size_t chunk_size = sizeof(ImGuiWindowSettings) + name_len + 1;
96449655
ImGuiWindowSettings* settings = g.SettingsWindows.alloc_chunk(chunk_size);
96459656
IM_PLACEMENT_NEW(settings) ImGuiWindowSettings();
96469657
settings->ID = ImHashStr(name);
96479658
memcpy(settings->GetName(), name.Begin, name_len);
9648-
settings->GetName()[name_len] = 0; // name may not contain \0, it must be inserted manually.
9659+
settings->GetName()[name_len] = 0; // name may not contain \0, it must be inserted manually.
96499660

96509661
return settings;
96519662
}

imgui.h

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

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

234234
// String view class.

imgui_widgets.cpp

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

33943394
if (BufTextLen != pos)
33953395
memmove(Buf + pos + new_text_len, Buf + pos, (size_t)(BufTextLen - pos));
3396-
memcpy(Buf + pos, new_text.Begin, (size_t)new_text_len * sizeof(char));
3396+
if (new_text_len > 0)
3397+
memcpy(Buf + pos, new_text.Begin, (size_t)new_text_len * sizeof(char));
33973398
Buf[BufTextLen + new_text_len] = '\0';
33983399

33993400
if (CursorPos >= pos)

0 commit comments

Comments
 (0)