Skip to content

Commit 7d65b6e

Browse files
GEOHASH_clone
1 parent c200b98 commit 7d65b6e

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Sources/FlitsGeohashC/flitsgeohash.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ void add_hash(GeohashArray* array, char* hash) {
170170
if (!new_hashes) {
171171
// Allocation failed
172172
GEOHASH_free_array(array);
173+
free(hash);
173174
exit(EXIT_FAILURE);
174175
}
175176
array->hashes = new_hashes;
@@ -182,6 +183,20 @@ int geohash_equals(const char* a, const char* b) {
182183
return strcmp(a, b) == 0;
183184
}
184185

186+
char* GEOHASH_clone(const char* hash) {
187+
if (!hash) {
188+
exit(EXIT_FAILURE);
189+
return NULL;
190+
}
191+
192+
char* clone = strdup(hash);
193+
if (!clone) {
194+
exit(EXIT_FAILURE);
195+
return NULL;
196+
}
197+
return clone;
198+
}
199+
185200
GeohashArray
186201
GEOHASH_hashes_for_region(double centerLat, double centerLon, double latDelta, double lonDelta, unsigned int len) {
187202

@@ -194,9 +209,9 @@ GEOHASH_hashes_for_region(double centerLat, double centerLon, double latDelta, d
194209
char* ne = GEOHASH_encode(north, east, len);
195210
char* se = GEOHASH_encode(south, east, len);
196211

197-
char* current = strdup(nw);
198-
char* eastLimit = strdup(ne);
199-
char* westStart = strdup(nw);
212+
char* current = GEOHASH_clone(nw);
213+
char* eastLimit = GEOHASH_clone(ne);
214+
char* westStart = GEOHASH_clone(nw);
200215

201216
GeohashArray result = {
202217
.hashes = malloc(128 * sizeof(char*)),
@@ -208,26 +223,26 @@ GEOHASH_hashes_for_region(double centerLat, double centerLon, double latDelta, d
208223
exit(EXIT_FAILURE);
209224
}
210225

211-
add_hash(&result, strdup(current));
226+
add_hash(&result, GEOHASH_clone(current));
227+
212228
int maxIterations = MAX_ITERATIONS; // Prevent infinite loops
213-
214229
while (!geohash_equals(current, se) && maxIterations-- > 0) {
215230
if (geohash_equals(nw, ne)) {
216231
// Single column case
217232
char* nextSouth = GEOHASH_get_adjacent(westStart, GEOHASH_SOUTH);
218233
free(westStart);
219234
westStart = nextSouth;
220235
free(current);
221-
current = strdup(westStart);
222-
add_hash(&result, strdup(current));
236+
current = GEOHASH_clone(westStart);
237+
add_hash(&result, GEOHASH_clone(current));
223238
continue;
224239
}
225240

226241
// Move east
227242
char* next = GEOHASH_get_adjacent(current, GEOHASH_EAST);
228243
free(current);
229244
current = next;
230-
add_hash(&result, strdup(current));
245+
add_hash(&result, GEOHASH_clone(current));
231246

232247
if (geohash_equals(current, eastLimit) && !geohash_equals(current, se)) {
233248
char* tmp;
@@ -241,8 +256,8 @@ GEOHASH_hashes_for_region(double centerLat, double centerLon, double latDelta, d
241256
westStart = tmp;
242257

243258
free(current);
244-
current = strdup(westStart);
245-
add_hash(&result, strdup(current));
259+
current = GEOHASH_clone(westStart);
260+
add_hash(&result, GEOHASH_clone(current));
246261
}
247262
}
248263

0 commit comments

Comments
 (0)