summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-08-09 12:50:32 (GMT)
committerThomas Haller <thaller@redhat.com>2019-08-09 14:48:55 (GMT)
commitc5c0240ec4455cfbd404155c43e5ff0d60d3cca7 (patch)
tree100407896261715e549b7c43555f5c3d6feed753 /lib
parentb2749acc019714bb739483cb94347a4f2cdc4450 (diff)
downloadlibnl-c5c0240ec4455cfbd404155c43e5ff0d60d3cca7.zip
libnl-c5c0240ec4455cfbd404155c43e5ff0d60d3cca7.tar.gz
libnl-c5c0240ec4455cfbd404155c43e5ff0d60d3cca7.tar.bz2
genl: reject invalid group names in genl_family_add_grp()
The compiler warns about string truncation: In function ‘genl_family_add_grp’, inlined from ‘family_clone’ at lib/genl/family.c:81:9, inlined from ‘family_clone’ at lib/genl/family.c:66:12: lib/genl/family.c:376:2: error: ‘strncpy’ output may be truncated copying 15 bytes from a string of length 15 [-Werror=stringop-truncation] 376 | strncpy(grp->name, name, GENL_NAMSIZ - 1); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Obvioulsy, it's a bug to use an invalid group name. But better handle it by checking for a suitable string length. Also use _nl_strncpy() which asserts that no truncation occurs.
Diffstat (limited to 'lib')
-rw-r--r--lib/genl/family.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/lib/genl/family.c b/lib/genl/family.c
index c98e5e1..eeb1fef 100644
--- a/lib/genl/family.c
+++ b/lib/genl/family.c
@@ -24,6 +24,8 @@
#include <netlink/genl/family.h>
#include <netlink/utils.h>
+#include "netlink-private/utils.h"
+
/** @cond SKIP */
#define FAMILY_ATTR_ID 0x01
#define FAMILY_ATTR_NAME 0x02
@@ -364,16 +366,20 @@ int genl_family_add_op(struct genl_family *family, int id, int flags)
}
int genl_family_add_grp(struct genl_family *family, uint32_t id,
- const char *name)
+ const char *name)
{
- struct genl_family_grp *grp;
+ struct genl_family_grp *grp;
+
+ if ( !name
+ || strlen (name) >= GENL_NAMSIZ)
+ return -NLE_INVAL;
grp = calloc(1, sizeof(*grp));
if (grp == NULL)
return -NLE_NOMEM;
grp->id = id;
- strncpy(grp->name, name, GENL_NAMSIZ - 1);
+ _nl_strncpy(grp->name, name, GENL_NAMSIZ);
nl_list_add_tail(&grp->list, &family->gf_mc_grps);