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=QqmegZli; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by passt.top (Postfix) with ESMTPS id 644385A026F for ; Sun, 19 Oct 2025 12:07:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1760868449; 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=fjtysFC/b3AF/ucgFc4D+7Myz3VQPdlufE2Qk7ByL1E=; b=QqmegZlie5NanDedKOzZH5SGioScjcDppG1y5uTZDGqozkcZVor8MBKpeK1BeRr/svRYk9 anzP8G2pv9/SoAVacShkcq+98o+oGIpGP2SwGWSGJq2JV2eOjJUOXEUAcsXSAIfOrtUVHl bAuqEnFSWTQWLjJ2ZHxs8Dfvujo5Yo0= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-294-3Xg-jDAvPzGHbbxV0fXjBQ-1; Sun, 19 Oct 2025 06:07:27 -0400 X-MC-Unique: 3Xg-jDAvPzGHbbxV0fXjBQ-1 X-Mimecast-MFC-AGG-ID: 3Xg-jDAvPzGHbbxV0fXjBQ_1760868447 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-471148ad64aso13856445e9.2 for ; Sun, 19 Oct 2025 03:07:27 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1760868446; x=1761473246; h=content-transfer-encoding:mime-version:organization: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=WSOU8RMtSJVZ+Gi+Lly4EFTXF/5j2CDdRs/ezjvZKzQ=; b=fD6R8ZHnCMkL095/Vk4IakazFIqaMvN4v5+vf8fyAXnXh6UY/IwE78LcVU1nRz1HM/ URFHYFP396i3JklHp7eeow6ZvH+mza4u/bheHDR7R0IZDGcscqScj6Rqa8eY3oQ3p9ti NvdkNv2DNF46E6WoB8O7gn679i48w1nl2vheBinG1ZJm3V20uUey3Dyl4Y0Z0MlGNTxP LjdDYHLBZEKrpGQVUTwHASyBKm9eAbrP51LzvFrMOld+5ebrkrY36evzKj+3yxf1K+sx GmIaHANk90C520D5IZzYeMOFo8XzVibe3W3+cd8MkJy1SxBaPivjkg2Dg+wCDpIri6XD Dx0Q== X-Forwarded-Encrypted: i=1; AJvYcCU0ieSVwM4SwA5SQhR1MbbPckbd2Oy7jEftTvfOTAj8fcZenoVj0dZ3UX+GmCsjz/df3VpxOTWVPIM=@passt.top X-Gm-Message-State: AOJu0Yw2yHfrdSgnjGqqmEcjHhgJ+mx6ApuH3pjAP7vz7HNP0de4XrFY eesQpfSfvN70kfZ4MYKReHCCjiKxhSPWYJyEj87t1DcGWjgC/k7/DPPqYt2fNtPuXj3XnvD2UHl CMCaDxqowQc65HRsD8wSXV3//25MDawWZbIFo3z8w4e5dPLRozpRwe4O8AytrtA== X-Gm-Gg: ASbGnctGb8xP2jogYYVIpdZJE4foOjeJMVoKAIPlZK9o4ma5yJ3ADFlGjpYEYvVP5jo IA2d3647PPhiIrRT7PD4R5Q2LR/JI2fjqaecevFURqWFuO96TPy21N6Qwr/0L57ZteppYerDoZk ciMaHN5Q2F/SUALQ+p0UcNMmCYb7F//I2otQAIo/goehYMyiYq7qSe+Hfny6BV4FoEaxpSJulbe E4rSPKC4cGmJdHD8hpbz0yiI2wfqH1ORo7GkOsI4ga0zEGXaB/8aTtiQs0HDDOvjVdhOg8gASSM O6IDHoonB6MgpecQbKLRIKPYWYkI1xOB7/uVFaxDq7lMHbqhCaqO+77njwb0xuqHUgTDOmiPMwY tLQfzi69c0NwUDddURvQg5aFVRf8= X-Received: by 2002:a05:600c:1e1f:b0:471:14af:c71b with SMTP id 5b1f17b1804b1-471178761eamr68481675e9.8.1760868446027; Sun, 19 Oct 2025 03:07:26 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGwsb7oEUYmc+0hnIQLj2uRP8lzN6XISZ9KRJIsOIsDjSJiUK101UgdmWFzala9ES+l3Bisig== X-Received: by 2002:a05:600c:1e1f:b0:471:14af:c71b with SMTP id 5b1f17b1804b1-471178761eamr68481525e9.8.1760868445514; Sun, 19 Oct 2025 03:07:25 -0700 (PDT) Received: from maya.myfinge.rs (ifcgrfdd.trafficplex.cloud. [176.103.220.4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-427f00ce586sm9418881f8f.49.2025.10.19.03.07.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 19 Oct 2025 03:07:24 -0700 (PDT) Date: Sun, 19 Oct 2025 12:07:23 +0200 From: Stefano Brivio To: Yumei Huang Subject: Re: [PATCH v4 2/4] util: Introduce read_file() and read_file_integer() function Message-ID: <20251019120723.74785308@elisabeth> In-Reply-To: References: <20251016023423.8923-1-yuhuang@redhat.com> <20251016023423.8923-3-yuhuang@redhat.com> <20251017002214.3fd4955b@elisabeth> Organization: Red Hat X-Mailer: Claws Mail 4.2.0 (GTK 3.24.49; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: _u3UTP5SXoTLwETTgKhqL8H1Wqx649meGyIk6i6yLns_1760868447 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Message-ID-Hash: JWW3EOHJXKKM4HBKXDUP6RA5SYMOKATP X-Message-ID-Hash: JWW3EOHJXKKM4HBKXDUP6RA5SYMOKATP X-MailFrom: sbrivio@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: David Gibson , 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, 17 Oct 2025 10:44:42 +0800 Yumei Huang wrote: > 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: =20 > > > On Fri, Oct 17, 2025 at 7:16=E2=80=AFAM David Gibson > > > wrote: =20 > > > > > > > > On Fri, Oct 17, 2025 at 12:22:14AM +0200, Stefano Brivio wrote: =20 > > > > > On Thu, 16 Oct 2025 15:49:39 +0800 > > > > > Yumei Huang wrote: > > > > > =20 > > > > > > On Thu, Oct 16, 2025 at 2:30=E2=80=AFPM David Gibson > > > > > > wrote: =20 > > > > > > > > > > > > > > On Thu, Oct 16, 2025 at 10:34:21AM +0800, Yumei Huang wrote: = =20 > > > > > > > > Signed-off-by: Yumei Huang =20 > > > > [snip] =20 > > > > > > > > + if (total_read =3D=3D buf_size) { > > > > > > > > + warn_perror("File %s truncated, buffer too sm= all", path); > > > > > > > > + return -2; > > > > > > > > + } > > > > > > > > + > > > > > > > > + buf[total_read] =3D '\0'; > > > > > > > > + > > > > > > > > + return (int)total_read; =20 > > > > > > > > > > > > > > Probably makes more sense for total_read and the return type = to be ssize_t. =20 > > > > > > > > > > > > Just tried to be consistent with write_file(). I can change it= to > > > > > > ssize_t if needed. =20 > > > > > > > > > > ssize_t is the type designed for this, if write_file() has it wro= ng (I > > > > > didn't check), we should fix that as well. =20 > > > > > > > > It does, and we should :). =20 > > > > > > 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 in= t > > > works fine here. I will update read_file() only. =20 > > > > Ah, good point. > > > > > > [snip] =20 > > > > > Rationale (added to my further list for CONTRIBUTING.md): > > > > > > > > > > https://hisham.hm/2018/06/16/when-listing-repeated-things-make-= pyramids/ > > > > > > > > > > and see also https://lwn.net/Articles/758552/. =20 > > > > > > > > If you want to update CONTRIBUTING.md to cover this, Yumei, that wo= uld > > > > be much appreciated. =20 > > > > > > Sure, will do it. =20 > > > > Thanks. > > > > [snip] =20 > > > > > > > > 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 fall= back); > > > > > > > > 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_fa= mily_t af) > > > > > > > > } > > > > > > > > > > > > > > > > #define UINT16_STRLEN (sizeof("65535")) > > > > > > > > +#define INTMAX_STRLEN (sizeof("-92233720368= 54775808")) =20 > > > > > > > > > > > > > > It's correct for now, and probably for any systems we're like= ly to run > > > > > > > on, but I dislike hard-assuming the size of intmax_t here. I= feel > > > > > > > like there must be a better way to derive the correct string = length, > > > > > > > but I haven't figured out what it is yet :(. =20 > > > > > > > > > > > > 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 a= nd null > > > > > > terminator. > > > > > > > > > > > > 1 bit =3D log=E2=82=81=E2=82=80(2) =E2=89=88 0.30103 decimal = digits > > > > > > 1 byte =3D 8 bits =3D 8 =C3=97 0.30103 =E2=89=88 2.408 decima= l digits =20 > > > > > > > > Works for me. > > > > =20 > > > > > If it's sourced from https://stackoverflow.com/a/10536254 and com= ment, > > > > > don't forget to mention that in whatever implementation / commit > > > > > message. =20 > > > > > > Actually, it's a suggestion from Claude and I double checked the logi= c and math. Please mention that the next time instead of making it look like it's yours. > > > Maybe I should mention Claude and the logic in the commit message ins= tead? =20 Using whatever tool to copy code or descriptions of techniques doesn't make you exempt from licensing requirements, see: https://stackoverflow.com/help/licensing > > 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. =20 >=20 > 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. That's not enough. Just to be very clear, I'm not going to merge anything that violates licensing requirements, no matter if some tool hides them from you. --=20 Stefano