fix potential segfault when overrunning char array

this fixed an issue when printing many neighbor cells because
the calculation of the remaining length can be negative
master
Andre Puschmann 5 years ago
parent 0e337a01c3
commit 23bab1f207

@ -562,12 +562,15 @@ void rrc::clean_neighbours()
void rrc::log_neighbour_cells()
{
if (not neighbour_cells.empty()) {
char ordered[512] = {};
const int32_t MAX_STR_LEN = 512;
char ordered[MAX_STR_LEN] = {};
int n = 0;
n += snprintf(ordered, 512, "[%s", neighbour_cells[0]->to_string().c_str());
n += snprintf(ordered, MAX_STR_LEN, "[%s", neighbour_cells[0]->to_string().c_str());
for (uint32_t i = 1; i < neighbour_cells.size(); i++) {
int m = snprintf(&ordered[n], 512 - n, " | %s", neighbour_cells[i]->to_string().c_str());
n += m;
if (MAX_STR_LEN - n > 0) { // make sure there is still room left
int m = snprintf(&ordered[n], MAX_STR_LEN - n, " | %s", neighbour_cells[i]->to_string().c_str());
n += m;
}
}
rrc_log->info("Neighbours: %s]\n", ordered);
} else {

Loading…
Cancel
Save