From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=Z0/wfSm5; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id 534855A061E for ; Fri, 14 Nov 2025 02:59:13 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1763085552; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q05dFgfGoCvYbxrj7J2fAnnjmNUNLIqqxz+a8LLx2R8=; b=Z0/wfSm5zvHFZLQMfYgxDk40kDgNxrK+eU9NVumizBIAXYwZv8dZ4B7+JplFqpifzzU0Wy k0ZVSSS7DcJqBhliu9RnwXa3+rFGYuK+vD7A/Yc/vYB+G8LtJPbdbJ9a7f4PQPzQ9qI02x J6ywgPca78ZBOiUk0Z+vED1qUc8wUg8= Received: from mail-ed1-f70.google.com (mail-ed1-f70.google.com [209.85.208.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-527-e4Tg4vfRP--InfSA8yL43A-1; Thu, 13 Nov 2025 20:59:10 -0500 X-MC-Unique: e4Tg4vfRP--InfSA8yL43A-1 X-Mimecast-MFC-AGG-ID: e4Tg4vfRP--InfSA8yL43A_1763085549 Received: by mail-ed1-f70.google.com with SMTP id 4fb4d7f45d1cf-64095d94c2cso1598043a12.1 for ; Thu, 13 Nov 2025 17:59:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763085549; x=1763690349; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=Q05dFgfGoCvYbxrj7J2fAnnjmNUNLIqqxz+a8LLx2R8=; b=T36vzPTAGAAGEBpgZW2JCIAsanqDNb6Y52anO0jLCzGIi2GP/BDHBvNg9DfWub1hQq 9J+5mCiLegDaObwJPOlze30pZAJxLWcZarRizTRmqyUZOuP75LuT1E0j1wPs1xOYtTvA sKHRaYZydU4mi7mZc4eExJbfF5+3ZtZ14WCGJcS1In8dy0ha6TBGH3QEKPa8JHRJMel5 Yi90iAovj58gQS7m1v8KbEQmgL9MhOkSesHwRUSLVdJYDhoRpulouOiyZpgUZO3yrm0A mcrbznULRw+4ptD48KNyDnEMf0XaJc/Jh7zQRvZAGoWvioXW6wwPgyztxjmdJkWLXbz+ laZg== X-Gm-Message-State: AOJu0Yxs81d6pY4MJkaldJmV3nHDLov1rERlYnRJXHySst7Jozkn0427 ex9JjMwl3qOgNB2U8aNZDOUcnuV0QG6obsUT2rryUNL1cDnAsUwymDQAwUUoPjjrr8vtw98Krvo 4VKE26oeT2gMn40PpXx4v8XVyzXPjtuUFail7uYZ/fbhiLsEhHMS8XPDSjND1E6ISlbsCA81RUu 2jYtcZQ6yhAhGKOeBGIpvU9lpYV3LU X-Gm-Gg: ASbGncsdiGHo0ZvY2kKh0qUJpKF4cy++Ls9eU8xZL+gO8bQ5JHg3UErzDWOj6Ya/Y/N KOH49c6X2J5Xy/X+FKvXruM36kCVzFYS+Vf0a8Gm/gIsRnchS1WAkxvEJh1Rw9xsQHR/8oRJnvB xhMKEYq3jke/QEWvw2w63R0cEadLVrTb/P4uRGudNtb+WLLaJah7LdRf/F X-Received: by 2002:a05:6402:4308:b0:640:c062:8bca with SMTP id 4fb4d7f45d1cf-64350e75eadmr998061a12.18.1763085549064; Thu, 13 Nov 2025 17:59:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IG2UdpZy+VOFdWkqfXv8qDjvLIKKtMSInw76hhh9ZDBtMZAt7ovAEo0sGL1AIezcIty7MiuIQ+v9uW5pQNnUfI= X-Received: by 2002:a05:6402:4308:b0:640:c062:8bca with SMTP id 4fb4d7f45d1cf-64350e75eadmr998046a12.18.1763085548549; Thu, 13 Nov 2025 17:59:08 -0800 (PST) MIME-Version: 1.0 References: <20251110093137.87705-1-yuhuang@redhat.com> <20251110093137.87705-3-yuhuang@redhat.com> <20251114010134.6a79cb30@elisabeth> In-Reply-To: <20251114010134.6a79cb30@elisabeth> From: Yumei Huang Date: Fri, 14 Nov 2025 09:58:57 +0800 X-Gm-Features: AWmQ_bm6tC1kY4Q3dsa3CzTxWC5O0sPJWFSokJtPdlN73J0_Wn0AjXNCk-8Jwfo Message-ID: Subject: Re: [PATCH v8 2/6] util: Introduce read_file() and read_file_integer() function To: Stefano Brivio X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: mgCkeZgKC5badehb2QR3VNdFXO_g19ZVKbExvcJuB88_1763085549 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: FVOX7AOKJV6WPOMWM6W4SRWEWYRV4EXH X-Message-ID-Hash: FVOX7AOKJV6WPOMWM6W4SRWEWYRV4EXH X-MailFrom: yuhuang@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: passt-dev@passt.top, david@gibson.dropbear.id.au X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: On Fri, Nov 14, 2025 at 8:01=E2=80=AFAM Stefano Brivio = wrote: > > On Mon, 10 Nov 2025 17:31:33 +0800 > Yumei Huang wrote: > > > Signed-off-by: Yumei Huang > > Reviewed-by: David Gibson > > --- > > util.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ > > util.h | 2 ++ > > 2 files changed, 88 insertions(+) > > > > diff --git a/util.c b/util.c > > index 44c21a3..c4c849c 100644 > > --- a/util.c > > +++ b/util.c > > @@ -590,6 +590,92 @@ int write_file(const char *path, const char *buf) > > return len =3D=3D 0 ? 0 : -1; > > } > > > > +/** > > + * read_file() - Read contents of file into a NULL-terminated buffer > > + * @path: Path to file to read > > + * @buf: Buffer to store file contents > > + * @buf_size: Size of buffer > > + * > > + * Return: number of bytes read on success, -1 on error, -ENOBUFS on t= runcation > > + */ > > +ssize_t read_file(const char *path, char *buf, size_t buf_size) > > +{ > > + int fd =3D open(path, O_RDONLY | O_CLOEXEC); > > + size_t total_read =3D 0; > > + ssize_t rc; > > + > > + if (fd < 0) { > > + warn_perror("Could not open %s", path); > > + return -1; > > + } > > + > > + while (total_read < buf_size) { > > + rc =3D read(fd, buf + total_read, buf_size - total_read); > > cppcheck rightfully says that: > > util.c:604:10: style: The scope of the variable 'rc' can be reduced. [var= iableScope] > ssize_t rc; > ^ Right. Seems it also says: tcp.c:2814:0: style: The function 'tcp_get_rto_params' should have static linkage since it is not used outside of its translation unit. [staticFunction] void tcp_get_rto_params(struct ctx *c) ^ util.c:601:0: style: The function 'read_file' should have static linkage since it is not used outside of its translation unit. [staticFunction] ssize_t read_file(const char *path, char *buf, size_t buf_size) I understand read_file() may be called from other places in the future. But do we need to add static now? I guess we need it for tcp_get_rto_params(). > > > + > > + if (rc < 0) { > > + warn_perror("Couldn't read from %s", path); > > + close(fd); > > + return -1; > > + } > > + > > + if (rc =3D=3D 0) > > + break; > > + > > + total_read +=3D rc; > > + } > > + > > + close(fd); > > + > > + if (total_read =3D=3D buf_size) { > > + warn("File %s contents exceed buffer size %zu", path, > > + buf_size); > > + buf[buf_size - 1] =3D '\0'; > > I suggested we need this, but Coverity Scan points out that: > > --- > /home/sbrivio/passt/util.c:631:3: > Type: Overflowed constant (INTEGER_OVERFLOW) > > /home/sbrivio/passt/util.c:606:2: > 1. path: Condition "fd < 0", taking false branch. > /home/sbrivio/passt/util.c:611:2: > 2. path: Condition "total_read < buf_size", taking false branch. > /home/sbrivio/passt/util.c:628:2: > 3. path: Condition "total_read =3D=3D buf_size", taking true branch. > /home/sbrivio/passt/util.c:631:3: > 4. overflow_const: Expression "buf_size - 1UL", where "buf_size" is kno= wn to be equal to 0, underflows the type of "buf_size - 1UL", which is type= "unsigned long". > --- Somehow my Coverity Scan didn't complain about that. > > in the (faulty) case where somebody calls this with 0 as buf_size. > > On the other hand, the passed value of buf_size might be a result of a > wrong calculation, and in that case we don't want to write some > unrelated value on the stack of the caller or smash the stack. > > We could ASSERT(buf_size), but in the future we might abuse read_file() > to just check that a file is there and can be read, instead of actually > reading it. > > So maybe we could just return (after closing fd) before read() on > !buf_size? Sure. I can add that. > > > + return -ENOBUFS; > > + } > > + > > + buf[total_read] =3D '\0'; > > + > > + return total_read; > > +} > > + > > +/** > > + * read_file_integer() - Read an integer value from a file > > + * @path: Path to file to read > > + * @fallback: Default value if file can't be read > > + * > > + * Return: integer value, @fallback on failure > > + */ > > +intmax_t read_file_integer(const char *path, intmax_t fallback) > > +{ > > + ssize_t bytes_read; > > + char buf[BUFSIZ]; > > + intmax_t value; > > + char *end; > > + > > + bytes_read =3D read_file(path, buf, sizeof(buf)); > > + > > + if (bytes_read < 0) > > + return fallback; > > + > > + if (bytes_read =3D=3D 0) { > > + debug("Empty file %s", path); > > + return fallback; > > + } > > + > > + errno =3D 0; > > + value =3D strtoimax(buf, &end, 10); > > + if (*end && *end !=3D '\n') { > > + debug("Non-numeric content in %s", path); > > + return fallback; > > + } > > + if (errno) { > > + debug("Out of range value in %s: %s", path, buf); > > + return fallback; > > + } > > + > > + return value; > > +} > > + > > #ifdef __ia64__ > > /* Needed by do_clone() below: glibc doesn't export the prototype of _= _clone2(), > > * use the description from clone(2). > > diff --git a/util.h b/util.h > > index a0b2ada..c1502cc 100644 > > --- a/util.h > > +++ b/util.h > > @@ -229,6 +229,8 @@ void pidfile_write(int fd, pid_t pid); > > int __daemon(int pidfile_fd, int devnull_fd); > > int fls(unsigned long x); > > int write_file(const char *path, const char *buf); > > +ssize_t read_file(const char *path, char *buf, size_t buf_size); > > +intmax_t read_file_integer(const char *path, intmax_t fallback); > > int write_all_buf(int fd, const void *buf, size_t len); > > int write_remainder(int fd, const struct iovec *iov, size_t iovcnt, si= ze_t skip); > > int read_all_buf(int fd, void *buf, size_t len); > > -- > Stefano > --=20 Thanks, Yumei Huang