Merge branch 'jk/type-from-string-gently' into maint
authorJunio C Hamano <gitster@pobox.com>
Wed, 13 May 2015 21:05:54 +0000 (14:05 -0700)
committerJunio C Hamano <gitster@pobox.com>
Wed, 13 May 2015 21:05:54 +0000 (14:05 -0700)
"git cat-file bl $blob" failed to barf even though there is no
object type that is "bl".

* jk/type-from-string-gently:
type_from_string_gently: make sure length matches

1  2 
object.c
diff --combined object.c
index 23d6c9671904cfe273fc16de8a4d2db56b91c3b4,91bdde25307ebfd853082c7e38d3806b388b08d5..980ac5fcdfa7654667c9902aa56407f1ce7053ba
+++ b/object.c
@@@ -41,7 -41,8 +41,8 @@@ int type_from_string_gently(const char 
                len = strlen(str);
  
        for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
-               if (!strncmp(str, object_type_strings[i], len))
+               if (!strncmp(str, object_type_strings[i], len) &&
+                   object_type_strings[i][len] == '\0')
                        return i;
  
        if (gentle)
@@@ -307,9 -308,10 +308,9 @@@ int object_list_contains(struct object_
   */
  static char object_array_slopbuf[1];
  
 -static void add_object_array_with_mode_context(struct object *obj, const char *name,
 -                                             struct object_array *array,
 -                                             unsigned mode,
 -                                             struct object_context *context)
 +void add_object_array_with_path(struct object *obj, const char *name,
 +                              struct object_array *array,
 +                              unsigned mode, const char *path)
  {
        unsigned nr = array->nr;
        unsigned alloc = array->alloc;
  
        if (nr >= alloc) {
                alloc = (alloc + 32) * 2;
 -              objects = xrealloc(objects, alloc * sizeof(*objects));
 +              REALLOC_ARRAY(objects, alloc);
                array->alloc = alloc;
                array->objects = objects;
        }
        else
                entry->name = xstrdup(name);
        entry->mode = mode;
 -      entry->context = context;
 +      if (path)
 +              entry->path = xstrdup(path);
 +      else
 +              entry->path = NULL;
        array->nr = ++nr;
  }
  
  void add_object_array(struct object *obj, const char *name, struct object_array *array)
  {
 -      add_object_array_with_mode(obj, name, array, S_IFINVALID);
 +      add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
  }
  
 -void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
 +/*
 + * Free all memory associated with an entry; the result is
 + * in an unspecified state and should not be examined.
 + */
 +static void object_array_release_entry(struct object_array_entry *ent)
  {
 -      add_object_array_with_mode_context(obj, name, array, mode, NULL);
 -}
 -
 -void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context)
 -{
 -      if (context)
 -              add_object_array_with_mode_context(obj, name, array, context->mode, context);
 -      else
 -              add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context);
 +      if (ent->name != object_array_slopbuf)
 +              free(ent->name);
 +      free(ent->path);
  }
  
  void object_array_filter(struct object_array *array,
                                objects[dst] = objects[src];
                        dst++;
                } else {
 -                      if (objects[src].name != object_array_slopbuf)
 -                              free(objects[src].name);
 +                      object_array_release_entry(&objects[src]);
                }
        }
        array->nr = dst;
  }
  
 +void object_array_clear(struct object_array *array)
 +{
 +      int i;
 +      for (i = 0; i < array->nr; i++)
 +              object_array_release_entry(&array->objects[i]);
 +      free(array->objects);
 +      array->objects = NULL;
 +      array->nr = array->alloc = 0;
 +}
 +
  /*
   * Return true iff array already contains an entry with name.
   */
@@@ -409,7 -401,8 +410,7 @@@ void object_array_remove_duplicates(str
                                objects[array->nr] = objects[src];
                        array->nr++;
                } else {
 -                      if (objects[src].name != object_array_slopbuf)
 -                              free(objects[src].name);
 +                      object_array_release_entry(&objects[src]);
                }
        }
  }