# HG changeset patch # User Matti Hamalainen # Date 1509307808 -7200 # Node ID 03db1dc33af94f629d375bd03e8ec8ae12461218 # Parent 7c7f24e27a602c04a58115df5dc06d52922870d0 Handle "no matches" differently, also fix a memory leak. diff -r 7c7f24e27a60 -r 03db1dc33af9 mapsearch.c --- a/mapsearch.c Sun Oct 29 21:54:12 2017 +0200 +++ b/mapsearch.c Sun Oct 29 22:10:08 2017 +0200 @@ -779,38 +779,35 @@ out: mapBlockFree(pattern); + // If an error occured, bail out now if (*verr != NULL) return; - if (nmatches > 0) - { - // We got some matches, output them as a JSON array - char *buf = NULL; - size_t bufLen = 0, bufSize = 0; + // We got some matches, output them as a JSON array + char *buf = NULL; + size_t bufLen = 0, bufSize = 0; - th_strbuf_puts(&buf, &bufSize, &bufLen, "RESULT:["); + th_strbuf_puts(&buf, &bufSize, &bufLen, "RESULT:["); - for (int n = 0; n < nmatches; n++) - { - MAPMatch *match = &matches[n]; - char *vstr = th_strdup_printf( - "[%1.2f,%d,\"%s\",%d,%d,%d,%d]%s", - match->accuracy, - match->centered, - match->map, - match->mx, match->my, - match->wx, match->wy, - (n < nmatches - 1) ? "," : ""); + for (int n = 0; n < nmatches; n++) + { + MAPMatch *match = &matches[n]; + char *vstr = th_strdup_printf( + "[%1.2f,%d,\"%s\",%d,%d,%d,%d]%s", + match->accuracy, + match->centered, + match->map, + match->mx, match->my, + match->wx, match->wy, + (n < nmatches - 1) ? "," : ""); - th_strbuf_puts(&buf, &bufSize, &bufLen, vstr); - th_free(vstr); - } + th_strbuf_puts(&buf, &bufSize, &bufLen, vstr); + th_free(vstr); + } - th_strbuf_puts(&buf, &bufSize, &bufLen, "]"); - lws_write(wsi, (unsigned char *) buf, bufLen, LWS_WRITE_TEXT); - } - else - *verr = "No matches found."; + th_strbuf_puts(&buf, &bufSize, &bufLen, "]"); + lws_write(wsi, (unsigned char *) buf, bufLen, LWS_WRITE_TEXT); + th_free(buf); }