]> git.openfabrics.org - ~shefty/rdma-win.git/commitdiff
etc/search: handle malloc failures and fix build warnings
authorshefty <shefty@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Wed, 23 Sep 2009 21:19:52 +0000 (21:19 +0000)
committershefty <shefty@ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86>
Wed, 23 Sep 2009 21:19:52 +0000 (21:19 +0000)
Signed-off-by: Sean Hefty <sean.hefty@intel.com>
git-svn-id: svn://openib.tc.cornell.edu/gen1@2454 ad392aa1-c5ef-ae45-8dd8-e69d62a5ef86

branches/winverbs/etc/user/search.c

index 4e47c6164d9b7a0e1cb9d6960547a649c1c18ed4..b2a2098ecc3e58ba224da419b3d49edb5c8c22d9 100644 (file)
@@ -48,18 +48,21 @@ void *tsearch(const void *key, void **rootp,
 \r
        if (!*rootp) {\r
                *rootp = malloc(sizeof(cl_fmap_t));\r
+               if (!*rootp)\r
+                       return NULL;\r
                cl_fmap_init((cl_fmap_t *) *rootp, fcompare);\r
        }\r
 \r
        compare = compar;\r
        item = malloc(sizeof(cl_fmap_item_t));\r
+       if (!item)\r
+               return NULL;\r
+\r
        map_item = cl_fmap_insert((cl_fmap_t *) *rootp, key, item);\r
-       if (map_item != item) {\r
+       if (map_item != item)\r
                free(item);\r
-               return (void *) map_item->p_key;\r
-       }\r
 \r
-       return (void *) key;\r
+       return (void *) &map_item->p_key;\r
 }\r
 \r
 void *tfind(const void *key, void *const *rootp,\r
@@ -75,9 +78,14 @@ void *tfind(const void *key, void *const *rootp,
        if (item == cl_fmap_end((cl_fmap_t *) *rootp))\r
                return NULL;\r
 \r
-       return (void *) item->p_key;\r
+       return (void *) &item->p_key;\r
 }\r
 \r
+/*\r
+ * Returns NULL if item is not found, or the item itself.  This differs\r
+ * from the tdelete call by not retuning the parent item, but works if\r
+ * the user is only checking against NULL.\r
+ */\r
 void *tdelete(const void *key, void **rootp,\r
                          int (*compar)(const void *, const void *))\r
 {\r