Re: filemode: Add tests.

2024-06-19 Thread Bruno Haible
Hi Collin,

> I ran into a compilation error a long time ago (not Gnulib related)
> because I declared strmode using the mode_t argument unconditionally.
> This is an issue because FreeBSD uses int as the first argument and
> not mode_t, which is typedef'd to __uint16_t. This test is mostly to
> make sure that doesn't happen due to this module. I've added some
> basic functionality tests too.

Thanks for the test!

> NetBSD fixed that declaration in 1997 [1], FreeBSD did in version
> 14.0 [2]. OpenBSD still has it declared with an int argument even
> though mode_t is a __uint32_t there [3] [4] [5].

Also, macOS is in the same camp as OpenBSD here.

Since the test includes , it should depend on the 'unistd'
module. Just to make sure we don't encounter a compilation error on
native Windows.


2024-06-19  Bruno Haible  

filemode tests: Tweak.
* tests/test-filemode.c: Update comment.
* modules/filemode-tests (Depends-on): Add unistd.

diff --git a/modules/filemode-tests b/modules/filemode-tests
index 983bc1e672..985de90272 100644
--- a/modules/filemode-tests
+++ b/modules/filemode-tests
@@ -3,6 +3,7 @@ tests/test-filemode.c
 tests/macros.h
 
 Depends-on:
+unistd
 
 configure.ac:
 
diff --git a/tests/test-filemode.c b/tests/test-filemode.c
index 56c2fdc033..f20211d083 100644
--- a/tests/test-filemode.c
+++ b/tests/test-filemode.c
@@ -24,8 +24,8 @@
 #include 
 
 /* The strmode function is already defined on some systems in  or
-   .  On FreeBSD versions 13 and lower the function takes an int
-   instead of a mode_t (uint16_t) as it's first argument.  Include these
+   .  On macOS, FreeBSD < 14.0, OpenBSD, the function takes an int
+   instead of a mode_t (uint16_t) as its first argument.  Include these
headers here to make sure our declaration doesn't conflict with system
functions.  */
 #include 






u*-vasnprintf tests: Add more tests of %U, %lU, %llU directives

2024-06-19 Thread Bruno Haible
In the %ls and %U directives handling of the u*_vasnprintf functions, the
width handling is tricky. Let me add some more unit tests for this code.


2024-06-19  Bruno Haible  

u*-vasnprintf tests: Add more tests of %U, %lU, %llU directives.
* tests/unistdio/test-u8-printf1.h (test_xfunction): Add more tests of
%U, %lU, %llU directives with width and non-ASCII argument.
* tests/unistdio/test-u16-printf1.h (test_xfunction): Likewise.
* tests/unistdio/test-u32-printf1.h (test_xfunction): Likewise.
* tests/unistdio/test-ulc-vasnprintf2.c (test_function): Likewise.
* tests/unistdio/test-ulc-vasnprintf3.c (test_function): Likewise.

diff --git a/tests/unistdio/test-u16-printf1.h 
b/tests/unistdio/test-u16-printf1.h
index e501c9e462..d2a71153c6 100644
--- a/tests/unistdio/test-u16-printf1.h
+++ b/tests/unistdio/test-u16-printf1.h
@@ -120,6 +120,32 @@ test_xfunction (uint16_t * (*my_xasprintf) (const char *, 
...))
   free (result);
 }
   }
+  { /* Width with a non-ASCII argument.  */
+static const uint8_t unicode_string[] = /* hétérogénéité */
+  "h\303\251t\303\251rog\303\251n\303\251it\303\251";
+uint16_t *result =
+  my_xasprintf ("%20U %d", unicode_string, 33, 44, 55);
+static const uint16_t expected[] =
+  { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't',
+0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9,
+' ', '3', '3', 0
+  };
+ASSERT (result != NULL);
+ASSERT (u16_strcmp (result, expected) == 0);
+free (result);
+  }
+  { /* Width with a non-BMP argument.  */
+static const uint8_t unicode_string[] = "\360\237\220\203"; /* 🐃 */
+uint16_t *result =
+  my_xasprintf ("%10U %d", unicode_string, 33, 44, 55);
+static const uint16_t expected[] =
+  { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 0xd83d,
+0xdc03, ' ', '3', '3', 0
+  };
+ASSERT (result != NULL);
+ASSERT (u16_strcmp (result, expected) == 0);
+free (result);
+  }
 
   {
 static const uint16_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
@@ -178,6 +204,34 @@ test_xfunction (uint16_t * (*my_xasprintf) (const char *, 
...))
   free (result);
 }
   }
+  { /* Width with a non-ASCII argument.  */
+static const uint16_t unicode_string[] = /* hétérogénéité */
+  { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9,
+'i', 't', 0x00e9, 0
+  };
+uint16_t *result =
+  my_xasprintf ("%20lU %d", unicode_string, 33, 44, 55);
+static const uint16_t expected[] =
+  { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't',
+0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9,
+' ', '3', '3', 0
+  };
+ASSERT (result != NULL);
+ASSERT (u16_strcmp (result, expected) == 0);
+free (result);
+  }
+  { /* Width with a non-BMP argument.  */
+static const uint16_t unicode_string[] = { 0xd83d, 0xdc03, 0 }; /* 🐃 */
+uint16_t *result =
+  my_xasprintf ("%10lU %d", unicode_string, 33, 44, 55);
+static const uint16_t expected[] =
+  { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 0xd83d,
+0xdc03, ' ', '3', '3', 0
+  };
+ASSERT (result != NULL);
+ASSERT (u16_strcmp (result, expected) == 0);
+free (result);
+  }
 
   {
 static const uint32_t unicode_string[] = { 'H', 'e', 'l', 'l', 'o', 0 };
@@ -236,6 +290,34 @@ test_xfunction (uint16_t * (*my_xasprintf) (const char *, 
...))
   free (result);
 }
   }
+  { /* Width with a non-ASCII argument.  */
+static const uint32_t unicode_string[] = /* hétérogénéité */
+  { 'h', 0x00e9, 't', 0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9,
+'i', 't', 0x00e9, 0
+  };
+uint16_t *result =
+  my_xasprintf ("%20llU %d", unicode_string, 33, 44, 55);
+static const uint16_t expected[] =
+  { ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'h', 0x00e9, 't',
+0x00e9, 'r', 'o', 'g', 0x00e9, 'n', 0x00e9, 'i', 't', 0x00e9,
+' ', '3', '3', 0
+  };
+ASSERT (result != NULL);
+ASSERT (u16_strcmp (result, expected) == 0);
+free (result);
+  }
+  { /* Width with a non-BMP argument.  */
+static const uint32_t unicode_string[] = { 0x1f403, 0 }; /* 🐃 */
+uint16_t *result =
+  my_xasprintf ("%10llU %d", unicode_string, 33, 44, 55);
+static const uint16_t expected[] =
+  { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 0xd83d,
+0xdc03, ' ', '3', '3', 0
+  };
+ASSERT (result != NULL);
+ASSERT (u16_strcmp (result, expected) == 0);
+free (result);
+  }
 
   /* Test the support of the 's' conversion specifier for strings.  */
 
diff --git a/tests/unistdio/test-u32-printf1.h 
b/tests/unistdio/test-u32-printf1.h
index ae0edc6a63..d7ba2e0998 100644
--- a/tests/unistdio/test-u32-printf1.h
+++ b/tests/unistdio/test-u32-printf1.h
@@ -120,6 +120,32 @@ test_xfunction (uint32_t * (*my_xasprintf) (const char *, 
...))
   free (result);
 }
   }
+  { /* Wi

Re: filemode: Add tests.

2024-06-19 Thread Collin Funk
Hi Bruno,

Bruno Haible wrote:
>> NetBSD fixed that declaration in 1997 [1], FreeBSD did in version
>> 14.0 [2]. OpenBSD still has it declared with an int argument even
>> though mode_t is a __uint32_t there [3] [4] [5].
> 
> Also, macOS is in the same camp as OpenBSD here.

I reported it with a proposed solution to OpenBSD so hopefully it will
get changed at some point [1]. Not sure how to make a suggestion to
the people working on macOS.

> Since the test includes , it should depend on the 'unistd'
> module. Just to make sure we don't encounter a compilation error on
> native Windows.

Ah, okay. I thought that would only be necessary if using a function
from  or macros like SEEK_*, *_FILENO, etc. I know Windows
needs messing around with some platform-specific headers for those.
Good to know, thanks.

Collin

[1] https://marc.info/?l=openbsd-bugs&m=171877409832450&w=2



vasnprintf: some tweaks

2024-06-19 Thread Bruno Haible
Three small tweaks of the *vasnprintf code:


2024-06-19  Bruno Haible  

vasnwprintf: Optimize handling of %c directive.
* lib/vasnprintf.c (VASNPRINTF): Use a single ENSURE_ALLOCATION instead
of two.

2024-06-19  Bruno Haible  

u*-vasnprintf: Fix a rare memory leak.
* lib/vasnprintf.c (VASNPRINTF): Use ENSURE_ALLOCATION_ELSE instead of
ENSURE_ALLOCATION, to free temporary allocations before failing due to
out-of-memory.

2024-06-19  Bruno Haible  

u*-vasnprintf: Improve comments.
* lib/vasnprintf.c (VASNPRINTF): Improve comments.

>From 983a290f1e55997d5648807a7ef95c3b96a7295c Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Wed, 19 Jun 2024 14:39:02 +0200
Subject: [PATCH 1/3] u*-vasnprintf: Improve comments.

* lib/vasnprintf.c (VASNPRINTF): Improve comments.
---
 ChangeLog| 5 +
 lib/vasnprintf.c | 9 ++---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 3c50f00eb3..a449920c84 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-06-19  Bruno Haible  
+
+	u*-vasnprintf: Improve comments.
+	* lib/vasnprintf.c (VASNPRINTF): Improve comments.
+
 2024-06-19  Bruno Haible  
 
 	u*-vasnprintf tests: Add more tests of %U, %lU, %llU directives.
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index 059c1a9cb3..c49beddd26 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -2538,7 +2538,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 {
   /* Use the entire string.  */
   arg_end = arg + u8_strlen (arg);
-  /* The number of characters doesn't matter.  */
+  /* The number of characters doesn't matter,
+ because !has_width and therefore width==0.  */
   characters = 0;
 }
 
@@ -2640,7 +2641,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 {
   /* Use the entire string.  */
   arg_end = arg + u16_strlen (arg);
-  /* The number of characters doesn't matter.  */
+  /* The number of characters doesn't matter,
+ because !has_width and therefore width==0.  */
   characters = 0;
 }
 
@@ -2742,7 +2744,8 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 {
   /* Use the entire string.  */
   arg_end = arg + u32_strlen (arg);
-  /* The number of characters doesn't matter.  */
+  /* The number of characters doesn't matter,
+ because !has_width and therefore width==0.  */
   characters = 0;
 }
 
-- 
2.34.1

>From 4dd9d8a7fa85e9e9223a6c175b4a3fd46d222ff6 Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Wed, 19 Jun 2024 14:57:40 +0200
Subject: [PATCH 2/3] u*-vasnprintf: Fix a rare memory leak.

* lib/vasnprintf.c (VASNPRINTF): Use ENSURE_ALLOCATION_ELSE instead of
ENSURE_ALLOCATION, to free temporary allocations before failing due to
out-of-memory.
---
 ChangeLog|  7 +++
 lib/vasnprintf.c | 31 ++-
 2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index a449920c84..34e01291ae 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-06-19  Bruno Haible  
+
+	u*-vasnprintf: Fix a rare memory leak.
+	* lib/vasnprintf.c (VASNPRINTF): Use ENSURE_ALLOCATION_ELSE instead of
+	ENSURE_ALLOCATION, to free temporary allocations before failing due to
+	out-of-memory.
+
 2024-06-19  Bruno Haible  
 
 	u*-vasnprintf: Improve comments.
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index c49beddd26..ed2052c0ae 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -2580,7 +2580,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 if (converted != result + length)
   {
 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
-{ free (converted); goto out_of_memory; });
+  { free (converted); goto out_of_memory; });
 DCHAR_CPY (result + length, converted, converted_len);
 free (converted);
   }
@@ -2683,7 +2683,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 if (converted != result + length)
   {
 ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
-{ free (converted); goto out_of_memory; });
+  { free (converted); goto out_of_memor

Re: filemode: Add tests.

2024-06-19 Thread Bruno Haible
Hi Collin,

> > Since the test includes , it should depend on the 'unistd'
> > module. Just to make sure we don't encounter a compilation error on
> > native Windows.
>
> Ah, okay. I thought that would only be necessary if using a function
> from  or macros like SEEK_*, *_FILENO, etc.

You can look up the actual problems in detail in the Gnulib documentation.
In this case, for example, doc/posix-headers/unistd.texi describes what
you can expect from a platform's  and what you cannot expect.

Bruno






vasnprintf, u*-vasnprintf: Support string arguments longer than 2 GiB

2024-06-19 Thread Bruno Haible
The next step towards the *zprintf family of functions. Part of step 1 of
.

I added three unit tests for %s with large arguments. (You need to pass option
--with-longrunning-tests to gnulib-tool, in order to include them.) Without
a change to lib/vasnprintf.c, all three failed:

FAIL: test-u8-asnprintf-big
===

../../gltests/unistdio/test-u8-asnprintf-big.c:131: assertion 'errno == ENOMEM' 
failed
FAIL test-u8-asnprintf-big (exit status: 134)

FAIL: test-ulc-asnprintf-big


../../gltests/unistdio/test-ulc-asnprintf-big.c:156: assertion 'errno == 
ENOMEM' failed
FAIL test-ulc-asnprintf-big (exit status: 134)

FAIL: test-vasnprintf-big
=

../../gltests/test-vasnprintf-big.c:131: assertion 'errno == ENOMEM' failed
FAIL test-vasnprintf-big (exit status: 134)

So, here's the vasnprintf update that makes all these tests work.


2024-06-19  Bruno Haible  

ulc-asnprintf tests: Add test of %U, %s directives with large arguments.
* tests/unistdio/test-ulc-asnprintf-big.c: New file, based on
tests/test-vasnprintf-big.c.
* modules/unistdio/ulc-asnprintf-extra-tests: New file.
* modules/unistdio/ulc-asnprintf-tests (Depends-on): Depend on it.

u8-asnprintf tests: Add test of %U, %s directives with large arguments.
* tests/unistdio/test-u8-asnprintf-big.c: New file, based on
tests/test-vasnprintf-big.c.
* modules/unistdio/u8-asnprintf-extra-tests: New file.
* modules/unistdio/u8-asnprintf-tests (Depends-on): Depend on it.

vasnprintf tests: Add test of %s directive with large arguments.
* tests/test-vasnprintf-big.c: New file.
* modules/vasnprintf-extra-tests: New file.
* modules/vasnprintf-tests (Depends-on): Depend on it.

vasnprintf, u*-vasnprintf: Support string arguments longer than 2 GiB.
* lib/vasnprintf.c: Include .
(VASNPRINTF): In 64-bit builds, handle the %s directive ourselves.
(local_strnlen): Adjust #if condition.
* modules/unistdio/u8-vasnprintf (Depends-on): Add stdint.
* modules/unistdio/u8-u8-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u16-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u16-u16-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u32-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u32-u32-vasnprintf (Depends-on): Likewise.
* modules/unistdio/ulc-vasnprintf (Depends-on): Likewise.

>From 4cada7ff4ddfc45f204f042ff2acfbac851eb25f Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Wed, 19 Jun 2024 20:46:47 +0200
Subject: [PATCH 1/4] vasnprintf, u*-vasnprintf: Support string arguments
 longer than 2 GiB.

* lib/vasnprintf.c: Include .
(VASNPRINTF): In 64-bit builds, handle the %s directive ourselves.
(local_strnlen): Adjust #if condition.
* modules/unistdio/u8-vasnprintf (Depends-on): Add stdint.
* modules/unistdio/u8-u8-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u16-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u16-u16-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u32-vasnprintf (Depends-on): Likewise.
* modules/unistdio/u32-u32-vasnprintf (Depends-on): Likewise.
* modules/unistdio/ulc-vasnprintf (Depends-on): Likewise.
---
 ChangeLog   |  14 +++
 lib/vasnprintf.c| 186 +++-
 modules/unistdio/u16-u16-vasnprintf |   1 +
 modules/unistdio/u16-vasnprintf |   1 +
 modules/unistdio/u32-u32-vasnprintf |   1 +
 modules/unistdio/u32-vasnprintf |   1 +
 modules/unistdio/u8-u8-vasnprintf   |   1 +
 modules/unistdio/u8-vasnprintf  |   1 +
 modules/unistdio/ulc-vasnprintf |   1 +
 9 files changed, 206 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 0f89dacf4a..fc98fc99af 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,17 @@
+2024-06-19  Bruno Haible  
+
+	vasnprintf, u*-vasnprintf: Support string arguments longer than 2 GiB.
+	* lib/vasnprintf.c: Include .
+	(VASNPRINTF): In 64-bit builds, handle the %s directive ourselves.
+	(local_strnlen): Adjust #if condition.
+	* modules/unistdio/u8-vasnprintf (Depends-on): Add stdint.
+	* modules/unistdio/u8-u8-vasnprintf (Depends-on): Likewise.
+	* modules/unistdio/u16-vasnprintf (Depends-on): Likewise.
+	* modules/unistdio/u16-u16-vasnprintf (Depends-on): Likewise.
+	* modules/unistdio/u32-vasnprintf (Depends-on): Likewise.
+	* modules/unistdio/u32-u32-vasnprintf (Depends-on): Likewise.
+	* modules/unistdio/ulc-vasnprintf (Depends-on): Likewise.
+
 2024-06-19  Bruno Haible  
 
 	vasnwprintf: Optimize handling of %c directive.
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index 1838ded22d..1178822df8 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -80,6 +80,7 @@
 #endif
 
 #include  /* localeconv() */
+#include  /* PTRDIFF_MAX */
 #include   /

filemode tests: Fix comment.

2024-06-19 Thread Collin Funk
When copying the copyright header from another file I forgot to change
the file description. Oops.

CollinFrom 30b5f5759d1e8a75f9b798b0c20f96734ad52e0f Mon Sep 17 00:00:00 2001
From: Collin Funk 
Date: Wed, 19 Jun 2024 14:52:49 -0700
Subject: [PATCH] filemode tests: Fix comment.

* tests/test-filemode.c: Make the file description accurate.
---
 ChangeLog | 5 +
 tests/test-filemode.c | 2 +-
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index fc37bc6d79..7b780206b9 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2024-06-19  Collin Funk  
+
+	filemode tests: Fix comment.
+	* tests/test-filemode.c: Make the file description accurate.
+
 2024-06-19  Bruno Haible  
 
 	ulc-asnprintf tests: Add test of %U, %s directives with large arguments.
diff --git a/tests/test-filemode.c b/tests/test-filemode.c
index f20211d083..8adf97b790 100644
--- a/tests/test-filemode.c
+++ b/tests/test-filemode.c
@@ -1,4 +1,4 @@
-/* Test the sig2str and str2sig functions.
+/* Test the strmode function.
Copyright (C) 2024 Free Software Foundation, Inc.
 
This file is free software: you can redistribute it and/or modify
-- 
2.45.2



Re: vasnprintf, u*-vasnprintf: Support string arguments longer than 2 GiB

2024-06-19 Thread Bruno Haible
Three more patches in this series:

1) Fix a small mistake that caused gcc warnings.
2) I measured the amount of RAM that the tests need:
 $ /usr/bin/time -f "Max RSS: %M KiB" ./test-...printf-big
   This is consistent with what I see by watching the process through "top".
3) vasnwprintf tests: Add test of %s directive with large arguments.
   This test passes without modifications to lib/vasnprintf.c. The
   corresponding code is in lines 3141..3628.


2024-06-19  Bruno Haible  

vasnwprintf tests: Add test of %s directive with large arguments.
* tests/test-vasnwprintf-big.c: New file, based on
tests/test-vasnprintf-big.c.
* modules/vasnwprintf-extra-tests: New file.
* modules/vasnwprintf-tests (Depends-on): Depend on it.

2024-06-19  Bruno Haible  

vasnprintf, u*-asnprintf tests: Adjust memory need.
* tests/test-vasnprintf-big.c (main): Require 10, not 12, GiB of RAM.
* tests/unistdio/test-u8-asnprintf-big.c (main): Require 15 GiB of RAM.
* tests/unistdio/test-ulc-asnprintf-big.c (main): Likewise.

2024-06-19  Bruno Haible  

u8-asnprintf tests: Fix mistake.
* tests/unistdio/test-u8-asnprintf-big.c: Include .
(main): Use u8_strlen instead of strlen.
* modules/unistdio/u8-asnprintf-extra-tests (Depends-on): Add
unistr/u8-strlen.

>From c97a622affb78e78a3beddeb0f7fa9e23e718ee9 Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Thu, 20 Jun 2024 01:27:12 +0200
Subject: [PATCH 1/3] u8-asnprintf tests: Fix mistake.

* tests/unistdio/test-u8-asnprintf-big.c: Include .
(main): Use u8_strlen instead of strlen.
* modules/unistdio/u8-asnprintf-extra-tests (Depends-on): Add
unistr/u8-strlen.
---
 ChangeLog | 8 
 modules/unistdio/u8-asnprintf-extra-tests | 1 +
 tests/unistdio/test-u8-asnprintf-big.c| 7 ---
 3 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 7b780206b9..04002a54a8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2024-06-19  Bruno Haible  
+
+	u8-asnprintf tests: Fix mistake.
+	* tests/unistdio/test-u8-asnprintf-big.c: Include .
+	(main): Use u8_strlen instead of strlen.
+	* modules/unistdio/u8-asnprintf-extra-tests (Depends-on): Add
+	unistr/u8-strlen.
+
 2024-06-19  Collin Funk  
 
 	filemode tests: Fix comment.
diff --git a/modules/unistdio/u8-asnprintf-extra-tests b/modules/unistdio/u8-asnprintf-extra-tests
index c4f7a18e19..ac223b2b63 100644
--- a/modules/unistdio/u8-asnprintf-extra-tests
+++ b/modules/unistdio/u8-asnprintf-extra-tests
@@ -9,6 +9,7 @@ Depends-on:
 stdbool
 stdint
 physmem
+unistr/u8-strlen
 
 configure.ac:
 AC_CHECK_FUNCS_ONCE([setrlimit])
diff --git a/tests/unistdio/test-u8-asnprintf-big.c b/tests/unistdio/test-u8-asnprintf-big.c
index 25a5880c0a..55eb933639 100644
--- a/tests/unistdio/test-u8-asnprintf-big.c
+++ b/tests/unistdio/test-u8-asnprintf-big.c
@@ -31,6 +31,7 @@
 #include 
 #include 
 #include 
+#include 
 
 #if HAVE_SETRLIMIT
 # include 
@@ -94,7 +95,7 @@ main ()
   }
 else
   {
-ASSERT (strlen (s) == len);
+ASSERT (u8_strlen (s) == len);
 ASSERT (len == n1 + n2 + 3);
 size_t i;
 for (i = 0; i <= len; i++)
@@ -133,7 +134,7 @@ main ()
   }
 else
   {
-ASSERT (strlen (s) == len);
+ASSERT (u8_strlen (s) == len);
 ASSERT (len == n1 + 2);
 size_t i;
 for (i = 0; i <= len; i++)
@@ -170,7 +171,7 @@ main ()
   }
 else
   {
-ASSERT (strlen (s) == len);
+ASSERT (u8_strlen (s) == len);
 ASSERT (len == n1 + 2);
 size_t i;
 for (i = 0; i <= len; i++)
-- 
2.34.1

>From 335ca665041ed16f3da008e34f17c1f7140f7173 Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Thu, 20 Jun 2024 01:28:33 +0200
Subject: [PATCH 2/3] vasnprintf, u*-asnprintf tests: Adjust memory need.

* tests/test-vasnprintf-big.c (main): Require 10, not 12, GiB of RAM.
* tests/unistdio/test-u8-asnprintf-big.c (main): Require 15 GiB of RAM.
* tests/unistdio/test-ulc-asnprintf-big.c (main): Likewise.
---
 ChangeLog   |  7 +++
 tests/test-vasnprintf-big.c | 11 ---
 tests/unistdio/test-u8-asnprintf-big.c  | 11 ---
 tests/unistdio/test-ulc-asnprintf-big.c | 11 ---
 4 files changed, 31 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 04002a54a8..2218550af3 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-06-19  Bruno Haible  
+
+	vasnprintf, u*-asnprintf tests: Adjust memory need.
+	* tests/test-vasnprintf-big.c (main): Require 10, not 12, GiB of RAM.
+	* tests/unistdio/test-u8-asnprintf-big.c (main): Require 1

vasnprintf, u*-vasnprintf: Support huge wide string arguments

2024-06-19 Thread Bruno Haible
Still working on making vasnprintf and u*_vasnprintf able to grok
arguments with more than 2^31 elements. Here: wide strings and the %ls
directive.


2024-06-19  Bruno Haible  

vasnprintf, u*-asnprintf tests: Add test of huge %ls arguments.
* tests/test-vasnprintf-big.c: Include .
(main): Add tests for wide string arguments with > 2^31 wide characters.
* tests/unistdio/test-u8-asnprintf-big.c: Include .
(main): Add tests for wide string arguments with > 2^31 wide characters.
* tests/unistdio/test-ulc-asnprintf-big.c: Include .
(main): Add tests for wide string arguments with > 2^31 wide characters.

vasnprintf, u*-vasnprintf: Support huge wide string arguments.
* lib/vasnprintf.c: (VASNPRINTF): In 64-bit builds, handle the %ls
directive ourselves.
(local_wcslen, local_wcrtomb): Adjust #if condition.

>From 4f25160078b665751a4655b13550e8431ee9381d Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Thu, 20 Jun 2024 03:19:15 +0200
Subject: [PATCH 1/2] vasnprintf, u*-vasnprintf: Support huge wide string
 arguments.

* lib/vasnprintf.c: (VASNPRINTF): In 64-bit builds, handle the %ls
directive ourselves.
(local_wcslen, local_wcrtomb): Adjust #if condition.
---
 ChangeLog| 7 +++
 lib/vasnprintf.c | 8 
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5b465a7eb7..4b9485f6c7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-06-19  Bruno Haible  
+
+	vasnprintf, u*-vasnprintf: Support huge wide string arguments.
+	* lib/vasnprintf.c: (VASNPRINTF): In 64-bit builds, handle the %ls
+	directive ourselves.
+	(local_wcslen, local_wcrtomb): Adjust #if condition.
+
 2024-06-19  Bruno Haible  
 
 	vasnwprintf tests: Add test of %s directive with large arguments.
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index 1178822df8..1036f98498 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -248,7 +248,7 @@ local_strnlen (const char *string, size_t maxlen)
 # endif
 #endif
 
-#if ((!USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_WPRINTF_DIRECTIVE_LC) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)
+#if ((!USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_WPRINTF_DIRECTIVE_LC) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || (PTRDIFF_MAX > INT_MAX) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)
 # if HAVE_WCSLEN
 #  define local_wcslen wcslen
 # else
@@ -290,7 +290,7 @@ local_wcsnlen (const wchar_t *s, size_t maxlen)
 # endif
 #endif
 
-#if ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK) || ((NEED_PRINTF_DIRECTIVE_LC || ENABLE_WCHAR_FALLBACK) && HAVE_WINT_T)) && !WIDE_CHAR_VERSION
+#if ((!USE_SNPRINTF || (PTRDIFF_MAX > INT_MAX) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK) || ((NEED_PRINTF_DIRECTIVE_LC || ENABLE_WCHAR_FALLBACK) && HAVE_WINT_T)) && !WIDE_CHAR_VERSION
 # if ENABLE_WCHAR_FALLBACK
 static size_t
 wctomb_fallback (char *s, wchar_t wc)
@@ -3138,7 +3138,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 }
   }
 #endif
-#if !USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK
+#if WIDE_CHAR_VERSION || !USE_SNPRINTF || (PTRDIFF_MAX > INT_MAX) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK
 else if (dp->conversion == 's'
 # if WIDE_CHAR_VERSION
  && a.arg[dp->arg_index].type != TYPE_WIDE_STRING
@@ -7268,7 +7268,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 errno = ENOMEM;
 goto fail_with_errno;
 
-#if ENABLE_UNISTDIO || (!USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK) || ((NEED_PRINTF_DIRECTIVE_LC || ENABLE_WCHAR_FALLBACK) && HAVE_WINT_T && !WIDE_CHAR_VERSION) || (NEED_WPRINTF_DIRECTIVE_C && WIDE_CHAR_VERSION)
+#if ENABLE_UNISTDIO || (WIDE_CHAR_VERSION || !USE_SNPRINTF || (PTRDIFF_MAX > INT_MAX) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS || ENABLE_WCHAR_FALLBACK) || ((NEED_PRINTF_DIRECTIVE_LC || ENABLE_WCHAR_FALLBACK) && HAVE_WINT_T && !WIDE_CHAR_VERSION) || (NEED_WPRINTF_DIRECTIVE_C && WIDE_CHAR_VERSION)
   fail_with_EILSEQ:
 errno = EILSEQ;
 goto fail_with_errno;
-- 
2.34.1

>From 572d2511a00bb6a608517c4975ee7ad693218c61 Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Thu, 20 Jun 2024 03:24:18 +0200
Subject: [PATCH 2/2] vasnprintf, u*-asnprintf tests: Add test of huge %ls
 arguments.

* tests/test-va

Re: bug#71470: test failure on CentOS 7

2024-06-19 Thread Bruno Haible
[Ccing bug-gnulib]
Collin Funk wrote:
> > CentOS 7 will be supported until 2028.
> > I've updated Gnulib's end-of-life.txt:
> 
> I remember we based our Python 3.7 dependency on CentOS almost being
> almost end of life [1]. Does this change anything there? Or will
> installing some python-3.7 package and using an alias/other method
> suffice?
> 
> [1] https://lists.gnu.org/archive/html/bug-gnulib/2024-03/msg4.html

Good point.

I don't think it changes anything. gnulib-tool is used by developers.
The extended support provided by SUSE is obviously targeted at large
organizations which are slow to migrate (either Linux servers, or Linux
desktops for office users). Development machines are not in this group.

Bruno






Re: [bug-diffutils] bug#71535: multiple defects found by covscan in diffutils-3.10

2024-06-19 Thread Paul Eggert

On 6/13/24 05:34, Wasser Mai wrote:

Error: INTEGER_OVERFLOW (CWE-190):
diffutils-3.10/lib/stackvma.c:198:23: tainted_data_return: Called
function ""read(fd, rof->buffer + rof->filled, size - rof->filled)"",
and a possible return value may be less than zero.
diffutils-3.10/lib/stackvma.c:198:23: cast_overflow: An assign that
casts to a different type, which might trigger an overflow.
diffutils-3.10/lib/stackvma.c:213:23: overflow: The expression
""rof->filled"" is considered to have possibly overflowed.
diffutils-3.10/lib/stackvma.c:198:23: overflow: The expression ""size
- rof->filled"" is deemed overflowed because at least one of its
arguments has overflowed.
diffutils-3.10/lib/stackvma.c:198:23: overflow_sink: ""size -
rof->filled"", which might have underflowed, is passed to ""read(fd,
rof->buffer + rof->filled, size - rof->filled)"". [Note: The source
code implementation of the function has been overridden by a builtin
model.]
#  196| for (;;)
#  197|   {
#  198|->   n = read (fd, rof->buffer +
rof->filled, size - rof->filled);
#  199| if (n < 0 && errno == EINTR)
#  200|   goto retry;"



As near as I can make out, this was the only defect report by Coverity 
that was not a false alarm. I installed the attached patch into Gnulib 
to fix the bug, which appears to be so unlikely that it's not worth 
losing sleep over.


Marking the diffutils bug as done since the other defect reports were 
false alarms.From 459da066b3b4fce2dc6cdfa09840508d5de729d8 Mon Sep 17 00:00:00 2001
From: Paul Eggert 
Date: Thu, 20 Jun 2024 00:19:42 -0400
Subject: [PATCH] sigsegv: avoid unlikely undefined behavior
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem found by Coverity for diffutils and reported by Wasser Mai in:
https://bugs.gnu.org/71535
* lib/stackvma.c (rof_open) [__linux__ || __FreeBSD__ || etc]:
Don’t assume result of ‘read’ fits in int.
Avoid undefined behavior if ‘n + MIN_LEFTOVER’ would overflow.
Also, move a test to be after an (n == 0) test, to help the compiler.
---
 ChangeLog  | 10 ++
 lib/stackvma.c | 10 +-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 1c156b791e..edbf067136 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2024-06-20  Paul Eggert  
+
+	sigsegv: avoid unlikely undefined behavior
+	Problem found by Coverity for diffutils and reported by Wasser Mai in:
+	https://bugs.gnu.org/71535
+	* lib/stackvma.c (rof_open) [__linux__ || __FreeBSD__ || etc]:
+	Don’t assume result of ‘read’ fits in int.
+	Avoid undefined behavior if ‘n + MIN_LEFTOVER’ would overflow.
+	Also, move a test to be after an (n == 0) test, to help the compiler.
+
 2024-06-19  Bruno Haible  
 
 	vasnprintf, u*-asnprintf tests: Add test of huge %ls arguments.
diff --git a/lib/stackvma.c b/lib/stackvma.c
index e93f939556..4424c6f58c 100644
--- a/lib/stackvma.c
+++ b/lib/stackvma.c
@@ -176,7 +176,7 @@ rof_open (struct rofile *rof, const char *filename)
   /* Attempt to read the contents in a single system call.  */
   if (size > MIN_LEFTOVER)
 {
-  int n = read (fd, rof->buffer, size);
+  ssize_t n = read (fd, rof->buffer, size);
   if (n < 0 && errno == EINTR)
 goto retry;
 # if defined __DragonFly__
@@ -186,7 +186,7 @@ rof_open (struct rofile *rof, const char *filename)
   if (n <= 0)
 /* Empty file.  */
 goto fail1;
-  if (n + MIN_LEFTOVER <= size)
+  if (MIN_LEFTOVER <= size - n)
 {
   /* The buffer was sufficiently large.  */
   rof->filled = n;
@@ -201,15 +201,15 @@ rof_open (struct rofile *rof, const char *filename)
   if (n < 0)
 /* Some error.  */
 goto fail1;
-  if (n + MIN_LEFTOVER > size - rof->filled)
-/* Allocate a larger buffer.  */
-break;
   if (n == 0)
 {
   /* Reached the end of file.  */
   close (fd);
   return 0;
 }
+  if (size - rof->filled - n < MIN_LEFTOVER)
+/* Allocate a larger buffer.  */
+break;
   rof->filled += n;
 }
 # else
-- 
2.34.1



Re: [bug-diffutils] bug#71535: multiple defects found by covscan in diffutils-3.10

2024-06-19 Thread Bruno Haible
Hi Paul,

> I installed the attached patch into Gnulib 
> to fix the bug, which appears to be so unlikely that it's not worth 
> losing sleep over.

Thanks! I've propagated it to GNU libsigsegv.

> As near as I can make out, this was the only defect report by Coverity 
> that was not a false alarm.

Well, from my point of view, since the file being read is from the /proc
file system, I assume that such synthetic files constructed by the
kernel are small (in the range of < 16 KB). Anyway, if it helps avoid
an alarm from static code analysis tools, the better.

Bruno






vasnwprintf: Support huge wide string arguments

2024-06-19 Thread Bruno Haible
These two patches add support for wide strings with more than INT_MAX
wide characters to vasnwprintf.

Without the change in lib/vasnprintf.c, the underlying swprintf function
is used, which always returns -1 for such arguments, and the logic in
vasnprintf.c then increases the memory allocation exponentially
(17 GB -> 34 GB -> 68 GB -> 137 GB, which finally fails).

This patch set completes step 1 of
.


2024-06-20  Bruno Haible  

vasnwprintf tests: Add test of huge %ls arguments.
* tests/test-vasnwprintf-big.c (main): Add tests for wide string
arguments with > 2^31 wide characters.

vasnwprintf: Support huge wide string arguments.
* lib/vasnprintf.c: (VASNPRINTF): In 64-bit builds, handle the %ls
directive ourselves.
(local_wcslen): Adjust #if condition.

>From 94d34cfb941880ba7112cefb196a4a0fc9f713de Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Thu, 20 Jun 2024 08:11:53 +0200
Subject: [PATCH 1/2] vasnwprintf: Support huge wide string arguments.

* lib/vasnprintf.c: (VASNPRINTF): In 64-bit builds, handle the %ls
directive ourselves.
(local_wcslen): Adjust #if condition.
---
 ChangeLog| 7 +++
 lib/vasnprintf.c | 4 ++--
 2 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c68af8c9e7..ea01cfe60d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-06-20  Bruno Haible  
+
+	vasnwprintf: Support huge wide string arguments.
+	* lib/vasnprintf.c: (VASNPRINTF): In 64-bit builds, handle the %ls
+	directive ourselves.
+	(local_wcslen): Adjust #if condition.
+
 2024-06-20  Paul Eggert  
 
 	sigsegv: avoid unlikely undefined behavior
diff --git a/lib/vasnprintf.c b/lib/vasnprintf.c
index 1036f98498..8efb9ebee2 100644
--- a/lib/vasnprintf.c
+++ b/lib/vasnprintf.c
@@ -248,7 +248,7 @@ local_strnlen (const char *string, size_t maxlen)
 # endif
 #endif
 
-#if ((!USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_WPRINTF_DIRECTIVE_LC) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || (PTRDIFF_MAX > INT_MAX) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)
+#if ((!USE_SNPRINTF || WIDE_CHAR_VERSION || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (PTRDIFF_MAX > INT_MAX) || !DCHAR_IS_TCHAR || NEED_WPRINTF_DIRECTIVE_LC) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || (PTRDIFF_MAX > INT_MAX) || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_DIRECTIVE_LS) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)
 # if HAVE_WCSLEN
 #  define local_wcslen wcslen
 # else
@@ -2993,7 +2993,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
 }
   }
 #endif
-#if WIDE_CHAR_VERSION && (!DCHAR_IS_TCHAR || NEED_WPRINTF_DIRECTIVE_LC)
+#if WIDE_CHAR_VERSION && ((PTRDIFF_MAX > INT_MAX) || !DCHAR_IS_TCHAR || NEED_WPRINTF_DIRECTIVE_LC)
 else if ((dp->conversion == 's'
   && a.arg[dp->arg_index].type == TYPE_WIDE_STRING)
  || (dp->conversion == 'c'
-- 
2.34.1

>From 45a1b5a72c097fc68b38638a3c56ef42c0f53d73 Mon Sep 17 00:00:00 2001
From: Bruno Haible 
Date: Thu, 20 Jun 2024 08:13:25 +0200
Subject: [PATCH 2/2] vasnwprintf tests: Add test of huge %ls arguments.

* tests/test-vasnwprintf-big.c (main): Add tests for wide string
arguments with > 2^31 wide characters.
---
 ChangeLog|  4 ++
 tests/test-vasnwprintf-big.c | 91 
 2 files changed, 87 insertions(+), 8 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ea01cfe60d..5fea7e958b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,9 @@
 2024-06-20  Bruno Haible  
 
+	vasnwprintf tests: Add test of huge %ls arguments.
+	* tests/test-vasnwprintf-big.c (main): Add tests for wide string
+	arguments with > 2^31 wide characters.
+
 	vasnwprintf: Support huge wide string arguments.
 	* lib/vasnprintf.c: (VASNPRINTF): In 64-bit builds, handle the %ls
 	directive ourselves.
diff --git a/tests/test-vasnwprintf-big.c b/tests/test-vasnwprintf-big.c
index 30f123f15f..1c4411efca 100644
--- a/tests/test-vasnwprintf-big.c
+++ b/tests/test-vasnwprintf-big.c
@@ -55,14 +55,16 @@ main ()
   rl.rlim_cur = rl.rlim_max = 0;
   setrlimit (RLIMIT_CORE, &rl);
 # endif
-  /* The test below needs about 25 GiB of memory:
+  /* The test below needs about 40 GiB of memory:
$ time /usr/bin/time -f "Max RSS: %M KiB" ./test-vasnwprintf-big
-   Max RSS: 26216204 KiB
-   real4m22,540s
-   user4m6,322s
-   sys 0m16,203s
- 5 GiB for the inputs and up to 20 GiB for temporary output buffers.  */
-  double needed = 25.0 * 1024 * 1024 * 1024;
+   Max RSS: 41944784 KiB
+   real5m8,508s
+   user4m38,035s
+   sys 0m30,456s
+ 5 GiB for the inputs in the %s tests,
+ or 20 GiB for the inputs in the %ls tests,
+ and up to 20 GiB for t