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=ZG+Mwyuo; 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 DF38B5A0619 for ; Fri, 17 Oct 2025 04:44:57 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760669096; 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=vpcaX8YkQU2pgDObcR/j5Ag/fU4vhQsQGU96uIo50Ns=; b=ZG+Mwyuo8eblCejYOmxTCHuy4RzR8BJLewfFhA/vVuCn01CXHqKf7EI7gKJCkM6dE/4p9N VlcMbKBgBWDvGtanJzCj3osH3MvtVl69MkrafcbJemBrGrmt/sLNoCAX8tSW5i/dh5rkrr pt9kMLtwRvHxyuOR9Lpg7DRlcfh3YH0= 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-67-X3feiBtnPPmCi0ejGbF3Kw-1; Thu, 16 Oct 2025 22:44:55 -0400 X-MC-Unique: X3feiBtnPPmCi0ejGbF3Kw-1 X-Mimecast-MFC-AGG-ID: X3feiBtnPPmCi0ejGbF3Kw_1760669094 Received: by mail-ed1-f70.google.com with SMTP id 4fb4d7f45d1cf-63bda1898c7so1478242a12.1 for ; Thu, 16 Oct 2025 19:44:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760669094; x=1761273894; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=vpcaX8YkQU2pgDObcR/j5Ag/fU4vhQsQGU96uIo50Ns=; b=ZJUaGmc/6w+FaQXrAwXQOq6+xP6lCAGO97T8YhgHa+NLQup3O47tpxA8nwhSBa64hw 9wMVC78sfGnwU3t4QoTBr66ASZ8reX5d6FKPSPpnj6mo7W4CZRNWZEOS//NB3KfjC0fJ PmbdphPc2aVE5t5g3kQvLqxpreK+BVme8y+d1R7YpwPdqhR20l7YnxZ9A2Rz98SMik5e 5vBHzG+5RmACP62xT+6Oi2k7PAyK1H8yfT0lpD2JB000K/ee2YCOoCkeBlx9sAC/YheV SBTyJIy8DEyfnyHuuixnuN+CUHVw+g+X51Vn7E9XGyFg3XylGohheq1hTK6bD8zZVyEh QTIQ== X-Forwarded-Encrypted: i=1; AJvYcCUpXFJQFr5inZulrbHxEMeEntKN5xA9f0VNE1wInzRlrcMykeFRka0aezJjTK8Bo0VH/jvZWOrdPRw=@passt.top X-Gm-Message-State: AOJu0Yw0ZEaqukVte1TuC0xCbAzvoeEZZT42oFWtY9fVMl5BuaQrVlHF 2hgizuICJOpFP3tYpqipP8y5+y8xqc7TDt5uqDnyKHjVRklWeQSPKGIw5U3o1cWlHxzuKTKq2L8 UdL3IwlIsJDUaGtQBhu9SW1D8yv/WV2EBTltltQzmjhRvYhMFc24+2vk+ORmQzy3dTtH0SLA3xB we4C93Gwe37bJgVMbOjehMCUiakyLN X-Gm-Gg: ASbGnctjQCV8pVlX7gfVPaihP8lqU2/dlPUDSnUjMVRU/sXhvI1kxRp/Ksvjucj38hr K+pwoE5pnGqTfNgkS8q+Tlq1WjWdarYIa785KFw/Jk0R9Z/cUxcAakVR98FYP+ikU0d4M5uLyrJ A4nvGG9MdJiHBoFySu2w38XjzyH5qU5sR6tyw8c6YJa3afuvzx0IlDaeUw X-Received: by 2002:aa7:c78a:0:b0:63c:1e24:d919 with SMTP id 4fb4d7f45d1cf-63c1f6d2abamr1208888a12.28.1760669094183; Thu, 16 Oct 2025 19:44:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHDkxDzvqXbD85c5ef1iq0HNsH2BiAgd0yJ5mtcZPKq+qB8beGwBxWjGg7nCWRGqTC5R1VGFLgmhrag7g1cpt8= X-Received: by 2002:aa7:c78a:0:b0:63c:1e24:d919 with SMTP id 4fb4d7f45d1cf-63c1f6d2abamr1208881a12.28.1760669093770; Thu, 16 Oct 2025 19:44:53 -0700 (PDT) MIME-Version: 1.0 References: <20251016023423.8923-1-yuhuang@redhat.com> <20251016023423.8923-3-yuhuang@redhat.com> <20251017002214.3fd4955b@elisabeth> In-Reply-To: From: Yumei Huang Date: Fri, 17 Oct 2025 10:44:42 +0800 X-Gm-Features: AS18NWCcGUGS4lL5srvGSM8quxuf02G02pRPN3ag1dIpcBVfGXs7I4qoEOAXr4s Message-ID: Subject: Re: [PATCH v4 2/4] util: Introduce read_file() and read_file_integer() function To: David Gibson X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: wVFkcfSZmBuBeDLLa_Ze2BbicA_YdMUrD8tdX2fYeXs_1760669094 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Message-ID-Hash: 5ZI73LKJM2JJMDZOAZSYIMRJ7NFVQQMO X-Message-ID-Hash: 5ZI73LKJM2JJMDZOAZSYIMRJ7NFVQQMO 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: Stefano Brivio , passt-dev@passt.top 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, Oct 17, 2025 at 10:30=E2=80=AFAM David Gibson wrote: > > On Fri, Oct 17, 2025 at 10:11:21AM +0800, Yumei Huang wrote: > > On Fri, Oct 17, 2025 at 7:16=E2=80=AFAM David Gibson > > wrote: > > > > > > On Fri, Oct 17, 2025 at 12:22:14AM +0200, Stefano Brivio wrote: > > > > On Thu, 16 Oct 2025 15:49:39 +0800 > > > > Yumei Huang wrote: > > > > > > > > > On Thu, Oct 16, 2025 at 2:30=E2=80=AFPM David Gibson > > > > > wrote: > > > > > > > > > > > > On Thu, Oct 16, 2025 at 10:34:21AM +0800, Yumei Huang wrote: > > > > > > > Signed-off-by: Yumei Huang > > > [snip] > > > > > > > + if (total_read =3D=3D buf_size) { > > > > > > > + warn_perror("File %s truncated, buffer too smal= l", path); > > > > > > > + return -2; > > > > > > > + } > > > > > > > + > > > > > > > + buf[total_read] =3D '\0'; > > > > > > > + > > > > > > > + return (int)total_read; > > > > > > > > > > > > Probably makes more sense for total_read and the return type to= be ssize_t. > > > > > > > > > > Just tried to be consistent with write_file(). I can change it t= o > > > > > ssize_t if needed. > > > > > > > > ssize_t is the type designed for this, if write_file() has it wrong= (I > > > > didn't check), we should fix that as well. > > > > > > It does, and we should :). > > > > Checked write_file(), seems the return value is not the same to > > read_file(). It returns 0 or 1 depending on success or failure. So int > > works fine here. I will update read_file() only. > > Ah, good point. > > > [snip] > > > > Rationale (added to my further list for CONTRIBUTING.md): > > > > > > > > https://hisham.hm/2018/06/16/when-listing-repeated-things-make-py= ramids/ > > > > > > > > and see also https://lwn.net/Articles/758552/. > > > > > > If you want to update CONTRIBUTING.md to cover this, Yumei, that woul= d > > > be much appreciated. > > > > Sure, will do it. > > Thanks. > > [snip] > > > > > > > diff --git a/util.h b/util.h > > > > > > > index 22eaac5..887d795 100644 > > > > > > > --- a/util.h > > > > > > > +++ b/util.h > > > > > > > @@ -222,6 +222,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); > > > > > > > +int read_file(const char *path, char *buf, size_t buf_size); > > > > > > > +intmax_t read_file_integer(const char *path, intmax_t fallba= ck); > > > > > > > int write_all_buf(int fd, const void *buf, size_t len); > > > > > > > int write_remainder(int fd, const struct iovec *iov, size_t = iovcnt, size_t skip); > > > > > > > int read_all_buf(int fd, void *buf, size_t len); > > > > > > > @@ -249,6 +251,7 @@ static inline const char *af_name(sa_fami= ly_t af) > > > > > > > } > > > > > > > > > > > > > > #define UINT16_STRLEN (sizeof("65535")) > > > > > > > +#define INTMAX_STRLEN (sizeof("-9223372036854= 775808")) > > > > > > > > > > > > It's correct for now, and probably for any systems we're likely= to run > > > > > > on, but I dislike hard-assuming the size of intmax_t here. I f= eel > > > > > > like there must be a better way to derive the correct string le= ngth, > > > > > > but I haven't figured out what it is yet :(. > > > > > > > > > > How about this: > > > > > > > > > > #define INTMAX_STRLEN (sizeof(intmax_t) * 3 + 2) > > > > > > > > > > Each byte can represent about 2.4 decimal digits as below, > > > > > sizeof(intmax_t) * 3 gives us a safe upper bound, +2 for sign and= null > > > > > terminator. > > > > > > > > > > 1 bit =3D log=E2=82=81=E2=82=80(2) =E2=89=88 0.30103 decimal di= gits > > > > > 1 byte =3D 8 bits =3D 8 =C3=97 0.30103 =E2=89=88 2.408 decimal = digits > > > > > > Works for me. > > > > > > > If it's sourced from https://stackoverflow.com/a/10536254 and comme= nt, > > > > don't forget to mention that in whatever implementation / commit > > > > message. > > > > Actually, it's a suggestion from Claude and I double checked the logic = and math. > > Maybe I should mention Claude and the logic in the commit message inste= ad? > > Heh, so Claude got it from that stackoverflow page or one like it, > probably. It's not correctness we're concerned about, but proper > attribution. This is small enough that it's probably not > copyrightable, but that would be a concern for larger segments as > well. Yeah, I understand. For most of the time, I only asked claude to explain the code instead of writing the code directly. For this piece, I will add a comment explaining the logic before the define line. Thanks. > > -- > David Gibson (he or they) | I'll have my music baroque, and my code > david AT gibson.dropbear.id.au | minimalist, thank you, not the other wa= y > | around. > http://www.ozlabs.org/~dgibson --=20 Thanks, Yumei Huang