From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr0-f196.google.com (mail-wr0-f196.google.com [209.85.128.196]) by dpdk.org (Postfix) with ESMTP id 36ECB8DA1 for ; Wed, 18 Apr 2018 05:09:50 +0200 (CEST) Received: by mail-wr0-f196.google.com with SMTP id v24-v6so601206wra.8 for ; Tue, 17 Apr 2018 20:09:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZWBlZW01wJx5uNjgPm+frBfpvyo0WmjbZjxnUD/EWzs=; b=Szque60WlEasYu6XNY3ZC4N9F9DiSgXXnuEVabRxBQGGm5l174+XffvTx7BG/i/KYX d2LQjtiUEEU70RObSzksa3QjfUd1dr/fabczzQuFHu4Pllj+fgzaPJpf5pEQrHFCKmu6 3rOp++VDgzw2whTvEV4l2fOCRzkqVZb6Ui8x5dgBo/j6Axsy4IrUzwADbkueKB/iLsvW cu+zjuq/RjA0HefF3OGw+2FIYnE6NhxS+dcLeFYP+bIru3Jd8wdBGip6AVY7fMpxYxRT ntcGf+5YvM875k5HWkU4gVsxrHNc9GfekIRLtcs14Y4nLwLve+eg9GQO4fTpmCB16MET KVWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZWBlZW01wJx5uNjgPm+frBfpvyo0WmjbZjxnUD/EWzs=; b=Rdhf6pH+2tgCP5/dfWPhYVdgj4NioEIHbc4/qbdMm2hribjJaKUkVOVXepqZAT4bh0 G2C24rKZLjJnAjyn9NZfPxPjJmtpgcZpWZsuGz/bWUz7YhHapnzUbWmpxM8ggRqBjjSe BBDfogHSPHX1YE7Obbs78iOVWyBAR5LM/WF3TdEzwntMuk/0eQtF6WioeKBDz23FQET/ nd5EuG3jozwD17kjoT06jqMzvY36voXS4jiYvBnleWAw6VjBgXaIgSXDpS1ijUE18fBa +YAT5rCwkq3FIcuOADv+9hGj3O3UmdDgJlTuGNaS5eCDfGZJc7ujWnG7rFl9kuCDLvH4 lBrQ== X-Gm-Message-State: ALQs6tBKBRlR0XSqrVqCvr92UMZv+7JWDBntnTX10DKYHvK6EZBIuWQB ZLqcv9Ng1yqzPWDG2ptcq8UY5Khs X-Google-Smtp-Source: AIpwx489NyVutQ9xk6PHlWKd0YW7iL4nETYa6I5q1IbmTtH+YhrXHbCZjHrEhJtahK+tcflsZGJZyA== X-Received: by 2002:adf:a281:: with SMTP id s1-v6mr199405wra.176.1524020989679; Tue, 17 Apr 2018 20:09:49 -0700 (PDT) Received: from localhost.localdomain (176.122.150.126.16clouds.com. [176.122.150.126]) by smtp.gmail.com with ESMTPSA id z63-v6sm536840wrb.34.2018.04.17.20.09.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 17 Apr 2018 20:09:49 -0700 (PDT) From: Yangchao Zhou To: dev@dpdk.org Cc: thomas@monjalon.net, anatoly.burakov@intel.com Date: Wed, 18 Apr 2018 11:09:13 +0800 Message-Id: <1524020953-27227-1-git-send-email-zhouyates@gmail.com> X-Mailer: git-send-email 1.7.1 In-Reply-To: References: Subject: [dpdk-dev] [PATCH v3] mem: fix memory leaks of hugedir and replace snprintf X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 18 Apr 2018 03:09:50 -0000 The hugedir returned by get_hugepage_dir is allocated by strdup but not released. Replace snprintf with a more suitable strlcpy. Coverity issue: 272585 Fixes: cb97d93e9d3b ("mem: share hugepage info primary and secondary") Signed-off-by: Yangchao Zhou Acked-by: Anatoly Burakov --- lib/librte_eal/bsdapp/eal/eal_hugepage_info.c | 2 +- lib/librte_eal/linuxapp/eal/eal_hugepage_info.c | 18 +++++++++--------- lib/librte_eal/linuxapp/eal/eal_memory.c | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/lib/librte_eal/bsdapp/eal/eal_hugepage_info.c b/lib/librte_eal/bsdapp/eal/eal_hugepage_info.c index 38d143c..836feb6 100644 --- a/lib/librte_eal/bsdapp/eal/eal_hugepage_info.c +++ b/lib/librte_eal/bsdapp/eal/eal_hugepage_info.c @@ -96,7 +96,7 @@ RTE_LOG(INFO, EAL, "Contigmem driver has %d buffers, each of size %dKB\n", num_buffers, (int)(buffer_size>>10)); - snprintf(hpi->hugedir, sizeof(hpi->hugedir), "%s", CONTIGMEM_DEV); + strlcpy(hpi->hugedir, CONTIGMEM_DEV, sizeof(hpi->hugedir)); hpi->hugepage_sz = buffer_size; hpi->num_pages[0] = num_buffers; hpi->lock_descriptor = fd; diff --git a/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c b/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c index fb4b667..db5aabd 100644 --- a/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c +++ b/lib/librte_eal/linuxapp/eal/eal_hugepage_info.c @@ -169,8 +169,8 @@ return size; } -static const char * -get_hugepage_dir(uint64_t hugepage_sz) +static int +get_hugepage_dir(uint64_t hugepage_sz, char *hugedir, int len) { enum proc_mount_fieldnames { DEVICE = 0, @@ -188,7 +188,7 @@ const char split_tok = ' '; char *splitstr[_FIELDNAME_MAX]; char buf[BUFSIZ]; - char *retval = NULL; + int retval = -1; FILE *fd = fopen(proc_mounts, "r"); if (fd == NULL) @@ -215,7 +215,8 @@ /* if no explicit page size, the default page size is compared */ if (pagesz_str == NULL){ if (hugepage_sz == default_size){ - retval = strdup(splitstr[MOUNTPT]); + strlcpy(hugedir, splitstr[MOUNTPT], len); + retval = 0; break; } } @@ -223,7 +224,8 @@ else { uint64_t pagesz = rte_str_to_size(&pagesz_str[pagesize_opt_len]); if (pagesz == hugepage_sz) { - retval = strdup(splitstr[MOUNTPT]); + strlcpy(hugedir, splitstr[MOUNTPT], len); + retval = 0; break; } } @@ -351,7 +353,6 @@ for (dirent = readdir(dir); dirent != NULL; dirent = readdir(dir)) { struct hugepage_info *hpi; - const char *hugedir; if (strncmp(dirent->d_name, dirent_start_text, dirent_start_len) != 0) @@ -363,10 +364,10 @@ hpi = &internal_config.hugepage_info[num_sizes]; hpi->hugepage_sz = rte_str_to_size(&dirent->d_name[dirent_start_len]); - hugedir = get_hugepage_dir(hpi->hugepage_sz); /* first, check if we have a mountpoint */ - if (hugedir == NULL) { + if (get_hugepage_dir(hpi->hugepage_sz, + hpi->hugedir, sizeof(hpi->hugedir)) < 0) { uint32_t num_pages; num_pages = get_num_hugepages(dirent->d_name); @@ -378,7 +379,6 @@ num_pages, hpi->hugepage_sz); continue; } - snprintf(hpi->hugedir, sizeof(hpi->hugedir), "%s", hugedir); /* try to obtain a writelock */ hpi->lock_descriptor = open(hpi->hugedir, O_RDONLY); diff --git a/lib/librte_eal/linuxapp/eal/eal_memory.c b/lib/librte_eal/linuxapp/eal/eal_memory.c index b7a2e95..fadc1de 100644 --- a/lib/librte_eal/linuxapp/eal/eal_memory.c +++ b/lib/librte_eal/linuxapp/eal/eal_memory.c @@ -1160,8 +1160,8 @@ void numa_error(char *where) for (socket = 0; socket < RTE_MAX_NUMA_NODES && total_mem != 0; socket++) { /* skips if the memory on specific socket wasn't requested */ for (i = 0; i < num_hp_info && memory[socket] != 0; i++){ - snprintf(hp_used[i].hugedir, sizeof(hp_used[i].hugedir), - "%s", hp_info[i].hugedir); + strlcpy(hp_used[i].hugedir, hp_info[i].hugedir, + sizeof(hp_used[i].hugedir)); hp_used[i].num_pages[socket] = RTE_MIN( memory[socket] / hp_info[i].hugepage_sz, hp_info[i].num_pages[socket]); -- 1.7.1