Skip to content

Commit 472f230

Browse files
committed
Fixed error in unit test which caused stack corruption
1 parent 1b1d0fd commit 472f230

File tree

4 files changed

+64
-59
lines changed

4 files changed

+64
-59
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ atlassian-ide-plugin.xml
9595
*.dsw
9696
*.ncb
9797
*.plg
98+
*.suo
99+
*.sln
100+
*.vcxproj*
101+
Debug
98102
*.opt
99103
*.pch
100104
*.pdb

mapcodelib/mapcoder.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ static int unpack_if_alldigits(char *input) {
447447
const int aonly = ((*s == 'A') || (*s == 'a'));
448448
if (aonly) {
449449
s++;
450-
} //*** v1.50
450+
} // v1.50
451451
for (; *s != 0 && s[2] != 0 && s[2] != '-'; s++) {
452452
if (*s == '-') {
453453
break;
@@ -934,7 +934,7 @@ static void encoderEngine(const int ccode, const encodeRec *enc, const int stop_
934934
// *** do a recursive call for the parent ***
935935
encoderEngine(ParentTerritoryOf(ccode), enc, stop_with_one_result, extraDigits, requiredEncoder,
936936
ccode);
937-
return; /**/
937+
return;
938938
} else // must be grid
939939
{
940940
// skip isRestricted records unless there already is a result

unittest/test_territories.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
extern int test_territory(const char *alphacode, int tc, int isAlias, int needsParent, int tcParent );
44

5-
int test_territories() {
5+
static int test_territories(void) {
66
int nrTests = 0;
77
nrTests += test_territory("AAA", 533, 0, 0, 0);
88
nrTests += test_territory("AB", 396, 0, 0, 496);

unittest/unittest.c

Lines changed: 57 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,46 @@
2323
#include <math.h>
2424
#include <time.h>
2525

26-
#define USE_PTHREADS // If your platform does not support pthread.h please change this line to #undef!
27-
// #undef USE_PTHREADS
28-
2926
#include "../mapcodelib/mapcoder.c"
3027
#include "../mapcodelib/mapcode_countrynames_short.h"
3128
#include "test_territories.c"
3229
#include "decode_test.h"
3330

34-
#ifdef USE_PTHREADS
35-
#include <pthread.h>
36-
#else
37-
#define pthread_mutex_lock(ignore) // Fake implementation of pthread.
31+
// If your platform does not support pthread.h, either add -DNO_POSIX_THREADS
32+
// to your compiler command-line, or uncomment the following line:
33+
// #define NO_POSIX_THREADS
34+
35+
#ifdef NO_POSIX_THREADS
36+
37+
// Fake implementation of pthread to not use threads at all:
38+
#define pthread_mutex_lock(ignore)
3839
#define pthread_mutex_unlock(ignore)
3940
#define pthread_mutex_t int
4041
#define PTHREAD_MUTEX_INITIALIZER 0
4142
#define pthread_t int
4243
#define pthread_join(ignore1, ignore2) 0
4344
#define pthread_create(ignore1, ignore2, func, context) func(context)
45+
#define MAX_THREADS 1
46+
#else
47+
#include <pthread.h>
48+
#define MAX_THREADS 16 // Optimal: not too much, approx. nr of cores * 2, better no more than 32.
4449
#endif
4550

4651
#define MAXLINESIZE 1024
47-
#ifdef USE_PTHREADS
48-
#define MAX_THREADS 16 // Optimal: not too much, approx. nr of cores * 2, better no more than 32.
49-
#else
50-
#define MAX_THREADS 1
51-
#endif
5252

53-
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
54-
int nrErrors = 0;
53+
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
54+
static int nrErrors = 0;
55+
56+
#define LARGE_NUMBER 16000
5557

56-
void found_error() {
58+
static void found_error(void) {
5759
pthread_mutex_lock(&mutex);
5860
++nrErrors;
5961
pthread_mutex_unlock(&mutex);
6062
}
6163

6264
// test the alphabet conversion routines
63-
int alphabet_tests() {
65+
static int alphabet_tests(void) {
6466
int nrTests = 0;
6567
int i, j;
6668
const char *str, *expect;
@@ -153,7 +155,7 @@ int alphabet_tests() {
153155
}
154156

155157
// Show progress.
156-
void show_progress(int at, int max, int nrTests) {
158+
static void show_progress(int at, int max, int nrTests) {
157159
static clock_t prevTick = 0;
158160

159161
// No worries, clock() is a very fast call.
@@ -167,7 +169,7 @@ void show_progress(int at, int max, int nrTests) {
167169
}
168170

169171
//
170-
void printGeneratedMapcodes(const char *title, const Mapcodes *mapcodes) {
172+
static void printGeneratedMapcodes(const char *title, const Mapcodes *mapcodes) {
171173
int i, nrresults = mapcodes->count;
172174
printf(" %s: %d results", title, nrresults);
173175
for (i = 0; i < nrresults; i++) {
@@ -178,7 +180,7 @@ void printGeneratedMapcodes(const char *title, const Mapcodes *mapcodes) {
178180
}
179181

180182
// test encode x,y to M, decode M, re-encode back to M
181-
int testEncodeAndDecode(const char *str, double y, double x, int localsolutions, int globalsolutions) {
183+
static int testEncodeAndDecode(const char *str, double y, double x, int localsolutions, int globalsolutions) {
182184
int nrTests = 0;
183185
char clean[MAX_MAPCODE_RESULT_LEN];
184186
const char *p, *s;
@@ -368,7 +370,7 @@ int testEncodeAndDecode(const char *str, double y, double x, int localsolutions,
368370
}
369371

370372
// test strings that are expected to FAIL a decode
371-
int test_failing_decodes() {
373+
static int test_failing_decodes(void) {
372374
int nrTests = 0;
373375
static const char *badcodes[] = {
374376

@@ -492,7 +494,7 @@ int test_territory(const char *alphaCode, int tc, int isAlias, int needsParent,
492494

493495

494496
// test closely around a particular coordinate
495-
int test_around(double y, double x) {
497+
static int test_around(double y, double x) {
496498
int nrTests = 0;
497499
nrTests += testEncodeAndDecode("", y + 0.00001, x + 0.00001, 0, 0);
498500
nrTests += testEncodeAndDecode("", y + 0.00001, x, 0, 0);
@@ -516,7 +518,7 @@ struct context_test_around {
516518
};
517519

518520

519-
int join_threads(pthread_t *threads, struct context_test_around *contexts, int total) {
521+
static int join_threads(pthread_t *threads, struct context_test_around *contexts, int total) {
520522
int i = 0;
521523
int nrTests = 0;
522524
for (i = 0; i < total; ++i) {
@@ -532,7 +534,7 @@ int join_threads(pthread_t *threads, struct context_test_around *contexts, int t
532534
}
533535

534536
// perform testEncodeAndDecode for all elements of encode_test[] (from decode_test.h)
535-
int encode_decode_tests() {
537+
static int encode_decode_tests(void) {
536538
int nrTests = 0;
537539
int i = 0;
538540
int nr = sizeof(encode_test) / sizeof(encode_test_record) - 1;
@@ -546,7 +548,7 @@ int encode_decode_tests() {
546548
return nrTests;
547549
}
548550

549-
void *execute_test_around(void *context) {
551+
static void *execute_test_around(void *context) {
550552
int nrTests = 0;
551553
double y, x, midx, midy, thirdx;
552554
struct context_test_around *c = (struct context_test_around *) context;
@@ -582,7 +584,7 @@ void *execute_test_around(void *context) {
582584

583585

584586
// test around all centers and corners of all territory rectangles
585-
int re_encode_tests() {
587+
static int re_encode_tests(void) {
586588
int nrTests = 0;
587589
int ccode = 0;
588590
int m = 0;
@@ -623,7 +625,7 @@ int re_encode_tests() {
623625
return nrTests;
624626
}
625627

626-
int distance_tests() {
628+
static int distance_tests(void) {
627629
int nrTests = 0;
628630
if (strcmp(mapcode_cversion, "2.1.3") >= 0) {
629631
int i;
@@ -662,7 +664,7 @@ int distance_tests() {
662664
}
663665

664666

665-
int test_territory_insides() {
667+
static int test_territory_insides(void) {
666668
int nrTests = 0;
667669
if (strcmp(mapcode_cversion, "2.1.5") >= 0) {
668670
int i;
@@ -724,7 +726,7 @@ int test_territory_insides() {
724726
return nrTests;
725727
}
726728

727-
int territory_code_tests() {
729+
static int territory_code_tests(void) {
728730
int nrTests = 0;
729731
int i;
730732

@@ -772,7 +774,7 @@ int territory_code_tests() {
772774
}
773775

774776

775-
int check_incorrect_get_territory_code_test(char *tcAlpha) {
777+
static int check_incorrect_get_territory_code_test(char *tcAlpha) {
776778
int tc = getTerritoryCode(tcAlpha, 0);
777779
if (tc >= 0) {
778780
found_error();
@@ -782,11 +784,11 @@ int check_incorrect_get_territory_code_test(char *tcAlpha) {
782784
}
783785

784786

785-
int get_territory_robustness_tests() {
787+
static int get_territory_robustness_tests(void) {
786788
int nrTests = 0;
787789
int i;
788790
char s1[1];
789-
char s10k[10000];
791+
char largeString[LARGE_NUMBER];
790792

791793
nrTests += check_incorrect_get_territory_code_test("UNKNOWN");
792794
nrTests += check_incorrect_get_territory_code_test("A");
@@ -806,15 +808,15 @@ int get_territory_robustness_tests() {
806808
s1[0] = 0;
807809
nrTests += check_incorrect_get_territory_code_test(s1);
808810

809-
for (i = 0; i < sizeof(s10k); ++i) {
810-
s10k[i] = (char) ((i % 223) + 32);
811+
for (i = 0; i < sizeof(largeString); ++i) {
812+
largeString[i] = (char) ((i % 223) + 32);
811813
}
812-
nrTests += check_incorrect_get_territory_code_test(s10k);
814+
nrTests += check_incorrect_get_territory_code_test(largeString);
813815
return nrTests;
814816
}
815817

816818

817-
int check_incorrect_encode_test(double lat, double lon, int treatAsError) {
819+
static int check_incorrect_encode_test(double lat, double lon, int treatAsError) {
818820
int nrResults;
819821
int nrTests = 0;
820822
Mapcodes mapcodes;
@@ -831,7 +833,7 @@ int check_incorrect_encode_test(double lat, double lon, int treatAsError) {
831833
}
832834

833835

834-
int check_correct_encode_test(double lat, double lon, int treatAsError) {
836+
static int check_correct_encode_test(double lat, double lon, int treatAsError) {
835837
Mapcodes mapcodes;
836838
int nrResults = encodeLatLonToMapcodes(&mapcodes, lat, lon, 0, 0);
837839
if (nrResults <= 0) {
@@ -845,7 +847,7 @@ int check_correct_encode_test(double lat, double lon, int treatAsError) {
845847
}
846848

847849

848-
int encode_robustness_tests() {
850+
static int encode_robustness_tests(void) {
849851
int nrTests = 0;
850852
double d;
851853
unsigned char *b = (unsigned char *) &d;
@@ -920,7 +922,7 @@ int encode_robustness_tests() {
920922
}
921923

922924

923-
int check_incorrect_decode_test(char *mc, int tc) {
925+
static int check_incorrect_decode_test(char *mc, int tc) {
924926
double lat;
925927
double lon;
926928
int rc = decodeMapcodeToLatLon(&lat, &lon, mc, tc);
@@ -932,7 +934,7 @@ int check_incorrect_decode_test(char *mc, int tc) {
932934
}
933935

934936

935-
int check_correct_decode_test(char *mc, int tc) {
937+
static int check_correct_decode_test(char *mc, int tc) {
936938
double lat;
937939
double lon;
938940
int rc = decodeMapcodeToLatLon(&lat, &lon, mc, tc);
@@ -944,11 +946,11 @@ int check_correct_decode_test(char *mc, int tc) {
944946
}
945947

946948

947-
int decode_robustness_tests() {
949+
static int decode_robustness_tests(void) {
948950
int nrTests = 0;
949951
int i;
950952
char s1[1];
951-
char s10k[10000];
953+
char largeString[LARGE_NUMBER];
952954

953955
int tc = getTerritoryCode("NLD", 0);
954956
nrTests += check_incorrect_decode_test("", 0);
@@ -965,16 +967,16 @@ int decode_robustness_tests() {
965967
nrTests += check_incorrect_decode_test(s1, 0);
966968
nrTests += check_incorrect_decode_test(s1, tc);
967969

968-
for (i = 0; i < sizeof(s10k); ++i) {
969-
s10k[i] = (char) ((i % 223) + 32);
970+
for (i = 0; i < sizeof(largeString); ++i) {
971+
largeString[i] = (char) ((i % 223) + 32);
970972
}
971973
nrTests += check_incorrect_decode_test(s1, 0);
972974
nrTests += check_incorrect_decode_test(s1, tc);
973975
return nrTests;
974976
}
975977

976978

977-
int check_alphabet_assertion(char *msg, int condition, char *format, int a) {
979+
static int check_alphabet_assertion(char *msg, int condition, char *format, int a) {
978980
if (condition == 0) {
979981
found_error();
980982
printf("*** ERROR *** %s, ", msg);
@@ -985,20 +987,20 @@ int check_alphabet_assertion(char *msg, int condition, char *format, int a) {
985987
}
986988

987989

988-
int alphabet_robustness_tests() {
990+
static int alphabet_robustness_tests(void) {
989991
int nrTests = 0;
990992
int i;
991993
int a;
992994
char s1[1];
993-
char s10k[10000];
995+
char largeString[LARGE_NUMBER];
994996
char *ps;
995997
UWORD u1[1];
996-
UWORD u10k[10000];
998+
UWORD largeUnicodeString[LARGE_NUMBER];
997999
UWORD *pu;
9981000

9991001
s1[0] = 0;
1000-
for (i = 0; i < sizeof(s10k); ++i) {
1001-
s10k[i] = (char) ((i % 223) + 32);
1002+
for (i = 0; i < sizeof(largeString); ++i) {
1003+
largeString[i] = (char) ((i % 223) + 32);
10021004
}
10031005

10041006
for (a = 0; a < MAPCODE_ALPHABETS_TOTAL; a++) {
@@ -1011,19 +1013,18 @@ int alphabet_robustness_tests() {
10111013
nrTests += check_alphabet_assertion("convertToRoman cannot return 0", ps != 0, "alphabet=%d", a);
10121014
nrTests += check_alphabet_assertion("convertToRoman must return empty string", ps[0] == 0, "alphabet=%d", a);
10131015

1014-
pu = convertToAlphabet(u10k, sizeof(u10k), s10k, 0);
1016+
pu = convertToAlphabet(largeUnicodeString, sizeof(largeUnicodeString) / sizeof(largeUnicodeString[0]), largeString, 0);
10151017
nrTests += check_alphabet_assertion("convertToAlphabet cannot return 0", pu != 0, "alphabet=%d", a);
10161018

1017-
ps = convertToRoman(s10k, sizeof(s10k), pu);
1019+
ps = convertToRoman(largeString, sizeof(largeString) / sizeof(largeString[0]), pu);
10181020
nrTests += check_alphabet_assertion("convertToRoman cannot return 0", ps != 0, "alphabet=%d", a);
1019-
nrTests += check_alphabet_assertion("convertToRoman must return size", strlen(ps) < sizeof(s10k), "alphabet=%d",
1020-
a);
1021+
nrTests += check_alphabet_assertion("convertToRoman must return size", strlen(ps) < sizeof(largeString), "alphabet=%d", a);
10211022
}
10221023
return nrTests;
10231024
}
10241025

10251026

1026-
int robustness_tests() {
1027+
static int robustness_tests(void) {
10271028
int nrTests = 0;
10281029
nrTests += get_territory_robustness_tests();
10291030
nrTests += encode_robustness_tests();
@@ -1032,7 +1033,7 @@ int robustness_tests() {
10321033
return nrTests;
10331034
}
10341035

1035-
int alphabet_per_territory_tests() {
1036+
static int alphabet_per_territory_tests(void) {
10361037
int nrTests = 0;
10371038
int i, j;
10381039
for (i = 0; i < MAX_CCODE; i++) {
@@ -1054,7 +1055,7 @@ int alphabet_per_territory_tests() {
10541055
return nrTests;
10551056
}
10561057

1057-
int test_territories_csv() {
1058+
static int test_territories_csv(void) {
10581059
int nrTests = 0;
10591060
int linesTested = 0;
10601061
const char *csvName = "territories.csv";

0 commit comments

Comments
 (0)