From e3822bdbd11f1546b002f00cc018a612e2f3d3e5 Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 27 Oct 2015 12:11:09 +0100 Subject: flowtop: Use strlcpy instead of memcpy to copy strings Make sure we always terminate the strings with '\0'. Also only set the first byte to '\0' instead of memset()ing the entire buffer in case no city/country is returned. Signed-off-by: Tobias Klauser --- flowtop.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/flowtop.c b/flowtop.c index 50ceccb..7573780 100644 --- a/flowtop.c +++ b/flowtop.c @@ -655,13 +655,11 @@ flow_entry_geo_city_lookup_generic(struct flow_entry *n, build_bug_on(sizeof(n->city_src) != sizeof(n->city_dst)); - if (city) { - memcpy(SELFLD(dir, city_src, city_dst), city, - min(sizeof(n->city_src), strlen(city))); - } else { - memset(SELFLD(dir, city_src, city_dst), 0, - sizeof(n->city_src)); - } + if (city) + strlcpy(SELFLD(dir, city_src, city_dst), city, + sizeof(n->city_src)); + else + SELFLD(dir, city_src, city_dst)[0] = '\0'; } static void @@ -689,13 +687,11 @@ flow_entry_geo_country_lookup_generic(struct flow_entry *n, build_bug_on(sizeof(n->country_src) != sizeof(n->country_dst)); - if (country) { - memcpy(SELFLD(dir, country_src, country_dst), country, - min(sizeof(n->country_src), strlen(country))); - } else { - memset(SELFLD(dir, country_src, country_dst), 0, - sizeof(n->country_src)); - } + if (country) + strlcpy(SELFLD(dir, country_src, country_dst), country, + sizeof(n->country_src)); + else + SELFLD(dir, country_src, country_dst)[0] = '\0'; } static void flow_entry_get_extended_geo(struct flow_entry *n, -- cgit v1.2.3-54-g00ecf