diff mbox

[C++] class method vector

Message ID e4c64705-0e29-b57d-f67c-0da21f0df5ee@acm.org
State New
Headers show

Commit Message

Nathan Sidwell June 16, 2017, 2:52 p.m. UTC
The class method vector needs resorting on PCH readin, and module 
readback.  It suffered an unfortunate signed/unsigned comparison 
collision, that I hit.  Reworked to be made safer.

Of course this thing should really be a hash table + vector of type 
conversions, but that's another story.

applied to trunk.

nathan
diff mbox

Patch

2017-06-16  Nathan Sidwell  <nathan@acm.org>

	* class.c (resort_type_method_vec): Avoid potential unsigned
	overflow.

Index: class.c
===================================================================
--- class.c	(revision 249264)
+++ class.c	(working copy)
@@ -2328,25 +2328,25 @@  resort_type_method_vec (void* obj,
 			gt_pointer_operator new_value,
 			void* cookie)
 {
-  vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj;
-  int len = vec_safe_length (method_vec);
-  size_t slot;
-  tree fn;
+  if (vec<tree, va_gc> *method_vec = (vec<tree, va_gc> *) obj)
+    {
+      int len = method_vec->length ();
+      int slot;
 
-  /* The type conversion ops have to live at the front of the vec, so we
-     can't sort them.  */
-  for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
-       vec_safe_iterate (method_vec, slot, &fn);
-       ++slot)
-    if (!DECL_CONV_FN_P (OVL_FIRST (fn)))
-      break;
+      /* The type conversion ops have to live at the front of the vec, so we
+	 can't sort them.  */
+      for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
+	   slot < len; slot++)
+	if (!DECL_CONV_FN_P (OVL_FIRST ((*method_vec)[slot])))
+	  break;
 
-  if (len - slot > 1)
-    {
-      resort_data.new_value = new_value;
-      resort_data.cookie = cookie;
-      qsort (method_vec->address () + slot, len - slot, sizeof (tree),
-	     resort_method_name_cmp);
+      if (len > slot + 1)
+	{
+	  resort_data.new_value = new_value;
+	  resort_data.cookie = cookie;
+	  qsort (method_vec->address () + slot, len - slot, sizeof (tree),
+		 resort_method_name_cmp);
+	}
     }
 }