Skip to content

Commit

Permalink
assert and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiud committed Nov 1, 2024
1 parent 93922d5 commit 5fd3750
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/demangle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "demangle.h"

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <limits>

Expand Down Expand Up @@ -1351,10 +1352,11 @@ bool Demangle(const char* mangled, char* out, size_t out_size) {
}

if (out_size > 0) {
assert(n > 1);
// n is the size of the allocated buffer, not the length of the string.
// Therefore, it includes the terminating zero (and possibly additional
// space).
std::size_t copy_size = std::min(n - 1, out_size - 1);
std::size_t copy_size = std::min(n, out_size) - 1;
std::copy_n(unmangled.get(), copy_size, out);
out[copy_size] = '\0'; // Ensure terminating null if n > out_size
}
Expand Down

0 comments on commit 5fd3750

Please sign in to comment.