Skip to content

Commit 0c84275

Browse files
committed
Adds stability
1 parent f03af33 commit 0c84275

File tree

11 files changed

+81
-31
lines changed

11 files changed

+81
-31
lines changed

src/json_object.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ inline static void popCallback(jsonsl_t jsn, jsonsl_action_t action, struct json
169169
}
170170
}
171171

172-
int CreateNodeFromJSON(const char *buf, size_t len, Node **node, sds *err) {
172+
int CreateNodeFromJSON(const char *buf, size_t len, Node **node, char **err) {
173173
int levels = JSONSL_MAX_LEVELS; // TODO: heur levels from len since we're not really streaming?
174174

175175
size_t _off = 0, _len = len;
@@ -248,7 +248,7 @@ int CreateNodeFromJSON(const char *buf, size_t len, Node **node, sds *err) {
248248
error:
249249
// set error string, if one has been passed
250250
if (err) {
251-
*err = strdup(serr);
251+
*err = rmstrndup(serr, strlen(serr));
252252
}
253253

254254
// free any nodes that are in the stack

src/json_object.h

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <sds.h>
2727
#include <stdlib.h>
2828
#include "object.h"
29+
#include "rmstrndup.h"
2930
#include "redismodule.h"
3031

3132
#define JSONOBJECT_OK 0

src/object.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Node *NewIntNode(int64_t val) {
4343

4444
Node *NewStringNode(const char *s, uint32_t len) {
4545
Node *ret = __newNode(N_STRING);
46-
ret->value.strval.data = rmalloc_strndup(s, len);
46+
ret->value.strval.data = rmstrndup(s, len);
4747
ret->value.strval.len = len;
4848
return ret;
4949
}
@@ -52,7 +52,7 @@ Node *NewCStringNode(const char *s) { return NewStringNode(s, strlen(s)); }
5252

5353
Node *NewKeyValNode(const char *key, uint32_t len, Node *n) {
5454
Node *ret = __newNode(N_KEYVAL);
55-
ret->value.kvval.key = rmalloc_strndup(key, len);
55+
ret->value.kvval.key = rmstrndup(key, len);
5656
ret->value.kvval.val = n;
5757
return ret;
5858
}

src/object.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
#include <string.h>
2525
#include <sys/param.h>
2626
#include <vector.h>
27-
#include <alloc.h>
2827
#include "redismodule.h"
28+
#include "rmstrndup.h"
2929

3030
// Return code from successful ops
3131
#define OBJ_OK 0

src/path.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void SearchPath_AppendIndex(SearchPath *p, int idx) {
113113
void SearchPath_AppendKey(SearchPath *p, const char *key, const size_t len) {
114114
PathNode pn;
115115
pn.type = NT_KEY;
116-
pn.value.key = rmalloc_strndup(key, len);
116+
pn.value.key = rmstrndup(key, len);
117117
__searchPath_append(p, pn);
118118
}
119119

src/path.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020

2121
#include <string.h>
2222
#include <sys/param.h>
23-
#include <alloc.h>
2423
#include "object.h"
2524
#include "redismodule.h"
25+
#include "rmstrndup.h"
2626

2727
/* The type of a path node */
2828
typedef enum {

src/rejson.c

+12-12
Original file line numberDiff line numberDiff line change
@@ -534,11 +534,11 @@ int JSONSet_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc
534534

535535
// Create object from json
536536
Object *jo = NULL;
537-
sds jerr = NULL;
537+
char *jerr = NULL;
538538
if (JSONOBJECT_OK != CreateNodeFromJSON(json, jsonlen, &jo, &jerr)) {
539539
if (jerr) {
540540
RedisModule_ReplyWithError(ctx, jerr);
541-
sdsfree(jerr);
541+
RedisModule_Free(jerr);
542542
} else {
543543
RM_LOG_WARNING(ctx, "%s", REJSON_ERROR_JSONOBJECT_ERROR);
544544
RedisModule_ReplyWithError(ctx, REJSON_ERROR_JSONOBJECT_ERROR);
@@ -1027,11 +1027,11 @@ int JSONNum_GenericCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int ar
10271027
// we use the json parser to convert the bval arg into a value to catch all of JSON's syntices
10281028
size_t vallen;
10291029
const char *val = RedisModule_StringPtrLen(argv[(4 == argc ? 3 : 2)], &vallen);
1030-
sds jerr = NULL;
1030+
char *jerr = NULL;
10311031
if (JSONOBJECT_OK != CreateNodeFromJSON(val, vallen, &joval, &jerr)) {
10321032
if (jerr) {
10331033
RedisModule_ReplyWithError(ctx, jerr);
1034-
sdsfree(jerr);
1034+
RedisModule_Free(jerr);
10351035
} else {
10361036
RM_LOG_WARNING(ctx, "%s", REJSON_ERROR_JSONOBJECT_ERROR);
10371037
RedisModule_ReplyWithError(ctx, REJSON_ERROR_JSONOBJECT_ERROR);
@@ -1165,11 +1165,11 @@ int JSONStrAppend_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, in
11651165

11661166
// make an object from the JSON value
11671167
Object *jo = NULL;
1168-
sds jerr = NULL;
1168+
char *jerr = NULL;
11691169
if (JSONOBJECT_OK != CreateNodeFromJSON(json, jsonlen, &jo, &jerr)) {
11701170
if (jerr) {
11711171
RedisModule_ReplyWithError(ctx, jerr);
1172-
sdsfree(jerr);
1172+
RedisModule_Free(jerr);
11731173
} else {
11741174
RM_LOG_WARNING(ctx, "%s", REJSON_ERROR_JSONOBJECT_ERROR);
11751175
RedisModule_ReplyWithError(ctx, REJSON_ERROR_JSONOBJECT_ERROR);
@@ -1272,12 +1272,12 @@ int JSONArrInsert_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, in
12721272

12731273
// create object from json
12741274
Object *jo = NULL;
1275-
sds jerr = NULL;
1275+
char *jerr = NULL;
12761276
if (JSONOBJECT_OK != CreateNodeFromJSON(json, jsonlen, &jo, &jerr)) {
12771277
Node_Free(sub);
12781278
if (jerr) {
12791279
RedisModule_ReplyWithError(ctx, jerr);
1280-
sdsfree(jerr);
1280+
RedisModule_Free(jerr);
12811281
} else {
12821282
RM_LOG_WARNING(ctx, "%s", REJSON_ERROR_JSONOBJECT_ERROR);
12831283
RedisModule_ReplyWithError(ctx, REJSON_ERROR_JSONOBJECT_ERROR);
@@ -1367,12 +1367,12 @@ int JSONArrAppend_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, in
13671367

13681368
// create object from json
13691369
Object *jo = NULL;
1370-
sds jerr = NULL;
1370+
char *jerr = NULL;
13711371
if (JSONOBJECT_OK != CreateNodeFromJSON(json, jsonlen, &jo, &jerr)) {
13721372
Node_Free(sub);
13731373
if (jerr) {
13741374
RedisModule_ReplyWithError(ctx, jerr);
1375-
sdsfree(jerr);
1375+
RedisModule_Free(jerr);
13761376
} else {
13771377
RM_LOG_WARNING(ctx, "%s", REJSON_ERROR_JSONOBJECT_ERROR);
13781378
RedisModule_ReplyWithError(ctx, REJSON_ERROR_JSONOBJECT_ERROR);
@@ -1466,11 +1466,11 @@ int JSONArrIndex_RedisCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int
14661466

14671467
// create an object from json
14681468
Object *jo = NULL;
1469-
sds jerr = NULL;
1469+
char *jerr = NULL;
14701470
if (JSONOBJECT_OK != CreateNodeFromJSON(json, jsonlen, &jo, &jerr)) {
14711471
if (jerr) {
14721472
RedisModule_ReplyWithError(ctx, jerr);
1473-
sdsfree(jerr);
1473+
RedisModule_Free(jerr);
14741474
} else {
14751475
RM_LOG_WARNING(ctx, "%s", REJSON_ERROR_JSONOBJECT_ERROR);
14761476
RedisModule_ReplyWithError(ctx, REJSON_ERROR_JSONOBJECT_ERROR);

src/rmstrndup.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2016 Redis Labs
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
#include <stddef.h>
18+
#include <string.h>
19+
#include "redismodule.h"
20+
#include "rmstrndup.h"
21+
22+
/* A patched implementation of strdup that will use our patched calloc */
23+
char *rmstrndup(const char *s, size_t n) {
24+
char *ret = RedisModule_Calloc(n + 1, sizeof(char));
25+
if (ret)
26+
memcpy(ret, s, n);
27+
return ret;
28+
}

src/rmstrndup.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2016 Redis Labs
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Affero General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Affero General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
#include <stddef.h>
18+
#include <string.h>
19+
#include "redismodule.h"
20+
21+
#ifndef __RMSTRNDUP_H__
22+
#define __RMSTRNDUP_H__
23+
24+
char *rmstrndup(const char *s, size_t n);
25+
26+
#endif

test/Makefile

+4-12
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,21 @@ export CFLAGS
2929
export DEBUGFLAGS
3030

3131
all: build test
32-
test: test_object test_json_object test_json_validator test_json_printer unittest
33-
build: object json_object json_printer json_validator
32+
test: test_object test_json_object test_json_validator unittest
33+
build: object json_object json_validator
3434

3535
# Dependency libraries
3636
# LIBS_DIRS = -L$(RM_INCLUDE_DIR) -L$(DEPS_DIR)/jsonsl -L$(DEPS_DIR)/RedisModuleSDK/rmutil
3737
# LIBS = -lrejson -lrmutil -ljsonsl -lm
3838

3939
LIBS = $(RM_INCLUDE_DIR)/librejson.a $(DEPS_DIR)/RedisModuleSDK/rmutil/librmutil.a $(DEPS_DIR)/jsonsl/libjsonsl.a -lm
4040

41-
# Building of json printer test
42-
json_printer:
43-
$(CC) $(CFLAGS) -o $@.out json_printer.c $(LIBS)
44-
45-
# Execute json_printer test
46-
test_json_printer: json_printer
47-
./$@
48-
.PHONY: test_json_printer
49-
41+
# TODO: add a test that uses json_printer on a JSON file and then validates the output
5042
# Building of json validator test
5143
json_validator:
5244
$(CC) $(CFLAGS) -o $@.out json_printer.c $(LIBS)
5345

54-
# Execute json_printer test
46+
# Execute json validator test
5547
test_json_validator: json_validator
5648
./$@.sh
5749
.PHONY: test_json_validator

test/pytest/test.py

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import json
55
import os
66

7+
# Path to JSON test case files
8+
json_path = os.path.abspath(os.path.join(os.getcwd(), '../files'))
9+
710
# TODO: these are currently not supported so ignore them
811
json_ignore = [
912
'pass-json-parser-0002.json', # UTF-8 to Unicode

0 commit comments

Comments
 (0)