From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31B762D97BE for ; Sun, 4 Jan 2026 18:34:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767551697; cv=none; b=vEg+UiJp13xRwuwI7K3ytTYzg53uam1EfxFKtsFKmTAB0D+J6te0VL1x3GN48rkEDwyLivmMTSLsL9G2ee2PImXn0lUWqGjsHZ5A41EjgS/I64rfGU1aM1pC9yibd4WJYzYk/HW0LsMCi73S9cVx0NoOzD1hi/PFJM3P8O+FDDw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1767551697; c=relaxed/simple; bh=0ylrhsdcDwW2eDjmKJMKlxEh+GOAJQy9Am+atzBYziI=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=tGQvtmkMmEkIXlRt2cWFSgKgvMq43Qo2/yl/KxaHW2b7d6qbgrcMfrmqhORgqmKXNbD+NC7mRGxM11VykDn8TK+VvBYaoiDIW7w87f5Fk4w7dM0QW3DLYoVpTZJTJOZG8W+5WndNPTl7mQbDda+ZwULzpfV8dLHyCu23aWDFN54= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=BJ+HnnOH; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="BJ+HnnOH" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-47774d3536dso10350675e9.0 for ; Sun, 04 Jan 2026 10:34:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767551694; x=1768156494; darn=vger.kernel.org; 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=hyPDr9SXPoe/VpTMv3ueCLHsPyMGZqgCEZn9uKBWVRk=; b=BJ+HnnOHqa9FhxVgIgjIxrnBOHN4FOu3wip6hVL4NayIJ/lIJ/3RaFh02Z92UHR7Z1 1aPInZDV4+b/ADGehb8aiVe5DMnnszbZwoHuflhVrYaXAvHsO+8+1cbkTRYR59VK9F19 W77F/8F5kBadxCWEqaWxmTzWJwkhhZopfOKaC6vpq0BGjpL+I7N53KL43XkAw1yvn2kN PPWNpmP68gS2HA9NLjp7fsNAITlN+PUVjuLIGR/N8w4GKvcJ7huZ9MhyWhVzj65enacK mHwK5Nc2ViwTDU2mbU+erIp+CsxKZH7Nb3kLBd+g3P649Kjm6daOMHUS8l5UEMGvNKM+ yplA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767551694; x=1768156494; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=hyPDr9SXPoe/VpTMv3ueCLHsPyMGZqgCEZn9uKBWVRk=; b=D33+jvT2cvN+b91pTo/i2T2Ce+Xu+DitSnWoelp3yP5XOJ0LCO/hajySd0dggur6QD BgLlNdPll/gUO39csCcrR04AO7RyJNSCnG+aVHsp5GVKXEcibxpfFXkQBH8/sgEJHBUP 8iyRJTvaH3JSSwGiE2RkdgzeEstTtlRH25uZtoQHr1hA9yjdoMkvm/bDITsaXJ52e5p1 LP8TMPdy8L5uPpn9P5MpA2gRheylx5h34VYPpOzDYpZm2ZXkrQjdnqvpi0n/AwHM+9Hj ccLFST6etxHk8XJmVrpTEPe0VGorI5nruYekcpjoa106CdDxQICOgGLLj3Vp4NKZN945 qoRg== X-Forwarded-Encrypted: i=1; AJvYcCXD0xcV8+Z9oUsmdDN/wf+8FD9WRDKQf1YaEntijLQnGTION0BLDG/Ky6CsCdck/dh+mxaCYgLY03OH9rg=@vger.kernel.org X-Gm-Message-State: AOJu0Ywk9/rnoP9FdlOFHilbKCJrC0aOlT2IiK2rE+KKcAr1Oln+cgsU zpbYHdiPn58kr39T1k3qYg+Xj3ErcORD9IqRxYUHzDooWv6M1gCCl38v8TMwxw== X-Gm-Gg: AY/fxX5mULvZQHYdDisV2WIAmmlmmB+UxVmgmZdUcfA82BV2bjFGLdgzL1a503hvrcS uWQJmpCSNaZ1eJJ9E0qgg1UDxCB/HgVXKQtEM+LOykKH1UaFhM05fgNP++y8SRfCCKXQzrIzLtv sL4qraI9Vtq0T2/ICP27swpHlfHqsRE6tKscbtpf/dAduNxGN7NnHYBKHfIhew7SA8giN6FPILw IUdzCB4K8j/VYqeAAACmEywjovlvQansoo0DBQmDrScGTIaNPWt1121tzOpd/NrE70AfK0ZN31z LW7pyR6x5VEhXRB4R+HNI2+azhHIAwA+OC+craUiPEU6Mjprsljxu4lxpgfWcjT4JmwvEVwinHt HLiiNn2k1ZHTdGgAGgu4EkmpnnZov5Abkv1eIUbJCfHVO/6syvWuEf/ABtfxmamT3OJQGI706j7 Vz6DLRrs2hcYkC8r3AopusaA88UR9m2L1e+qxVJQ1EUH5eeRxHAglc X-Google-Smtp-Source: AGHT+IEFTdZCrLVIFKCJ5BZAEAuVRJ2aKZ1pu7sBWJkRSCWBBu67pJLZXv5xGE29CWVEr2OKIUFgpQ== X-Received: by 2002:a05:600c:4846:b0:477:75b4:d2d1 with SMTP id 5b1f17b1804b1-47d6ba99787mr50111255e9.15.1767551694280; Sun, 04 Jan 2026 10:34:54 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4324ea8311fsm96357613f8f.28.2026.01.04.10.34.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 04 Jan 2026 10:34:54 -0800 (PST) Date: Sun, 4 Jan 2026 18:34:52 +0000 From: David Laight To: Daniel Palmer Cc: w@1wt.eu, linux@weissschuh.net, linux-kernel@vger.kernel.org Subject: Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h Message-ID: <20260104183452.57213367@pumpkin> In-Reply-To: <20260104083837.1390041-2-daniel@thingy.jp> References: <20260104083837.1390041-1-daniel@thingy.jp> <20260104083837.1390041-2-daniel@thingy.jp> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 4 Jan 2026 17:38:36 +0900 Daniel Palmer wrote: > Add a very basic version of fread() like we already have for fwrite(). > > Signed-off-by: Daniel Palmer > --- > tools/include/nolibc/stdio.h | 35 ++++++++++++++++++++++++++++++++++- > 1 file changed, 34 insertions(+), 1 deletion(-) > > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h > index 1f16dab2ac88..21569ebae824 100644 > --- a/tools/include/nolibc/stdio.h > +++ b/tools/include/nolibc/stdio.h > @@ -170,7 +170,7 @@ int putchar(int c) > } > > > -/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ > +/* fwrite(), fread(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ > > /* internal fwrite()-like function which only takes a size and returns 0 on > * success or EOF on error. It automatically retries on short writes. > @@ -204,6 +204,39 @@ size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) > return written; > } > > +/* internal fread()-like function which only takes a size and returns 0 on > + * success or EOF on error. It automatically retries on short reads. > + */ > +static __attribute__((unused)) > +int _fread(void *buf, size_t size, FILE *stream) > +{ > + ssize_t ret; > + int fd = fileno(stream); > + > + while (size) { > + ret = read(fd, buf, size); > + if (ret <= 0) > + return EOF; You need to return a partial length if some data was read before EOF. > + size -= ret; > + buf += ret; > + } > + return 0; > +} > + > + > +static __attribute__((unused)) > +size_t fread(void *s, size_t size, size_t nmemb, FILE *stream) > +{ > + size_t readed; Isn't it enough to just multiply 'size' and 'nmemb' together and then do a single long read() (maybe with retries for short reads). The multiply can't overflow (for a valid request) because the buffer size is also limited to 'size_t'. OTOH Linux limits the maximum return from read() to INT_MAX - PAGE_SIZE (even on 64bit). David > + > + for (readed = 0; readed < nmemb; readed++) { > + if (_fread(s, size, stream) != 0) > + break; > + s += size; > + } > + return readed; > +} > + > static __attribute__((unused)) > int fputs(const char *s, FILE *stream) > {