nss: Get rid of alloca usage in makedb's write_output.

Message ID 20230926135528.3517253-1-josimmon@redhat.com
State Superseded
Headers
Series nss: Get rid of alloca usage in makedb's write_output. |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed

Commit Message

Joe Simmons-Talbott Sept. 26, 2023, 1:54 p.m. UTC
  Replace alloca usage with a scratch_buffer.
---
 nss/makedb.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
  

Comments

Andreas Schwab Sept. 28, 2023, 11:16 a.m. UTC | #1
On Sep 26 2023, Joe Simmons-Talbott wrote:

> @@ -802,6 +812,7 @@ write_output (int fd)
>    assert (iov_nelts <= INT_MAX);
>    if (writev (fd, iov, iov_nelts) != keydataoffset)
>      {
> +      scratch_buffer_free (&sbuf);
>        error (0, errno, gettext ("failed to write new database file"));
>        return EXIT_FAILURE;

Does scratch_buffer_free guarantee that errno is not changed?
  
Joe Simmons-Talbott Sept. 28, 2023, 1:14 p.m. UTC | #2
On Thu, Sep 28, 2023 at 01:16:00PM +0200, Andreas Schwab wrote:
> On Sep 26 2023, Joe Simmons-Talbott wrote:
> 
> > @@ -802,6 +812,7 @@ write_output (int fd)
> >    assert (iov_nelts <= INT_MAX);
> >    if (writev (fd, iov, iov_nelts) != keydataoffset)
> >      {
> > +      scratch_buffer_free (&sbuf);
> >        error (0, errno, gettext ("failed to write new database file"));
> >        return EXIT_FAILURE;
> 
> Does scratch_buffer_free guarantee that errno is not changed?

scratch_buffer_free doesn't do anything other than call free when the
buffer has been heap-allocated.  IIUC free preserves errno since 2.33 in
the default free.  So I guess if there is a non-default free that
doesn't preserve errno then there is no explicit guarantee.  Should I
adjust scratch_buffer_free to explicitly preserve errno (in a separate
patch) or just preserve errno around this one call to
scratch_buffer_free?

Thanks,
Joe
  
Andreas Schwab Sept. 28, 2023, 1:32 p.m. UTC | #3
On Sep 28 2023, Joe Simmons-Talbott wrote:

> On Thu, Sep 28, 2023 at 01:16:00PM +0200, Andreas Schwab wrote:
>> On Sep 26 2023, Joe Simmons-Talbott wrote:
>> 
>> > @@ -802,6 +812,7 @@ write_output (int fd)
>> >    assert (iov_nelts <= INT_MAX);
>> >    if (writev (fd, iov, iov_nelts) != keydataoffset)
>> >      {
>> > +      scratch_buffer_free (&sbuf);
>> >        error (0, errno, gettext ("failed to write new database file"));
>> >        return EXIT_FAILURE;
>> 
>> Does scratch_buffer_free guarantee that errno is not changed?
>
> scratch_buffer_free doesn't do anything other than call free when the
> buffer has been heap-allocated.  IIUC free preserves errno since 2.33 in
> the default free.  So I guess if there is a non-default free that
> doesn't preserve errno then there is no explicit guarantee.  Should I
> adjust scratch_buffer_free to explicitly preserve errno (in a separate
> patch) or just preserve errno around this one call to
> scratch_buffer_free?

You could just move the call down.
  
Gabriel Ravier Sept. 28, 2023, 3:52 p.m. UTC | #4
On 9/28/23 14:32, Andreas Schwab wrote:
> On Sep 28 2023, Joe Simmons-Talbott wrote:
>
>> On Thu, Sep 28, 2023 at 01:16:00PM +0200, Andreas Schwab wrote:
>>> On Sep 26 2023, Joe Simmons-Talbott wrote:
>>>
>>>> @@ -802,6 +812,7 @@ write_output (int fd)
>>>>     assert (iov_nelts <= INT_MAX);
>>>>     if (writev (fd, iov, iov_nelts) != keydataoffset)
>>>>       {
>>>> +      scratch_buffer_free (&sbuf);
>>>>         error (0, errno, gettext ("failed to write new database file"));
>>>>         return EXIT_FAILURE;
>>> Does scratch_buffer_free guarantee that errno is not changed?
>> scratch_buffer_free doesn't do anything other than call free when the
>> buffer has been heap-allocated.  IIUC free preserves errno since 2.33 in
>> the default free.  So I guess if there is a non-default free that
>> doesn't preserve errno then there is no explicit guarantee.  Should I
>> adjust scratch_buffer_free to explicitly preserve errno (in a separate
>> patch) or just preserve errno around this one call to
>> scratch_buffer_free?
> You could just move the call down.
>
Hmmm, this solves the issue for this patch but it seems like something 
that would merit further discussion in my opinion - personally I think 
the answer should just be to assume a compliant `free` is present, but 
others might have good arguments as to why this shouldn't be the case.
  

Patch

diff --git a/nss/makedb.c b/nss/makedb.c
index 48c8fe1333..c2779d9d9e 100644
--- a/nss/makedb.c
+++ b/nss/makedb.c
@@ -25,6 +25,7 @@ 
 #include <inttypes.h>
 #include <libintl.h>
 #include <locale.h>
+#include <scratch_buffer.h>
 #include <search.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -739,7 +740,16 @@  write_output (int fd)
   struct nss_db_header *header;
   uint64_t file_offset = (sizeof (struct nss_db_header)
 			  + (ndatabases * sizeof (header->dbs[0])));
-  header = alloca (file_offset);
+  struct scratch_buffer sbuf;
+  scratch_buffer_init (&sbuf);
+
+
+  if (!scratch_buffer_set_array_size (&sbuf, 1, file_offset))
+    {
+      error (0, errno, gettext ("failed to allocate memory"));
+      return EXIT_FAILURE;
+    }
+  header = sbuf.data;
 
   header->magic = NSS_DB_MAGIC;
   header->ndbs = ndatabases;
@@ -802,6 +812,7 @@  write_output (int fd)
   assert (iov_nelts <= INT_MAX);
   if (writev (fd, iov, iov_nelts) != keydataoffset)
     {
+      scratch_buffer_free (&sbuf);
       error (0, errno, gettext ("failed to write new database file"));
       return EXIT_FAILURE;
     }
@@ -810,6 +821,7 @@  write_output (int fd)
   DIAG_POP_NEEDS_COMMENT;
 #endif
 
+  scratch_buffer_free (&sbuf);
   return EXIT_SUCCESS;
 }