summaryrefslogtreecommitdiffstats
path: root/main/libmaxminddb/0002-make-MMDB_aget_value-s-path-argument-const-correct.patch
blob: 958067185fca8712f3e329580e1f68d43fc04164 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
From ea773070121c9e0ef55680af4d9b45a8be6bfe1b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timo=20Ter=C3=A4s?= <timo.teras@iki.fi>
Date: Thu, 14 Nov 2013 15:28:04 +0200
Subject: [PATCH 2/2] make MMDB_aget_value()'s path argument const correct

---
 include/maxminddb.h |  2 +-
 src/maxminddb.c     | 49 ++++++++++++++++++-------------------------------
 2 files changed, 19 insertions(+), 32 deletions(-)

diff --git a/include/maxminddb.h b/include/maxminddb.h
index 599d4d5..3c6e645 100644
--- a/include/maxminddb.h
+++ b/include/maxminddb.h
@@ -178,7 +178,7 @@ typedef struct MMDB_search_node_s {
     extern int MMDB_vget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data,
                                va_list va_path);
     extern int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data,
-                               char **path);
+                               const char *const * path);
     extern int MMDB_get_metadata_as_entry_data_list(
                MMDB_s *mmdb, MMDB_entry_data_list_s **entry_data_list);
     extern int MMDB_get_entry_data_list(MMDB_entry_s *start,
diff --git a/src/maxminddb.c b/src/maxminddb.c
index 3bf7154..fc82c9a 100644
--- a/src/maxminddb.c
+++ b/src/maxminddb.c
@@ -132,9 +132,9 @@ LOCAL int populate_result(MMDB_s *mmdb, uint32_t node_count, uint32_t value,
                           uint16_t netmask, MMDB_lookup_result_s *result);
 LOCAL uint32_t get_left_28_bit_record(const uint8_t *record);
 LOCAL uint32_t get_right_28_bit_record(const uint8_t *record);
-LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb,
+LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb,
                                MMDB_entry_data_s *entry_data);
-LOCAL int lookup_path_in_map(char *path_elem, MMDB_s *mmdb,
+LOCAL int lookup_path_in_map(const char *path_elem, MMDB_s *mmdb,
                              MMDB_entry_data_s *entry_data);
 LOCAL int skip_map_or_array(MMDB_s *mmdb, MMDB_entry_data_s *entry_data);
 LOCAL int decode_one_follow(MMDB_s *mmdb, uint32_t offset,
@@ -402,7 +402,7 @@ LOCAL MMDB_s make_fake_metadata_db(MMDB_s *mmdb)
 LOCAL uint32_t value_for_key_as_uint16(MMDB_entry_s *start, char *key)
 {
     MMDB_entry_data_s entry_data;
-    char *path[] = { key, NULL };
+    const char *path[] = { key, NULL };
     MMDB_aget_value(start, &entry_data, path);
     return entry_data.uint16;
 }
@@ -410,7 +410,7 @@ LOCAL uint32_t value_for_key_as_uint16(MMDB_entry_s *start, char *key)
 LOCAL uint32_t value_for_key_as_uint32(MMDB_entry_s *start, char *key)
 {
     MMDB_entry_data_s entry_data;
-    char *path[] = { key, NULL };
+    const char *path[] = { key, NULL };
     MMDB_aget_value(start, &entry_data, path);
     return entry_data.uint32;
 }
@@ -418,7 +418,7 @@ LOCAL uint32_t value_for_key_as_uint32(MMDB_entry_s *start, char *key)
 LOCAL uint64_t value_for_key_as_uint64(MMDB_entry_s *start, char *key)
 {
     MMDB_entry_data_s entry_data;
-    char *path[] = { key, NULL };
+    const char *path[] = { key, NULL };
     MMDB_aget_value(start, &entry_data, path);
     return entry_data.uint64;
 }
@@ -426,7 +426,7 @@ LOCAL uint64_t value_for_key_as_uint64(MMDB_entry_s *start, char *key)
 LOCAL char *value_for_key_as_string(MMDB_entry_s *start, char *key)
 {
     MMDB_entry_data_s entry_data;
-    char *path[] = { key, NULL };
+    const char *path[] = { key, NULL };
     MMDB_aget_value(start, &entry_data, path);
     return strndup((char *)entry_data.utf8_string, entry_data.data_size);
 }
@@ -436,7 +436,7 @@ LOCAL int populate_languages_metadata(MMDB_s *mmdb, MMDB_s *metadata_db,
 {
     MMDB_entry_data_s entry_data;
 
-    char *path[] = { "languages", NULL };
+    const char *path[] = { "languages", NULL };
     MMDB_aget_value(metadata_start, &entry_data, path);
 
     if (MMDB_DATA_TYPE_ARRAY != entry_data.type) {
@@ -487,7 +487,7 @@ LOCAL int populate_description_metadata(MMDB_s *mmdb, MMDB_s *metadata_db,
 {
     MMDB_entry_data_s entry_data;
 
-    char *path[] = { "description", NULL };
+    const char *path[] = { "description", NULL };
     MMDB_aget_value(metadata_start, &entry_data, path);
 
     if (MMDB_DATA_TYPE_MAP != entry_data.type) {
@@ -831,43 +831,30 @@ int MMDB_get_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data, ...)
 int MMDB_vget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data,
                     va_list va_path)
 {
-    char **path = NULL;
-
+    const char **path = NULL;
+    const char *path_elem;
     int i = 0;
-    char *path_elem;
-    while (NULL != (path_elem = va_arg(va_path, char *))) {
-        path = realloc(path, sizeof(char *) * (i + 1));
+
+    while (NULL != (path_elem = va_arg(va_path, const char *))) {
+        path = realloc(path, sizeof(const char *) * (i + 2));
         if (NULL == path) {
             return MMDB_OUT_OF_MEMORY_ERROR;
         }
 
-        path[i] = strdup(path_elem);
-        if (NULL == path[i]) {
-            return MMDB_OUT_OF_MEMORY_ERROR;
-        }
+        path[i] = path_elem;
         i++;
     }
-
-    path = realloc(path, sizeof(char *) * (i + 1));
-    if (NULL == path) {
-        return MMDB_OUT_OF_MEMORY_ERROR;
-    }
     path[i] = NULL;
 
     int status = MMDB_aget_value(start, entry_data, path);
 
-    i = 0;
-    char *elem;
-    while (NULL != (elem = path[i++])) {
-        free(elem);
-    }
     free(path);
 
     return status;
 }
 
 int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data,
-                    char **path)
+                    const char *const * path)
 {
     MMDB_s *mmdb = start->mmdb;
     uint32_t offset = start->offset;
@@ -888,7 +875,7 @@ int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data,
         return MMDB_INVALID_LOOKUP_PATH_ERROR;
     }
 
-    char *path_elem;
+    const char *path_elem;
     while (NULL != (path_elem = *(path++))) {
         DEBUG_NL;
         DEBUG_MSGF("path elem = %s", path_elem);
@@ -921,7 +908,7 @@ int MMDB_aget_value(MMDB_entry_s *start, MMDB_entry_data_s *entry_data,
     return MMDB_SUCCESS;
 }
 
-LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb,
+LOCAL int lookup_path_in_array(const char *path_elem, MMDB_s *mmdb,
                                MMDB_entry_data_s *entry_data)
 {
     uint32_t size = entry_data->data_size;
@@ -952,7 +939,7 @@ LOCAL int lookup_path_in_array(char *path_elem, MMDB_s *mmdb,
     return MMDB_SUCCESS;
 }
 
-LOCAL int lookup_path_in_map(char *path_elem, MMDB_s *mmdb,
+LOCAL int lookup_path_in_map(const char *path_elem, MMDB_s *mmdb,
                              MMDB_entry_data_s *entry_data)
 {
     uint32_t size = entry_data->data_size;
-- 
1.8.4.1