Skip to content

Commit 606bc87

Browse files
Minor improvements
Protect binfindmatch against too-long territory strings, or lowercase strings;
1 parent ade5fc3 commit 606bc87

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

mapcodelib/mapcoder.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
#include <string.h> // strlen strcpy strcat memcpy memmove strstr strchr memcmp
17+
#include <string.h> // strlen strcpy strcat memcpy memmove strstr strchr memcmp strupr
1818
#include <stdlib.h> // atof
1919
#include <ctype.h> // toupper
20+
#include <math.h> // floor
2021
#include "mapcoder.h"
2122
#include "basics.h"
2223

@@ -170,12 +171,7 @@ static int disambiguate_str(const char *s, int len) // returns disambiguation 1-
170171
if (len != 2 && len != 3) { return -923; } // solve bad args
171172
memcpy(country, s, len);
172173
country[len] = 0;
173-
{
174-
char *t;
175-
for (t = country; *t != 0; t++) {
176-
*t = (char) toupper(*t);
177-
}
178-
}
174+
strupr(country);
179175
f = strstr(p, country);
180176
if (f == NULL) {
181177
return -23; // unknown country
@@ -1357,6 +1353,10 @@ static int decoderEngine(decodeRec *dec) {
13571353
if (ccode == ccode_mex && len < 8) {
13581354
ccode = ccode_of_iso3("5MX", -1);
13591355
} // special case for mexico country vs state
1356+
if (*s=='u' || *s=='U') {
1357+
strcpy(s,s+1);
1358+
repack_if_alldigits(s, 1);
1359+
}
13601360
dec->context = ccode;
13611361
dec->mapcode = s;
13621362
dec->extension = "";
@@ -1905,23 +1905,25 @@ int cmp_alphacode(const void *e1, const void *e2) {
19051905
} // cmp
19061906

19071907
int binfindmatch(int parentcode, const char *str) {
1908+
// build a 4-letter uppercase search term
19081909
char tmp[5];
19091910
if (parentcode < 0) { return -1; }
19101911
if (parentcode > 0) {
19111912
tmp[0] = '0' + parentcode;
19121913
memcpy(tmp + 1, str, 3);
1913-
tmp[4] = 0;
1914-
str = tmp;
1915-
} //
1914+
} else {
1915+
memcpy(tmp, str, 4);
1916+
}
1917+
tmp[4] = 0;
19161918
{ // binary-search the result
19171919
const alphaRec *p;
19181920
alphaRec t;
1919-
t.alphaCode = str;
1921+
t.alphaCode = strupr(tmp);
19201922
t.ccode = parentcode;
19211923

19221924
p = (const alphaRec *) bsearch(&t, alphaSearch, NRTERREC, sizeof(alphaRec), cmp_alphacode);
19231925
if (p) {
1924-
if (strcmp(str, p->alphaCode) == 0) {
1926+
if (strcmp(t.alphaCode, p->alphaCode) == 0) { // only interested in PERFECT match
19251927
return p->ccode + 1;
19261928
} // match
19271929
} // found

0 commit comments

Comments
 (0)