mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-18 02:43:33 +08:00
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:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user