assert and simplify

This commit is contained in:
Sergiu Deitsch 2024-11-01 14:05:00 +01:00
parent 93922d5b83
commit 5fd3750f16
No known key found for this signature in database

View File

@ -37,6 +37,7 @@
#include "demangle.h"
#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <limits>
@ -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
}