Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp6080418rwi; Tue, 18 Oct 2022 07:55:13 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6g982nfGTSmnlLzn+3GYQmA2pG5YYIvj5nhofN0CY7M8vqkKQEDUqkkJ/ZNS6br/pPdvyE X-Received: by 2002:a62:e40d:0:b0:563:44d8:c6d1 with SMTP id r13-20020a62e40d000000b0056344d8c6d1mr3711768pfh.53.1666104913202; Tue, 18 Oct 2022 07:55:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666104913; cv=none; d=google.com; s=arc-20160816; b=qfc4sbAQDKxbVnlOnzFImGTqIgmZj3RwqmyvmHXKrYT4P1fxZoek/kVUGf9b1P1mVl ShbvMMOk3gaPko1TYsHuRhZVM+m1NCA5CbaGX0dUeKNPux4vxw7Fax0VTcXxHRBAC/gz laHpgtU+DzI9KHejMvW1sYmb2Cxypo1nl7WCi5NuYJAuaxFIwy4/4gRtfnWUOK3ACqPD jL2frk07IJslNGNPjxpMTdPTa9UDEV2XeT/QhqoylaPzlFOhHFU4PcXg21i6R9ybv3U2 MB6jC5wr5iVXYvxU7ADf3m/8olUWUUuqlBJoQO8pyoPoqB0sIwrXsPGLJDsSu4qO9/Ss Kxkg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=P9rxsXCH3aKrCeBxviPAdEHqwep/4MCeZ1ZvloVcjMU=; b=ZOXuhfOA9ePehBRYvF5khHcFxaLcNfzGL8tZ5KS08mnVx7mnPTGjbxer3fGeh08JnF YhU2rnJHlbuNeUlZTDlvL8SSX/ZXLmIWGNRsV0gU1D5Mrti6T9LcyIim05gwXSo0GUPs 9Ek0tMD44BOEzaZmquOkeAe/W4PMEd4jTHODh1zipfrRnIkbCkqQxOBcPQm+0edKC51W jyi5FctdMK0N6ytUJeuGbboiA0b0eD7MQqw8xJVcFU25QB4FO7frXcxc7OHILEs6Wg4Y AoIeAz+e9YUOxD6PipkF8ujW6e27CDaqReCi6yf53h9VFX3ZjKqMPd3xgcj+3lNmn3o+ kRVA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r19-20020a63ce53000000b0043c1cb75c22si15578031pgi.333.2022.10.18.07.54.57; Tue, 18 Oct 2022 07:55:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231251AbiJROiJ (ORCPT + 99 others); Tue, 18 Oct 2022 10:38:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38786 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229966AbiJROiE (ORCPT ); Tue, 18 Oct 2022 10:38:04 -0400 Received: from dggsgout11.his.huawei.com (dggsgout11.his.huawei.com [45.249.212.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DBF36C97CC; Tue, 18 Oct 2022 07:38:01 -0700 (PDT) Received: from mail02.huawei.com (unknown [172.30.67.169]) by dggsgout11.his.huawei.com (SkyGuard) with ESMTP id 4MsGcT5n69zKG7J; Tue, 18 Oct 2022 22:35:37 +0800 (CST) Received: from k01.huawei.com (unknown [10.67.174.197]) by APP3 (Coremail) with SMTP id _Ch0CgBne11Guk5jh5e9AQ--.19076S2; Tue, 18 Oct 2022 22:37:59 +0800 (CST) From: Xu Kuohai To: bpf@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Alexei Starovoitov , Daniel Borkmann , John Fastabend , Andrii Nakryiko , Martin KaFai Lau , Song Liu , Yonghong Song , KP Singh , Stanislav Fomichev , Hao Luo , Jiri Olsa Subject: [PATCH] libbpf: Avoid allocating reg_name with sscanf in parse_usdt_arg() Date: Tue, 18 Oct 2022 10:55:38 -0400 Message-Id: <20221018145538.2046842-1-xukuohai@huaweicloud.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-TRANSID: _Ch0CgBne11Guk5jh5e9AQ--.19076S2 X-Coremail-Antispam: 1UD129KBjvJXoWxWFW8Xw4fuF1rKF17Kw1rJFb_yoW5Ar43pw 4Sgw1qvrn7J3ySga4DXanYq34UCrZrJrZYyr18ta45ZF4fWr95t34fKF4Fywn5GFW7AF4a 9F4rCryrGFy5Xr7anT9S1TB71UUUUUDqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDU0xBIdaVrnRJUUUk2b4IE77IF4wAFF20E14v26r4j6ryUM7CY07I20VC2zVCF04k2 6cxKx2IYs7xG6rWj6s0DM7CIcVAFz4kK6r1j6r18M28lY4IEw2IIxxk0rwA2F7IY1VAKz4 vEj48ve4kI8wA2z4x0Y4vE2Ix0cI8IcVAFwI0_Xr0_Ar1l84ACjcxK6xIIjxv20xvEc7Cj xVAFwI0_Cr0_Gr1UM28EF7xvwVC2z280aVAFwI0_GcCE3s1l84ACjcxK6I8E87Iv6xkF7I 0E14v26rxl6s0DM2AIxVAIcxkEcVAq07x20xvEncxIr21l5I8CrVACY4xI64kE6c02F40E x7xfMcIj6xIIjxv20xvE14v26r106r15McIj6I8E87Iv67AKxVWUJVW8JwAm72CE4IkC6x 0Yz7v_Jr0_Gr1lF7xvr2IYc2Ij64vIr41lFIxGxcIEc7CjxVA2Y2ka0xkIwI1l42xK82IY c2Ij64vIr41l4I8I3I0E4IkC6x0Yz7v_Jr0_Gr1lx2IqxVAqx4xG67AKxVWUJVWUGwC20s 026x8GjcxK67AKxVWUGVWUWwC2zVAF1VAY17CE14v26r1q6r43MIIYrxkI7VAKI48JMIIF 0xvE2Ix0cI8IcVAFwI0_Jr0_JF4lIxAIcVC0I7IYx2IY6xkF7I0E14v26r4j6F4UMIIF0x vE42xK8VAvwI8IcIk0rVWrJr0_WFyUJwCI42IY6I8E87Iv67AKxVWUJVW8JwCI42IY6I8E 87Iv6xkF7I0E14v26r4j6r4UJbIYCTnIWIevJa73UjIFyTuYvjxUOyCJDUUUU X-CM-SenderInfo: 50xn30hkdlqx5xdzvxpfor3voofrz/ X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Xu Kuohai The reg_name in parse_usdt_arg() is used to hold register name, which is short enough to be held in a 16-byte array, so we could define reg_name as char reg_name[16] to avoid dynamically allocating reg_name with sscanf. Suggested-by: Andrii Nakryiko Signed-off-by: Xu Kuohai --- tools/lib/bpf/usdt.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/tools/lib/bpf/usdt.c b/tools/lib/bpf/usdt.c index 49f3c3b7f609..28fa1b2283de 100644 --- a/tools/lib/bpf/usdt.c +++ b/tools/lib/bpf/usdt.c @@ -1225,26 +1225,24 @@ static int calc_pt_regs_off(const char *reg_name) static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg) { - char *reg_name = NULL; + char reg_name[16]; int arg_sz, len, reg_off; long off; - if (sscanf(arg_str, " %d @ %ld ( %%%m[^)] ) %n", &arg_sz, &off, ®_name, &len) == 3) { + if (sscanf(arg_str, " %d @ %ld ( %%%15[^)] ) %n", &arg_sz, &off, reg_name, &len) == 3) { /* Memory dereference case, e.g., -4@-20(%rbp) */ arg->arg_type = USDT_ARG_REG_DEREF; arg->val_off = off; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off; - } else if (sscanf(arg_str, " %d @ %%%ms %n", &arg_sz, ®_name, &len) == 2) { + } else if (sscanf(arg_str, " %d @ %%%15s %n", &arg_sz, reg_name, &len) == 2) { /* Register read case, e.g., -4@%eax */ arg->arg_type = USDT_ARG_REG; arg->val_off = 0; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off; @@ -1456,16 +1454,15 @@ static int calc_pt_regs_off(const char *reg_name) static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec *arg) { - char *reg_name = NULL; + char reg_name[16]; int arg_sz, len, reg_off; long off; - if (sscanf(arg_str, " %d @ %ld ( %m[a-z0-9] ) %n", &arg_sz, &off, ®_name, &len) == 3) { + if (sscanf(arg_str, " %d @ %ld ( %15[a-z0-9] ) %n", &arg_sz, &off, reg_name, &len) == 3) { /* Memory dereference case, e.g., -8@-88(s0) */ arg->arg_type = USDT_ARG_REG_DEREF; arg->val_off = off; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off; @@ -1474,12 +1471,11 @@ static int parse_usdt_arg(const char *arg_str, int arg_num, struct usdt_arg_spec arg->arg_type = USDT_ARG_CONST; arg->val_off = off; arg->reg_off = 0; - } else if (sscanf(arg_str, " %d @ %m[a-z0-9] %n", &arg_sz, ®_name, &len) == 2) { + } else if (sscanf(arg_str, " %d @ %15[a-z0-9] %n", &arg_sz, reg_name, &len) == 2) { /* Register read case, e.g., -8@a1 */ arg->arg_type = USDT_ARG_REG; arg->val_off = 0; reg_off = calc_pt_regs_off(reg_name); - free(reg_name); if (reg_off < 0) return reg_off; arg->reg_off = reg_off; -- 2.30.2