Fix undefined behavior in vs_format_ascii (#1409)

This commit is contained in:
Jacqueline Deans 2022-11-29 16:29:54 -06:00 committed by GitHub
parent fb0a760a71
commit 86b0bc9d39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,7 @@ PROGRAMMERS: (((Keith Vetter) (LinCom) (September 2001) (--)))
#include <ctype.h>
#include <limits>
#include <udunits2.h>
#include <sstream>
#include "trick/parameter_types.h"
#include "trick/attributes.h"
@ -190,11 +191,15 @@ int vs_format_ascii(Trick::VariableReference * var, char *value, size_t value_si
} //end while
if (ref->units) {
std::stringstream unit_str;
if ( ref->attr->mods & TRICK_MODS_UNITSDASHDASH ) {
snprintf(value, value_size, "%s {--}", value);
unit_str << " {--}";
} else {
snprintf(value, value_size, "%s {%s}", value, ref->units);
unit_str << " {" << ref->units << "}";
}
size_t max_copy_size = value_size - strlen(value) - 1;
strncat(value, unit_str.str().c_str(), max_copy_size);
}
return (0);