Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/e9patch/e9json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ static Trampoline *dupTrampoline(const std::vector<Entry> &entries)
T->prot = PROT_READ | PROT_EXEC;
T->preload = false;
T->num_entries = num_entries;
memcpy(T->entries, &entries[0], num_entries * sizeof(Entry));
if (num_entries > 0)
memcpy(T->entries, entries.data(), num_entries * sizeof(Entry));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, just want to note that the branching is not necessary here (and on whether it's desirable, I don't have any informed opinion).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message in my description is from libstdc++ debug mode. I also checked with UBSan, and it shows an error message for this exact same access. Accessing index 0 of an empty vector is an Undefined Behavior according to the C++ standard.

I agree that this bug might not be very critical in real situations. However, considering memory safety, I think it is better to keep this branch to prevent the UB explicitly.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @McSinyx was just pointing out that the branch if (num_entries > 0) could be technically removed from the patch, since if num_entries == 0 then entries.data() is still defined and memcpy is a nop. The original code was clearly UB however.


static std::set<Trampoline *, TrampolineCmp> cache;
auto i = cache.insert(T);
Expand Down
Loading