Skip to content

Commit 4ffce36

Browse files
committed
fix sanitizer errors
1 parent 9f5f4df commit 4ffce36

8 files changed

Lines changed: 105 additions & 45 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ dkms.conf
5555
main
5656
main-optimize
5757
main-debug
58-
lab-private
58+
private
5959
.vscode
6060
test
6161
notes.txt
@@ -65,3 +65,5 @@ json-sanitize
6565
test.c
6666
.task
6767
lab
68+
lab-sanitize
69+
hold.c

Taskfile.yml

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ vars:
1212
- hashmap.c
1313
- dynamicarray.c
1414
DYN_LIBS_USED_PATH: -L/usr/local/lib/standardloop
15-
DYN_LIBS_USED: -lstandardloop-util
15+
DYN_LIBS_USED: "-lstandardloop-util"
1616
DYLIB_NAME: libstandardloop-json.dylib
1717
DYLIB_PATH: /usr/local/lib/standardloop/
1818
DYLIB_INCLUDE_PATH: /usr/local/include/standardloop/
19-
RELEASE_VERSION: 0.0.1
19+
RELEASE_VERSION: 0.0.2
2020

2121
# Inputs
2222
# - REPO_NAME
@@ -43,7 +43,7 @@ tasks:
4343
- cat json.h | grep -q {{.RELEASE_VERSION}}
4444
- cat json.h | grep -q "MAJOR_VERSION 0"
4545
- cat json.h | grep -q "MINOR_VERSION 0"
46-
- cat json.h | grep -q "PATCH_VERSION 1"
46+
- cat json.h | grep -q "PATCH_VERSION 2"
4747

4848
dependencies:
4949
deps:
@@ -52,7 +52,7 @@ tasks:
5252
dependencies:util:
5353
vars:
5454
REPO_NAME: c-util
55-
DYLIB_VERSION: 0.0.1
55+
DYLIB_VERSION: 0.0.3
5656
DYLIB_NAME: util
5757
cmds:
5858
- sudo -v
@@ -84,7 +84,7 @@ tasks:
8484
-compatibility_version {{.RELEASE_VERSION}} \
8585
-o {{.DYLIB_NAME}}
8686
sources:
87-
- ./json.c
87+
- "*.c"
8888
generates:
8989
- "{{.DYLIB_NAME}}"
9090

@@ -93,7 +93,7 @@ tasks:
9393
- release:build
9494
cmds:
9595
- sudo -v
96-
- sudo cp {{.DYLIB_NAME}} {{.DYLIB_PATH}}
96+
- sudo mv {{.DYLIB_NAME}} {{.DYLIB_PATH}}
9797
- sudo cp json.h {{.DYLIB_INCLUDE_PATH}}
9898

9999
release:clean:
@@ -137,28 +137,57 @@ tasks:
137137
{{range .SOURCE_FILES}} {{.}} {{end}} \
138138
{{.DYN_LIBS_USED_PATH}} \
139139
{{.DYN_LIBS_USED}} \
140-
-L/usr/local/lib/standardloop \
141-
-fno-omit-frame-pointer \
140+
-O0 \
141+
-lstandardloop-json \
142142
-o {{.LAB_EXECUTABLE_NAME}}
143143
sources:
144144
- "*.c"
145145
generates:
146146
- "{{.LAB_EXECUTABLE_NAME}}"
147+
148+
lab:sanitize:
149+
deps:
150+
- lab:run-sanitize
151+
152+
lab:build-sanitize:
153+
run: once
154+
cmds:
155+
- |
156+
{{.CC}} {{.CC_FLAGS}} \
157+
lab.c \
158+
{{range .SOURCE_FILES}} {{.}} {{end}} \
159+
{{.DYN_LIBS_USED_PATH}} \
160+
{{.DYN_LIBS_USED}} \
161+
-O0 \
162+
-lstandardloop-json \
163+
-fno-omit-frame-pointer \
164+
-fsanitize=address \
165+
-o {{.LAB_EXECUTABLE_NAME}}-sanitize
166+
sources:
167+
- "*.c"
168+
generates:
169+
- "{{.LAB_EXECUTABLE_NAME}}-sanitize"
147170

148171
lab:run:
149172
deps:
150173
- lab:build
151174
cmds:
152175
- ./{{.LAB_EXECUTABLE_NAME}}
176+
177+
lab:run-sanitize:
178+
deps:
179+
- lab:build-sanitize
180+
cmds:
181+
- ./{{.LAB_EXECUTABLE_NAME}}-sanitize
153182

154183
lab:leaks:
155184
deps:
156185
- lab:build
157186
cmds:
187+
- cp /usr/local/lib/standardloop/libstandardloop-util.dylib ./ # to get around sips we need to move dylibs to local dir :(
188+
- cp /usr/local/lib/standardloop/libstandardloop-json.dylib ./
158189
- leaks --atExit -- ./{{.LAB_EXECUTABLE_NAME}}
159-
env:
160-
DYLD_FALLBACK_LIBRARY_PATH: /usr/local/lib/standardloop/
161-
DYLD_LIBRARY_PATH: /usr/local/lib/standardloop/
162-
# MallocStackLogging: YES
163-
# -lstandardloop-json \
164-
# -fsanitize=address \
190+
- rm libstandardloop-util.dylib
191+
- rm libstandardloop-json.dylib
192+
# env:
193+
# MallocStackLogging: YES

hashmap.c

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ extern void HashMapInsert(HashMap *map, JSONValue *entry)
112112
bool collision = hashMapEntriesInsert(map->entries, index, entry);
113113
if (collision)
114114
{
115+
// printf("COLLISION!\n");
116+
// fflush(stdout);
115117
map->collision_count++;
116118
}
117119
else
@@ -137,30 +139,49 @@ static bool hashMapEntriesInsert(JSONValue **entries, u_int32_t index, JSONValue
137139
entries[index] = entry;
138140
return false;
139141
}
142+
// printf("%s -> %s\n", collision->key, entry->key);
140143
// If duplicate key, update (in future could maybe make this a feature flag for the init function)
141-
if (collision->key != NULL && strcmp(collision->key, entry->key) == 0)
144+
if (collision->key != NULL)
142145
{
143-
entry->next = collision->next;
144-
collision->next = NULL;
145-
freeHashMapEntrySingle(collision, true);
146-
entries[index] = entry;
147-
return true;
146+
size_t collision_key_len = strlen(collision->key);
147+
size_t entry_key_len = strlen(entry->key);
148+
if (collision_key_len == entry_key_len)
149+
{
150+
if (strncmp(collision->key, entry->key, entry_key_len) == 0)
151+
{
152+
entry->next = collision->next;
153+
collision->next = NULL;
154+
freeHashMapEntrySingle(collision, true);
155+
entries[index] = entry;
156+
return true;
157+
}
158+
}
148159
}
149160
JSONValue *iterator_prev = collision;
150161
JSONValue *iterator = collision->next;
151162
while (iterator != NULL)
152163
{
153-
if (iterator->key != NULL && entry->key != NULL && strcmp(iterator->key, entry->key) == 0)
164+
if (iterator->key != NULL && entry->key != NULL)
154165
{
155-
iterator_prev->next = entry;
156-
entry->next = iterator->next;
157-
iterator->next = NULL;
158-
freeHashMapEntrySingle(iterator, true); // FIXME
159-
return true;
166+
size_t collision_key_len = strlen(collision->key);
167+
size_t entry_key_len = strlen(entry->key);
168+
if (collision_key_len == entry_key_len)
169+
{
170+
if (strncmp(iterator->key, entry->key, collision_key_len) == 0)
171+
{
172+
iterator_prev->next = entry;
173+
entry->next = iterator->next;
174+
iterator->next = NULL;
175+
freeHashMapEntrySingle(iterator, true);
176+
return true;
177+
}
178+
}
160179
}
180+
161181
iterator_prev = iterator;
162182
iterator = iterator->next;
163183
}
184+
entry->next = NULL;
164185
iterator_prev->next = entry;
165186
return true;
166187
}
@@ -351,7 +372,6 @@ static void printHashMapEntry(JSONValue *entry)
351372
printf("\"%s\": ", iterator->key);
352373
PrintJSONValue(iterator);
353374
}
354-
355375
iterator = iterator->next;
356376
if (iterator != NULL)
357377
{
@@ -363,6 +383,8 @@ static void printHashMapEntry(JSONValue *entry)
363383
// JOSH
364384
static void hashMapResize(HashMap *map)
365385
{
386+
// printf("hashMapResize\n");
387+
// fflush(stdout);
366388
if (map == NULL)
367389
{
368390
return;

json.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ static char *doubleToString(double num)
120120
// double fractional = modf(num, &integral);
121121
// int64_t integral_as_int64 = (int64_t)integral;
122122
// printf("%lld %lf\n", integral_as_int64, fractional);
123-
char *double_as_string = malloc(sizeof(char) * FLOAT_CHAR_MAX);
123+
char *double_as_string = malloc((sizeof(char) * FLOAT_CHAR_MAX) + sizeof(char));
124124
(void)gcvt(num, FLOAT_CHAR_MAX, double_as_string);
125-
125+
double_as_string[FLOAT_CHAR_MAX] = NULL_CHAR;
126126
return double_as_string;
127127
}
128128

json.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#define STANDARDLOOP_JSON_H_MAJOR_VERSION 0
55
#define STANDARDLOOP_JSON_H_MINOR_VERSION 0
6-
#define STANDARDLOOP_JSON_H_PATCH_VERSION 1
7-
#define STANDARDLOOP_JSON_H_VERSION "0.0.1"
6+
#define STANDARDLOOP_JSON_H_PATCH_VERSION 2
7+
#define STANDARDLOOP_JSON_H_VERSION "0.0.2"
88

99
#include <stdio.h>
1010
#include <stdlib.h>

lab.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,30 @@
22
#include <stdlib.h>
33
#include <string.h>
44

5+
// #include <standardloop/json.h>
56
#include "./json.h"
67

78
int main(void)
89
{
910
// JSON *json = StringToJSON("[{\"guid\": \"\", \"about\": \"\"}]");
10-
JSON *json = StringToJSON("{\"guid\": \"\", \"about\": \"\"}");
11+
// JSON *json = StringToJSON("{\"foo\": \"\"}");
12+
// JSON *json = StringToJSON("{\"guid\": \"foo\", \"about\": \"bar\"}");
13+
14+
JSON *json = JSONFromFile("./private/lab/1mil.json");
15+
1116
if (json == NULL)
1217
{
1318
return EXIT_FAILURE;
1419
}
1520
// PrintJSON(json);
16-
FreeJSON(json);
21+
// FreeJSON(json);
1722

18-
// char *json_string = JSONToString(json);
19-
// if (json_string == NULL)
20-
// {
21-
// return EXIT_FAILURE;
22-
// }
23-
// // printf("%s", json_string);
24-
// free(json_string);
23+
char *json_string = JSONToString(json);
24+
if (json_string == NULL)
25+
{
26+
return EXIT_FAILURE;
27+
}
28+
// printf("%s", json_string);
29+
free(json_string);
2530
return EXIT_SUCCESS;
2631
}

lexer.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,13 +394,13 @@ static char *makeNumberLiteral(Lexer *lexer)
394394

395395
static char *makeStringLiteral(Lexer *lexer)
396396
{
397-
u_int32_t start_position = lexer->position + 1;
397+
u_int32_t start_position = lexer->position + 1; // move pass quotes
398398
char prev_char = lexer->current_char;
399399
advanceChar(lexer);
400400
bool is_error = false;
401401
while (ALWAYS)
402402
{
403-
// printf("%d\n", lexer->current_char);
403+
// printf("%c\n", lexer->current_char);
404404
if (lexer->current_char == BACKSLASH_CHAR)
405405
{
406406
prev_char = lexer->current_char;
@@ -473,7 +473,8 @@ static char *makeStringLiteral(Lexer *lexer)
473473
}
474474
copyString(lexer->input, string_literal, string_literal_size, start_position);
475475
string_literal[string_literal_size - 1] = NULL_CHAR;
476-
// printf("[JOSH]\n", string_literal);
476+
// printf("[JOSH]: %s\n", string_literal);
477+
// printf("[JOSH]: %d\n", (int)strlen(string_literal));
477478
return string_literal;
478479
}
479480

parser.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,8 @@ static JSONValue *parse(Parser *parser)
500500
{
501501
return NULL;
502502
}
503-
// printf("[JOSH]: %u\n", (unsigned int)parser->list_nested);
503+
// printf("[JOSH]: %d\n", parser->peek_token->type);
504+
// printf("[JOSH]: %s\n", parser->peek_token->literal);
504505

505506
nextToken(parser);
506507
// PrintToken(parser->current_token, false);

0 commit comments

Comments
 (0)