Skip to content

Commit 4a3620d

Browse files
committed
Fixed 5.5 bug
1 parent 0333992 commit 4a3620d

File tree

4 files changed

+43
-27
lines changed

4 files changed

+43
-27
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ unittest
1515
mapcode
1616
test/unittest
1717
utility/mapcode
18+
cmake-build-debug/
19+
*.cbp
1820

1921
# -----------------------------------------------------------------------------
2022
# Compiled sources

mapcodelib/mapcoder.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ static void encodeGrid(char *result, const EncodeRec *enc, const int m, const in
909909
const int postlen = codexm % 10;
910910

911911
divy = SMART_DIV(m);
912+
ASSERT(divy > 0);
912913
if (divy == 1) {
913914
divx = X_SIDE[prelen];
914915
divy = Y_SIDE[prelen];
@@ -1060,6 +1061,7 @@ static void encodeNameless(char *result, const EncodeRec *enc, const enum Territ
10601061
}
10611062

10621063
SIDE = SMART_DIV(m);
1064+
ASSERT(SIDE > 0);
10631065

10641066
b = TERRITORY_BOUNDARY(m);
10651067
orgSIDE = SIDE;
@@ -1509,6 +1511,7 @@ static enum MapcodeError decodeGrid(DecodeRec *dec, const int m, const int hasHe
15091511
int divx, divy;
15101512

15111513
divy = SMART_DIV(m);
1514+
ASSERT(divy > 0);
15121515
if (divy == 1) {
15131516
divx = X_SIDE[prelen];
15141517
divy = Y_SIDE[prelen];
@@ -1697,7 +1700,9 @@ static enum MapcodeError decodeNameless(DecodeRec *dec, int m) {
16971700

16981701
m = (F + X);
16991702

1700-
xSIDE = SIDE = SMART_DIV(m);
1703+
SIDE = SMART_DIV(m);
1704+
ASSERT(SIDE > 0);
1705+
xSIDE = SIDE;
17011706

17021707
b = TERRITORY_BOUNDARY(m);
17031708

@@ -2296,7 +2301,10 @@ static enum MapcodeError decoderEngine(DecodeRec *dec, int parseFlags) {
22962301
wasAllDigits = 1;
22972302
}
22982303

2299-
if (codex == 54) {
2304+
if (codex > 54) {
2305+
ASSERT(codex == 55);
2306+
return ERR_MAPCODE_UNDECODABLE;
2307+
} else if (codex == 54) {
23002308
// international mapcodes must be in international context
23012309
ccode = TERRITORY_AAA;
23022310
} else if (ccode < _TERRITORY_MIN) {
@@ -2312,7 +2320,8 @@ static enum MapcodeError decoderEngine(DecodeRec *dec, int parseFlags) {
23122320
from = firstRec(ccode);
23132321
upto = lastRec(ccode);
23142322

2315-
// try all ccode rectangles to decode s (pointing to first character of proper mapcode)
2323+
// try all ccode rectangles to decode s (pointing to first character of proper mapcode), assume not decodable
2324+
err = ERR_MAPCODE_UNDECODABLE;
23162325
for (i = from; i <= upto; i++) {
23172326
const int codexi = coDex(i);
23182327
const int r = REC_TYPE(i);

test/run_all.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ echo "" | tee -a $OUT
66
./run_compare.sh | tee -a $OUT
77
./run_normal.sh | tee -a $OUT
88
./run_sanitizer.sh | tee -a $OUT
9-
./run_gcov.sh | tee -a $OUT
109
./run_valgrind.sh | tee -a $OUT
10+
./run_gcov.sh | tee -a $OUT
1111
./run_gprof.sh | tee -a $OUT
1212
echo "" | tee -a $OUT
1313
echo "Done" | tee -a $OUT

test/unittest.c

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,9 @@ static int testMapcodeFormats(void) {
363363
{"DDD.L ", ERR_INVALID_MAPCODE_FORMAT, ERR_OK}, // 7.0 : postfix too short
364364
{"DDDDDD xx.xx", ERR_INVALID_MAPCODE_FORMAT, ERR_OK}, // 5/2 : 6char ter
365365
{"DDDDDD.xxx", ERR_INVALID_MAPCODE_FORMAT, ERR_OK}, // 5/2 : 6char mc
366+
{"XXXX.XXXXX", ERR_OK, ERR_MISSING_TERRITORY}, // 4/5
367+
{"XXXXX.XXXXX", ERR_OK, ERR_MAPCODE_UNDECODABLE}, // 5/5
368+
366369
// errors because there are too many letters after a postfix vowel
367370
{"XXXX.AXXX", ERR_INVALID_VOWEL, ERR_OK},
368371
{"nld XXXX.AXX", ERR_INVALID_VOWEL, ERR_OK},
@@ -374,10 +377,13 @@ static int testMapcodeFormats(void) {
374377
// 5th letter
375378
{"nld DD.DDDDD ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
376379
{"nld XXXX.XXXXX", ERR_OK, ERR_MAPCODE_UNDECODABLE},
377-
{" TAM XX.XXXXX-XX ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
378-
{" TAM XXX.XXXXX-XX ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
379-
{" TAM XXXX.XXXXX-X ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
380-
{" TAM XXXXX.XXXXX-X ", ERR_OK, ERR_MAPCODE_UNDECODABLE},
380+
{"TAM XX.XXXXX-XX", ERR_OK, ERR_MAPCODE_UNDECODABLE},
381+
{"TAM XXX.XXXXX-XX", ERR_OK, ERR_MAPCODE_UNDECODABLE},
382+
{"TAM XXXX.XXXXX-X", ERR_OK, ERR_MAPCODE_UNDECODABLE},
383+
{"TAM XXXXX.XXXXX-X", ERR_OK, ERR_MAPCODE_UNDECODABLE},
384+
{"40822.schol", ERR_OK, ERR_MAPCODE_UNDECODABLE},
385+
{"AAA 40822.schol", ERR_OK, ERR_MAPCODE_UNDECODABLE},
386+
381387
// errors because the postfix has a 6th letter
382388
{"DD.DDDDDD ", ERR_INVALID_MAPCODE_FORMAT, ERR_OK},
383389
{"nld XXXX.XXXXXX", ERR_INVALID_MAPCODE_FORMAT, ERR_OK},
@@ -477,11 +483,11 @@ static int testAlphabetParser(void) {
477483
enum Alphabet alphabet;
478484
const char *expected;
479485
} convertTests[] = {
480-
{"nld bc.XY-p2q", ALPHABET_ROMAN, "nld BC.XY-P2Q"},
481-
{"DNK PQ.XX", ALPHABET_DEVANAGARI, "DNK नप.सस"},
482-
{"GBR XX.XX", ALPHABET_HEBREW, "GBR רר.56ר"},
483-
{"BEL PQ.XP", ALPHABET_ARABIC, "BEL طظ.56ط"},
484-
{"nld 00.E0", ALPHABET_GREEK, "nld \xCE\x91\x30.12"}
486+
{"nld bc.XY-p2q", ALPHABET_ROMAN, "nld BC.XY-P2Q"},
487+
{"DNK PQ.XX", ALPHABET_DEVANAGARI, "DNK नप.सस"},
488+
{"GBR XX.XX", ALPHABET_HEBREW, "GBR רר.56ר"},
489+
{"BEL PQ.XP", ALPHABET_ARABIC, "BEL طظ.56ط"},
490+
{"nld 00.E0", ALPHABET_GREEK, "nld \xCE\x91\x30.12"}
485491
};
486492
int i;
487493
for (i = 0; i < (int) (sizeof(convertTests) / sizeof(convertTests[0])); i++) {
@@ -921,8 +927,8 @@ static int testTerritories() {
921927
++nrTests;
922928
if (strcmp(getTerritoryIsoName(nam, TERRITORY_US_CA, 1), "CA") ||
923929
strcmp(getTerritoryIsoName(nam, TERRITORY_IN_DD, 1), "DD") ||
924-
strcmp(getTerritoryIsoName(nam, TERRITORY_NLD, 1), "NLD") ||
925-
strcmp(getTerritoryIsoName(nam, TERRITORY_USA, 1), "USA")) {
930+
strcmp(getTerritoryIsoName(nam, TERRITORY_NLD, 1), "NLD") ||
931+
strcmp(getTerritoryIsoName(nam, TERRITORY_USA, 1), "USA")) {
926932
foundError();
927933
printf("*** ERROR *** getTerritoryIsoName returned bad short versions\n");
928934
}
@@ -1267,16 +1273,16 @@ static int testTerritoryCode(void) {
12671273
{TERRITORY_NONE, TERRITORY_NONE},
12681274
{_TERRITORY_MIN, TERRITORY_NONE},
12691275
{TERRITORY_VAT, TERRITORY_NONE},
1270-
{TERRITORY_MX_DIF, TERRITORY_MEX },
1271-
{TERRITORY_MX_CHH, TERRITORY_MEX },
1276+
{TERRITORY_MX_DIF, TERRITORY_MEX},
1277+
{TERRITORY_MX_CHH, TERRITORY_MEX},
12721278
{TERRITORY_GRL, TERRITORY_NONE},
1273-
{TERRITORY_IN_DD, TERRITORY_IND },
1274-
{TERRITORY_AU_VIC, TERRITORY_AUS },
1275-
{TERRITORY_BR_DF, TERRITORY_BRA },
1276-
{TERRITORY_US_AL, TERRITORY_USA },
1277-
{TERRITORY_CA_NU, TERRITORY_CAN },
1278-
{TERRITORY_RU_LIP, TERRITORY_RUS },
1279-
{TERRITORY_CN_HA , TERRITORY_CHN },
1279+
{TERRITORY_IN_DD, TERRITORY_IND},
1280+
{TERRITORY_AU_VIC, TERRITORY_AUS},
1281+
{TERRITORY_BR_DF, TERRITORY_BRA},
1282+
{TERRITORY_US_AL, TERRITORY_USA},
1283+
{TERRITORY_CA_NU, TERRITORY_CAN},
1284+
{TERRITORY_RU_LIP, TERRITORY_RUS},
1285+
{TERRITORY_CN_HA, TERRITORY_CHN},
12801286
{TERRITORY_AAA, TERRITORY_NONE},
12811287
{_TERRITORY_MAX, TERRITORY_NONE},
12821288
{TERRITORY_UNKNOWN, TERRITORY_NONE}
@@ -1523,7 +1529,6 @@ static int testDecodeRobustness(void) {
15231529
nrTests += testCorrectDecode("MEX 49.4V", tc);
15241530
nrTests += testCorrectDecode("NLD XX.XX", TERRITORY_NONE);
15251531
nrTests += testCorrectDecode("MX XX.XX", TERRITORY_NONE);
1526-
nrTests += testCorrectDecode("AAA 40822.schol", TERRITORY_NONE);
15271532

15281533
s1[0] = 0;
15291534
nrTests += testIncorrectDecode(s1, TERRITORY_NONE);
@@ -1788,7 +1793,7 @@ int testGetFullTerritoryName(void) {
17881793
const TerritoryAlphabets *territoryAlphabets;
17891794
char territoryName[MAX_TERRITORY_FULLNAME_UTF8_LEN + 1024]; // large so we can test overflow
17901795
static const char *locales_to_test[] = {
1791-
"AF", "AR", "BE", "CN", "CS", "DA", "DE", "EN", "FI", "ES", "FR", "HE", "HI",
1796+
"AF", "AR", "BE", "CN", "CS", "DA", "DE", "EN", "FI", "ES", "FR", "HE", "HI",
17921797
"HR", "ID", "IT", "JA", "KO", "NL", "NO", "PL", "PT", "RU", "SV", "SW", "TR", "UK"};
17931798

17941799
nrTests += testGetFullTerritoryNameInLocale("Sancta Sedes", TERRITORY_VAT, 0, NULL); // Local name.
@@ -1839,7 +1844,7 @@ int testGetFullTerritoryName(void) {
18391844
for (i = 0; i < territoryAlphabets->count; i++) {
18401845
nrTests++;
18411846
if (!getFullTerritoryNameLocalInAlphabetUtf8(territoryName, territory, 0,
1842-
territoryAlphabets->alphabet[i])) {
1847+
territoryAlphabets->alphabet[i])) {
18431848
char s[MAX_ISOCODE_ASCII_LEN + 1];
18441849
foundError();
18451850
printf("*** ERROR *** getFullTerritoryNameLocal territory %s has NO name in common alphabet (%d)\n",

0 commit comments

Comments
 (0)