diff mbox series

[PR,libstdc++/94747] negating a size_t does not show intent

Message ID 7064cb4c-f45b-5e5b-c6eb-ff14170c4cb2@acm.org
State New
Headers show
Series [PR,libstdc++/94747] negating a size_t does not show intent | expand

Commit Message

Nathan Sidwell May 4, 2020, 5:10 p.m. UTC
Pushed this patch to dynamic_cast.  We negate an offsetof value, which 
although well formed, it doesn't show intent well.  The reason checkers 
trigger on this is that it is a cause of real bugs.  So, negate a 
ptrdiff_t instead.

nathan
diff mbox series

Patch

2020-05-04  Nathan Sidwell  <nathan@acm.org>

	PR libstdc++/94747
	* libsupc++/dyncast.cc (__dynamic_cast): Cast offsetof to
	ptrdiff_t before negation, to show intent more clearly.

diff --git i/libstdc++-v3/libsupc++/dyncast.cc w/libstdc++-v3/libsupc++/dyncast.cc
index 1254471f6e8..7a5f483f9cf 100644
--- i/libstdc++-v3/libsupc++/dyncast.cc
+++ w/libstdc++-v3/libsupc++/dyncast.cc
@@ -49,8 +49,8 @@  __dynamic_cast (const void *src_ptr,    // object started from
   {
   const void *vtable = *static_cast <const void *const *> (src_ptr);
   const vtable_prefix *prefix =
-      adjust_pointer <vtable_prefix> (vtable, 
-				      -offsetof (vtable_prefix, origin));
+    (adjust_pointer <vtable_prefix>
+     (vtable,  -ptrdiff_t (offsetof (vtable_prefix, origin))));
   const void *whole_ptr =
       adjust_pointer <void> (src_ptr, prefix->whole_object);
   const __class_type_info *whole_type = prefix->whole_type;
@@ -63,8 +63,8 @@  __dynamic_cast (const void *src_ptr,    // object started from
   // segfault later trying to use a vbase offset that doesn't exist.
   const void *whole_vtable = *static_cast <const void *const *> (whole_ptr);
   const vtable_prefix *whole_prefix =
-    adjust_pointer <vtable_prefix> (whole_vtable,
-				    -offsetof (vtable_prefix, origin));
+    (adjust_pointer <vtable_prefix>
+     (whole_vtable, -ptrdiff_t (offsetof (vtable_prefix, origin))));
   if (whole_prefix->whole_type != whole_type)
     return NULL;
   
@@ -75,7 +75,8 @@  __dynamic_cast (const void *src_ptr,    // object started from
   if (contained_public_p (result.dst2src))
     // Src is known to be a public base of dst.
     return const_cast <void *> (result.dst_ptr);
-  if (contained_public_p (__class_type_info::__sub_kind (result.whole2src & result.whole2dst)))
+  if (contained_public_p (__class_type_info::__sub_kind
+			  (result.whole2src & result.whole2dst)))
     // Both src and dst are known to be public bases of whole. Found a valid
     // cross cast.
     return const_cast <void *> (result.dst_ptr);