]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
libctf: archive, open: when opening, always set errp to something
authorNick Alcock <nick.alcock@oracle.com>
Mon, 12 May 2025 11:31:00 +0000 (12:31 +0100)
committerNick Alcock <nick.alcock@oracle.com>
Tue, 20 May 2025 13:34:55 +0000 (14:34 +0100)
ctf_arc_import_parent, called by the cached-opening machinery used by
ctf_archive_next and archive-wide lookup functions like
ctf_arc_lookup_symbol, has an err-pointer parameter like all other opening
functions.  Unfortunately it unconditionally initializes it whenever
provided, even if there was no error, which can lead to its being
initialized to an uninitialized value.  This is not technically an
API-contract violation, since we don't define what happens to the error
value except when an error happens, but it is still unpleasant.

Initialize it only when there is an actual error, so we never initialize it
to an uninitialized value.

While we're at it, improve all the opening pathways: on success, set errp to
0, rather than leaving it what it was, reducing the likelihood of
uninitialized error param returns in callers too.  (This is inconsistent
with the treatment of ctf_errno(), but the err value being a parameter
passed in from outside makes the divergence acceptable: in open functions,
you're never going to be overwriting some old error value someone might want
to keep around across multiple calls, some of which are successful and some
of which are not.)

Soup up existing tests to verify all this.

Thanks to Bruce McCulloch for the original patch, and Stephen Brennan for
the report.

libctf/
PR libctf/32903
* ctf-archive.c (ctf_arc_open_internal): Zero errp on success.
(ctf_dict_open_sections): Zero errp at the start.
(ctf_arc_import_parent): Intialize err.
* ctf-open.c (ctf_bufopen): Zero errp at the start.
* testsuite/libctf-lookup/add-to-opened.c: Make sure one-element
archive opens update errp.
* testsuite/libctf-writable/ctf-compressed.c: Make sure real archive
opens update errp.

libctf/ctf-archive.c
libctf/ctf-open.c
libctf/testsuite/libctf-lookup/add-to-opened.c
libctf/testsuite/libctf-writable/ctf-compressed.c

index 4b19cc9cc933655a3edeccba86f2a34f371f30ad..31ef4966334c68edb97b24bfc1ed78d0dec228da 100644 (file)
@@ -552,6 +552,10 @@ ctf_arc_open_internal (const char *filename, int *errp)
      is private.)  */
   arc->ctfa_magic = s.st_size;
   close (fd);
+
+  if (errp)
+    *errp = 0;
+
   return arc;
 
 err_unmap:
@@ -655,6 +659,9 @@ ctf_dict_open_sections (const ctf_archive_t *arc,
                        const char *name,
                        int *errp)
 {
+  if (errp)
+    *errp = 0;
+
   if (arc->ctfi_is_archive)
     {
       ctf_dict_t *ret;
@@ -841,7 +848,7 @@ ctf_arc_import_parent (const ctf_archive_t *arc, ctf_dict_t *fp, int *errp)
 {
   if ((fp->ctf_flags & LCTF_CHILD) && !fp->ctf_parent)
     {
-      int err;
+      int err = 0;
       ctf_dict_t *parent;
       const char *parent_name = fp->ctf_parent_name;
 
index 251940833e3502211c5480b7b44743d5851eda39..676388371ba0c085827fc19812b1b31a94c05d09 100644 (file)
@@ -1685,6 +1685,8 @@ ctf_bufopen (const ctf_sect_t *ctfsect, const ctf_sect_t *symsect,
 
   libctf_init_debug();
 
+  ctf_set_open_errno (errp, 0);
+
   ctf_dprintf ("ctf_bufopen %zi+%zi+%zi bytes: validating\n",
               ctfsect ? ctfsect->cts_size : 0,
               symsect ? symsect->cts_size : 0,
index 96629afe1aad2a7d8e86dbbc161087893083cbb9..700257e8a0ee486ceecb69fcdd01534d7cb40304 100644 (file)
@@ -15,7 +15,7 @@ main (int argc, char *argv[])
   ctf_encoding_t en = { CTF_INT_SIGNED, 0, sizeof (int) };
   unsigned char *ctf_written;
   size_t size;
-  int err;
+  int err = 666;
 
   if (argc != 2)
     {
@@ -25,9 +25,18 @@ main (int argc, char *argv[])
 
   if ((ctf = ctf_open (argv[1], NULL, &err)) == NULL)
     goto open_err;
+
+  /* The error int should be reset on success as well as on error.  */
+  if (err != 0)
+    goto err_err;
+
+  err = 666;
   if ((fp = ctf_dict_open (ctf, NULL, &err)) == NULL)
     goto open_err;
 
+  if (err != 0)
+    goto err_err;
+
   /* Check that various modifications to already-written types
      are prohibited.  */
 
@@ -145,4 +154,8 @@ main (int argc, char *argv[])
  open_err:
   fprintf (stderr, "%s: cannot open: %s\n", argv[0], ctf_errmsg (err));
   return 1;
+
+ err_err:
+  fprintf (stderr, "%s: open error not set to success on success\n", argv[0]);
+  return 1;
 }    
index 4769cdb97f22bd8417b2597cc6e2634d93b739e7..646b6035d1409554b375306ea9be2b860fa75721 100644 (file)
@@ -119,10 +119,14 @@ main (int argc, char *argv[])
 
   /* Dump the header of each archive member, and search for CTF_F_COMPRESS in
      the resulting dump.  */
+  err = 666;
   while ((dump_fp = ctf_archive_next (final_arc, &i, NULL, 0, &err)) != NULL)
     {
       char *dumpstr;
 
+      if (err != 0)
+       fprintf (stderr, "err not set to success on success\n");
+
       while ((dumpstr = ctf_dump (dump_fp, &dump_state, CTF_SECT_HEADER,
                                  NULL, NULL)) != NULL)
        {