From patchwork Fri Jun 3 23:19:53 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacques de Laval X-Patchwork-Id: 12869448 X-Patchwork-Delegate: dsahern@gmail.com Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A92EDC43334 for ; Fri, 3 Jun 2022 23:20:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245755AbiFCXUD (ORCPT ); Fri, 3 Jun 2022 19:20:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35762 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236404AbiFCXUC (ORCPT ); Fri, 3 Jun 2022 19:20:02 -0400 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92184D13A for ; Fri, 3 Jun 2022 16:20:00 -0700 (PDT) Date: Fri, 03 Jun 2022 23:19:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1654298397; x=1654557597; bh=YRxUSVZ+qoYXN7oduiTL6n6ULE7wKJlSgwj7h0HLgHM=; h=Date:To:From:Cc:Reply-To:Subject:Message-ID:Feedback-ID:From:To: Cc:Date:Subject:Reply-To:Feedback-ID:Message-ID; b=o1s5FLQJV0PZnekxGDyYMCYtXLRlpONNj/ST25pY9hk45FRzzyQz3ZObMjAg+ESLs jor4duyb28Ttyvrm+q7tAUfEAobKghC/1e2dzy8PbOA+oZysCh3Rm7t/x28QdJ3ggU Qo3ked079mAMXat6ogdOcUsrD3jIUSPLwi1167WUZaWfYCIDGynrl9jErbu8mZzLqm Ol/ikykY8phBcVdFxZBsBFEp2uJidFlIcbk3jCGQGFnmvY212mTPVq+wlVpT4Rb0cl N5ZYB28pEaIc+bCoxbxgYb9lYCqibtugKmn5blR43aW1CiJeN97haDlKWCPl8mxeks E3+XtmzRB3FCw== To: netdev@vger.kernel.org From: Jacques de Laval Cc: Jacques de Laval Reply-To: Jacques de Laval Subject: [PATCH iproute2-next] lib/rt_names: Fix cache getting trashed on integer input to rtnl_*_a2n Message-ID: <20220603231933.127804-1-jacques.delaval@protonmail.com> Feedback-ID: 21766145:user:proton MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org X-Patchwork-Delegate: dsahern@gmail.com The cache value 'res' should only be updated when the cache key 'cache' is updated. Otherwise the rtnl_*_a2n functions risk returning wrong values on subsequent calls. Signed-off-by: Jacques de Laval --- lib/rt_names.c | 48 +++++++++++++------------- testsuite/tests/ip/route/set_rtproto.t | 26 ++++++++++++++ 2 files changed, 50 insertions(+), 24 deletions(-) create mode 100755 testsuite/tests/ip/route/set_rtproto.t -- 2.36.1 diff --git a/lib/rt_names.c b/lib/rt_names.c index b976471d..a67d8e89 100644 --- a/lib/rt_names.c +++ b/lib/rt_names.c @@ -202,7 +202,7 @@ int rtnl_rtprot_a2n(__u32 *id, const char *arg) static char *cache; static unsigned long res; char *end; - int i; + unsigned long i; if (cache && strcmp(cache, arg) == 0) { *id = res; @@ -222,10 +222,10 @@ int rtnl_rtprot_a2n(__u32 *id, const char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res > 255) + i = strtoul(arg, &end, 0); + if (!end || end == arg || *end || i > 255) return -1; - *id = res; + *id = i; return 0; } @@ -271,7 +271,7 @@ int rtnl_rtscope_a2n(__u32 *id, const char *arg) static const char *cache; static unsigned long res; char *end; - int i; + unsigned long i; if (cache && strcmp(cache, arg) == 0) { *id = res; @@ -291,10 +291,10 @@ int rtnl_rtscope_a2n(__u32 *id, const char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res > 255) + i = strtoul(arg, &end, 0); + if (!end || end == arg || *end || i > 255) return -1; - *id = res; + *id = i; return 0; } @@ -334,7 +334,7 @@ int rtnl_rtrealm_a2n(__u32 *id, const char *arg) static char *cache; static unsigned long res; char *end; - int i; + unsigned long i; if (cache && strcmp(cache, arg) == 0) { *id = res; @@ -354,10 +354,10 @@ int rtnl_rtrealm_a2n(__u32 *id, const char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res > 255) + i = strtoul(arg, &end, 0); + if (!end || end == arg || *end || i > 255) return -1; - *id = res; + *id = i; return 0; } @@ -511,7 +511,7 @@ int rtnl_dsfield_a2n(__u32 *id, const char *arg) static char *cache; static unsigned long res; char *end; - int i; + unsigned long i; if (cache && strcmp(cache, arg) == 0) { *id = res; @@ -531,10 +531,10 @@ int rtnl_dsfield_a2n(__u32 *id, const char *arg) } } - res = strtoul(arg, &end, 16); - if (!end || end == arg || *end || res > 255) + i = strtoul(arg, &end, 16); + if (!end || end == arg || *end || i > 255) return -1; - *id = res; + *id = i; return 0; } @@ -668,7 +668,7 @@ int nl_proto_a2n(__u32 *id, const char *arg) static char *cache; static unsigned long res; char *end; - int i; + unsigned long i; if (cache && strcmp(cache, arg) == 0) { *id = res; @@ -688,10 +688,10 @@ int nl_proto_a2n(__u32 *id, const char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res > 255) + i = strtoul(arg, &end, 0); + if (!end || end == arg || *end || i > 255) return -1; - *id = res; + *id = i; return 0; } @@ -760,7 +760,7 @@ int protodown_reason_a2n(__u32 *id, const char *arg) static char *cache; static unsigned long res; char *end; - int i; + unsigned long i; if (cache && strcmp(cache, arg) == 0) { *id = res; @@ -780,9 +780,9 @@ int protodown_reason_a2n(__u32 *id, const char *arg) } } - res = strtoul(arg, &end, 0); - if (!end || end == arg || *end || res >= PROTODOWN_REASON_NUM_BITS) + i = strtoul(arg, &end, 0); + if (!end || end == arg || *end || i >= PROTODOWN_REASON_NUM_BITS) return -1; - *id = res; + *id = i; return 0; } diff --git a/testsuite/tests/ip/route/set_rtproto.t b/testsuite/tests/ip/route/set_rtproto.t new file mode 100755 index 00000000..f6dfe053 --- /dev/null +++ b/testsuite/tests/ip/route/set_rtproto.t @@ -0,0 +1,26 @@ +#!/bin/sh + +. lib/generic.sh + +ts_log "[Testing setting protocol]" + +DEV=dummy0 + +ts_ip "$0" "Add new interface $DEV" link add $DEV type dummy +ts_ip "$0" "Set $DEV into UP state" link set up dev $DEV + +cat <