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

Fix variable scope warnings #190

Open
wants to merge 4 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions jsmn.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

/* Fixed variable scope warning by Ercan Ersoy. */

#ifndef JSMN_H
#define JSMN_H

Expand Down Expand Up @@ -268,7 +271,9 @@ static int jsmn_parse_string(jsmn_parser *parser, const char *js,
JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
jsmntok_t *tokens, const unsigned int num_tokens) {
int r;
#ifndef JSMN_PARENT_LINKS
int i;
#endif
jsmntok_t *token;
int count = parser->toknext;

Expand Down Expand Up @@ -441,6 +446,9 @@ JSMN_API int jsmn_parse(jsmn_parser *parser, const char *js, const size_t len,
}

if (tokens != NULL) {
#ifdef JSMN_PARENT_LINKS
int i;
#endif
for (i = parser->toknext - 1; i >= 0; i--) {
/* Unmatched opened object or array */
if (tokens[i].start != -1 && tokens[i].end == -1) {
Expand Down
11 changes: 5 additions & 6 deletions test/tests.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Fixed variable scope warnings by Ercan Ersoy. */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
Expand Down Expand Up @@ -97,15 +99,14 @@ int test_string(void) {
}

int test_partial_string(void) {
int r;
unsigned long i;
jsmn_parser p;
jsmntok_t tok[5];
const char *js = "{\"x\": \"va\\\\ue\", \"y\": \"value y\"}";

jsmn_init(&p);
for (i = 1; i <= strlen(js); i++) {
r = jsmn_parse(&p, js, i, tok, sizeof(tok) / sizeof(tok[0]));
int r = jsmn_parse(&p, js, i, tok, sizeof(tok) / sizeof(tok[0]));
if (i == strlen(js)) {
check(r == 5);
check(tokeq(js, tok, 5, JSMN_OBJECT, -1, -1, 2, JSMN_STRING, "x", 1,
Expand All @@ -120,15 +121,14 @@ int test_partial_string(void) {

int test_partial_array(void) {
#ifdef JSMN_STRICT
int r;
unsigned long i;
jsmn_parser p;
jsmntok_t tok[10];
const char *js = "[ 1, true, [123, \"hello\"]]";

jsmn_init(&p);
for (i = 1; i <= strlen(js); i++) {
r = jsmn_parse(&p, js, i, tok, sizeof(tok) / sizeof(tok[0]));
int r = jsmn_parse(&p, js, i, tok, sizeof(tok) / sizeof(tok[0]));
if (i == strlen(js)) {
check(r == 6);
check(tokeq(js, tok, 6, JSMN_ARRAY, -1, -1, 3, JSMN_PRIMITIVE, "1",
Expand All @@ -144,7 +144,6 @@ int test_partial_array(void) {

int test_array_nomem(void) {
int i;
int r;
jsmn_parser p;
jsmntok_t toksmall[10], toklarge[10];
const char *js;
Expand All @@ -155,7 +154,7 @@ int test_array_nomem(void) {
jsmn_init(&p);
memset(toksmall, 0, sizeof(toksmall));
memset(toklarge, 0, sizeof(toklarge));
r = jsmn_parse(&p, js, strlen(js), toksmall, i);
int r = jsmn_parse(&p, js, strlen(js), toksmall, i);
check(r == JSMN_ERROR_NOMEM);

memcpy(toklarge, toksmall, sizeof(toksmall));
Expand Down
4 changes: 3 additions & 1 deletion test/testutil.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Fixed variable scope warning by Ercan Ersoy. */

#ifndef __TEST_UTIL_H__
#define __TEST_UTIL_H__

Expand Down Expand Up @@ -73,7 +75,6 @@ static int tokeq(const char *s, jsmntok_t *tokens, unsigned long numtok, ...) {
static int parse(const char *s, int status, unsigned long numtok, ...) {
int r;
int ok = 1;
va_list args;
jsmn_parser p;
jsmntok_t *t = malloc(numtok * sizeof(jsmntok_t));

Expand All @@ -85,6 +86,7 @@ static int parse(const char *s, int status, unsigned long numtok, ...) {
}

if (status >= 0) {
va_list args;
va_start(args, numtok);
ok = vtokeq(s, t, numtok, args);
va_end(args);
Expand Down