Control lib: Fix string handling corner cases

strncpy() does not enforce NUL-termination, but snprintf() does. So we need to ensure all strings are terminated. Another issue was the use of the wrong length parameter for these functions, resulting in the limiting arguments not being applied to the right length.
This commit is contained in:
Lorenz Meier
2017-01-04 10:14:25 +01:00
parent d5082251d9
commit 36ba5eb741
2 changed files with 10 additions and 2 deletions
+6 -2
View File
@@ -66,16 +66,20 @@ void Block::getName(char *buf, size_t n)
{
if (getParent() == NULL) {
strncpy(buf, _name, n);
// ensure string is terminated
buf[n - 1] = '\0';
} else {
char parentName[blockNameLengthMax];
getParent()->getName(parentName, n);
if (!strcmp(_name, "")) {
strncpy(buf, parentName, blockNameLengthMax);
strncpy(buf, parentName, n);
// ensure string is terminated
buf[n - 1] = '\0';
} else {
snprintf(buf, blockNameLengthMax, "%s_%s", parentName, _name);
snprintf(buf, n, "%s_%s", parentName, _name);
}
}
}
+4
View File
@@ -62,12 +62,16 @@ BlockParamBase::BlockParamBase(Block *parent, const char *name, bool parent_pref
if (!strcmp(name, "")) {
strncpy(fullname, parentName, blockNameLengthMax);
// ensure string is terminated
fullname[sizeof(fullname) - 1] = '\0';
} else if (parent_prefix) {
snprintf(fullname, blockNameLengthMax, "%s_%s", parentName, name);
} else {
strncpy(fullname, name, blockNameLengthMax);
// ensure string is terminated
fullname[sizeof(fullname) - 1] = '\0';
}
parent->getParams().add(this);