Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify the function cJSON_GetArraySize #781

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -1503,6 +1503,7 @@ static cJSON_bool parse_array(cJSON * const item, parse_buffer * const input_buf
{
goto fail; /* failed to parse value */
}
item->size++;
buffer_skip_whitespace(input_buffer);
}
while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
Expand Down Expand Up @@ -1679,6 +1680,7 @@ static cJSON_bool parse_object(cJSON * const item, parse_buffer * const input_bu
{
goto fail; /* failed to parse value */
}
item->size++;
buffer_skip_whitespace(input_buffer);
}
while (can_access_at_index(input_buffer, 0) && (buffer_at_offset(input_buffer)[0] == ','));
Expand Down Expand Up @@ -1835,17 +1837,9 @@ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array)
return 0;
}

child = array->child;

while(child != NULL)
{
size++;
child = child->next;
}

/* FIXME: Can overflow here. Cannot be fixed without breaking the API */

return (int)size;
return array->size;
}

static cJSON* get_array_item(const cJSON *array, size_t index)
Expand Down Expand Up @@ -1982,6 +1976,7 @@ static cJSON_bool add_item_to_array(cJSON *array, cJSON *item)
array->child->prev = item;
}
}
array->size++;

return true;
}
Expand Down Expand Up @@ -2212,7 +2207,7 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const it
/* last element */
parent->child->prev = item->prev;
}

parent->size--;
/* make sure the detached item doesn't point anywhere anymore */
item->prev = NULL;
item->next = NULL;
Expand Down
2 changes: 2 additions & 0 deletions cJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ typedef struct cJSON

/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
char *string;
/* The item's size include object and array */
int size;
} cJSON;

typedef struct cJSON_Hooks
Expand Down