From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 4F7C442A50; Wed, 3 May 2023 16:54:13 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id D82A44114B; Wed, 3 May 2023 16:54:12 +0200 (CEST) Received: from mail-pl1-f170.google.com (mail-pl1-f170.google.com [209.85.214.170]) by mails.dpdk.org (Postfix) with ESMTP id BD34841144 for ; Wed, 3 May 2023 16:54:11 +0200 (CEST) Received: by mail-pl1-f170.google.com with SMTP id d9443c01a7336-1aaea43def7so31049475ad.2 for ; Wed, 03 May 2023 07:54:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20221208.gappssmtp.com; s=20221208; t=1683125651; x=1685717651; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=wMm2PrzCu4eS+vYlPwhE+62sgXezdbNboSZryPuzhjI=; b=ghTBFhlRXf+yIiXyEls1Gc3fJebmKnbts9PsSEY2Dwl8BZ785L6cWCL90JV5ipvB9Y DvG+wsY+R1QfekZLmVKqKV8CTDHvcRAdvWWAyhubdLv18ZZzZyRnx8j73eAYVVwYN5GF PaQ9XH/KXgibYk0C09r4I8jn45Y+3Buq3f4XGHfhwOYpZbJfSttFAO0L47BqkLDPQWjH AdFS5Ud7CuZDIY38WkEyEO79+WTibd76tqbDGPeJCBPsBdudjiL29wyAvbPBQeh6cKeq TaMcAAVBqBohbTXXJhaZbLfZXA4nW2qPS7Qh9/qUxIFgJSd9HPcjyMGk75h1bSQKVdwg XE8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683125651; x=1685717651; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=wMm2PrzCu4eS+vYlPwhE+62sgXezdbNboSZryPuzhjI=; b=QwDBKo9aRvt7M810bnUSpu/2U9k4GqEJdEdcMgx1A0JVZlPocV8pRsvjiRS+Z13OEe 7CWatQFLgOhMqoMkgDPoQ87IY0bnWZHp4i0RWeJ01DonGaT2r04L4fc4mqTYEq1Qw87q 1NE98XemtheZHfmVS8hjPbPsJ1t6fEJYIiuvVeqghUdX+fUDIalJp94TVGXlHuXeeuvi yUFseHGRtV0yHoddfSg/hQtcr/0spHOKAq56lPQzloYDCLhRq8BiHVY/uXEiyzgqAJTB 6eb90BmqwVYTJdlbAcMHRu+uP/qEjT0WgPeqAKIs9JPsHyp6138TNraxtLxkJFTI+1V6 KKEg== X-Gm-Message-State: AC+VfDxWdpyjlwfSD+GtIsZkcGr1QnaBTJYVuHWnLock/kCzxMK/uyKY Ivgh6Uowz2ltzMFZclv19D54Gw== X-Google-Smtp-Source: ACHHUZ7IRqax9atHsAAHjjTqhqFTRqDXq5FFhy1mM5aogx6d0XlFVmRC3MYqmSW60Ha4ZtyoSUJiqg== X-Received: by 2002:a17:902:d485:b0:1ab:b0e:1941 with SMTP id c5-20020a170902d48500b001ab0b0e1941mr235211plg.69.1683125650900; Wed, 03 May 2023 07:54:10 -0700 (PDT) Received: from hermes.local (204-195-120-218.wavecable.com. [204.195.120.218]) by smtp.gmail.com with ESMTPSA id u8-20020a170902bf4800b00198d7b52eefsm21757166pls.257.2023.05.03.07.54.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 May 2023 07:54:10 -0700 (PDT) Date: Wed, 3 May 2023 07:54:08 -0700 From: Stephen Hemminger To: Srikanth Yalavarthi Cc: Anup Prabhu , , , Subject: Re: [PATCH v3] app/mldev: add internal function for file read Message-ID: <20230503075408.455ee334@hermes.local> In-Reply-To: <20230503085642.7284-1-syalavarthi@marvell.com> References: <20230323152801.27666-1-syalavarthi@marvell.com> <20230503085642.7284-1-syalavarthi@marvell.com> MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org On Wed, 3 May 2023 01:56:41 -0700 Srikanth Yalavarthi wrote: > Added internal function to read model, input and reference > files with required error checks. This change fixes the > unchecked return value and improper use of negative value > issues reported by coverity scan for file read operations. > > Coverity issue: 383742, 383743 > Fixes: f6661e6d9a3a ("app/mldev: validate model operations") > Fixes: da6793390596 ("app/mldev: support inference validation") > > Signed-off-by: Srikanth Yalavarthi > --- > v3: > * Fix incorrect use of rte_free with free > > v2: > * Replace rte_malloc in ml_read_file with malloc > > v1: > * Initial patch > > app/test-mldev/test_common.c | 59 ++++++++++++++++++++++++++ > app/test-mldev/test_common.h | 2 + > app/test-mldev/test_inference_common.c | 54 +++++++++-------------- > app/test-mldev/test_model_common.c | 39 ++++------------- > 4 files changed, 90 insertions(+), 64 deletions(-) > > diff --git a/app/test-mldev/test_common.c b/app/test-mldev/test_common.c > index 016b31c6ba..d8a8e8a448 100644 > --- a/app/test-mldev/test_common.c > +++ b/app/test-mldev/test_common.c > @@ -5,12 +5,71 @@ > #include > > #include > +#include > #include > #include > > #include "ml_common.h" > #include "test_common.h" > > +int > +ml_read_file(char *file, size_t *size, char **buffer) > +{ > + char *file_buffer = NULL; > + long file_size = 0; > + int ret = 0; > + FILE *fp; > + > + fp = fopen(file, "r"); > + if (fp == NULL) { > + ml_err("Failed to open file: %s\n", file); > + return -EIO; > + } > + > + if (fseek(fp, 0, SEEK_END) == 0) { > + file_size = ftell(fp); > + if (file_size == -1) { > + ret = -EIO; > + goto error; > + } > + > + file_buffer = malloc(file_size); > + if (file_buffer == NULL) { > + ml_err("Failed to allocate memory: %s\n", file); > + ret = -ENOMEM; > + goto error; > + } > + > + if (fseek(fp, 0, SEEK_SET) != 0) { > + ret = -EIO; > + goto error; > + } > + > + if (fread(file_buffer, sizeof(char), file_size, fp) != (unsigned long)file_size) { > + ml_err("Failed to read file : %s\n", file); > + ret = -EIO; > + goto error; > + } > + fclose(fp); > + } else { Granted this is a test program. But why did you ignore my feedback that this is the slowest way to read a file. Stdio requires extra buffering, use regular read() or better yet mmap().