cmdline: check size of result buffer to avoid overflow
[libcmdline.git] / src / extension_example / parse_obj_list.c
index 0781987..aa743cf 100644 (file)
@@ -46,35 +46,33 @@ struct cmdline_token_ops token_obj_list_ops = {
        .get_help = get_help_obj_list,
 };
 
-int 
-parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res)
+int
+parse_obj_list(cmdline_parse_token_hdr_t *tk, const char *buf, void *res,
+              unsigned ressize)
 {
        struct token_obj_list *tk2 = (struct token_obj_list *)tk;
        struct token_obj_list_data *tkd = &tk2->obj_list_data;
        struct object *o;
        unsigned int token_len = 0;
 
-       if (*buf == 0)
+       if (res && ressize < sizeof(struct object *))
                return -1;
 
-       while(!cmdline_isendoftoken(buf[token_len]))
-               token_len++;
+       token_len = strlen(buf);
 
        SLIST_FOREACH(o, tkd->list, next) {
-               if (token_len != strlen(o->name))
-                       continue;
-               if (strncmp(buf, o->name, token_len))
+               if (strcmp(buf, o->name))
                        continue;
                break;
        }
        if (!o) /* not found */
                return -1;
-        
+
        /* store the address of object in structure */
        if (res)
                *(struct object **)res = o;
 
-        return token_len;
+       return token_len;
 }
 
 int complete_get_nb_obj_list(cmdline_parse_token_hdr_t *tk)
@@ -90,7 +88,7 @@ int complete_get_nb_obj_list(cmdline_parse_token_hdr_t *tk)
        return ret;
 }
 
-int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk, int idx, 
+int complete_get_elt_obj_list(cmdline_parse_token_hdr_t *tk, int idx,
                              char *dstbuf, unsigned int size)
 {
        struct token_obj_list *tk2 = (struct token_obj_list *)tk;