]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
diagnostic, pch: Fix up the new diagnostic PCH methods for ubsan checking [PR116936]
authorJakub Jelinek <jakub@redhat.com>
Fri, 4 Oct 2024 12:02:13 +0000 (14:02 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 4 Oct 2024 12:02:13 +0000 (14:02 +0200)
The PR notes that the new pch_save/pch_restore methods I've added
recently invoke UB if either m_classification_history.address ()
or m_push_list.address () is NULL (which can happen if those vectors
are empty (and in the pch_save case nothing has been pushed into them
before either).  While the corresponding length is necessarily 0,
fwrite (NULL, something, 0, f) or
fread (NULL, something, 0, f) still invoke UB.

The following patch fixes that by not calling fwrite/fread if the
corresponding length is 0.

2024-10-04  Jakub Jelinek  <jakub@redhat.com>

PR pch/116936
* diagnostic.cc (diagnostic_option_classifier::pch_save): Only call
fwrite if corresponding length is non-zero.
(diagnostic_option_classifier::pch_restore): Only call fread if
corresponding length is non-zero.

gcc/diagnostic.cc

index 73ed2ea154cc770dc2c1996d0a95d2d2a6811fe3..27ac2bd67b9bc7ee6af8e83f098344d41f55e458 100644 (file)
@@ -167,11 +167,13 @@ diagnostic_option_classifier::pch_save (FILE *f)
   unsigned int lengths[2] = { m_classification_history.length (),
                              m_push_list.length () };
   if (fwrite (lengths, sizeof (lengths), 1, f) != 1
-      || fwrite (m_classification_history.address (),
-                sizeof (diagnostic_classification_change_t),
-                lengths[0], f) != lengths[0]
-      || fwrite (m_push_list.address (), sizeof (int),
-                lengths[1], f) != lengths[1])
+      || (lengths[0]
+         && fwrite (m_classification_history.address (),
+                    sizeof (diagnostic_classification_change_t),
+                    lengths[0], f) != lengths[0])
+      || (lengths[1]
+         && fwrite (m_push_list.address (), sizeof (int),
+                    lengths[1], f) != lengths[1]))
     return -1;
   return 0;
 }
@@ -189,11 +191,13 @@ diagnostic_option_classifier::pch_restore (FILE *f)
   gcc_checking_assert (m_push_list.is_empty ());
   m_classification_history.safe_grow (lengths[0]);
   m_push_list.safe_grow (lengths[1]);
-  if (fread (m_classification_history.address (),
-            sizeof (diagnostic_classification_change_t),
-            lengths[0], f) != lengths[0]
-      || fread (m_push_list.address (), sizeof (int),
-               lengths[1], f) != lengths[1])
+  if ((lengths[0]
+       && fread (m_classification_history.address (),
+                sizeof (diagnostic_classification_change_t),
+                lengths[0], f) != lengths[0])
+      || (lengths[1]
+         && fread (m_push_list.address (), sizeof (int),
+                   lengths[1], f) != lengths[1]))
     return -1;
   return 0;
 }