1 /*
2  * Test program that uses std::string object from more than one thread and
3  * that also triggers a call to __GI_strlen() (from inside strdup()). See also
4  * https://bugs.kde.org/show_bug.cgi?id=326091.
5  */
6 
7 #include <list>
8 #include <string>
9 #include <cstring>
10 #include <pthread.h>
11 #include <unistd.h>
12 
list2byteArray()13 char* list2byteArray()
14 {
15   size_t data_size = 24;
16   char *data = new char[data_size];
17   for (size_t i = 0; i < data_size; i++)
18     data[i] = 'a';
19   data[data_size - 1] = 0;
20   char *ret = strdup(data);
21   delete[] data;
22   return ret;
23 }
24 
addRecord()25 int addRecord()
26 {
27   char *data = list2byteArray();
28   usleep(100);
29   free(data);
30   return 0;
31 }
32 
fillTable(void * ptr)33 void *fillTable(void *ptr)
34 {
35   for (int i = 0; i < 100; i++) {
36     std::string id("000");
37     id.append(1, 'a' + i);
38     std::list<std::string> record;
39     record.push_back("some data");
40     addRecord();
41   }
42   usleep(1000 * 1000);
43   return NULL;
44 }
45 
main(int argc,char * argv[])46 int main(int argc, char* argv[])
47 {
48   pthread_t thread[2];
49 
50   for (int i = 0; i < sizeof(thread)/sizeof(thread[0]); i++) {
51     int ret = pthread_create(&thread[i], NULL, &fillTable, NULL);
52     if (ret) {
53       fprintf(stderr, "Failed to create thread %d: %d\n", i, ret);
54       return 1;
55     }
56   }
57 
58   for (int i = 0; i < sizeof(thread)/sizeof(thread[0]); i++) {
59     int ret = pthread_join(thread[i], NULL);
60     if (ret != 0) {
61       fprintf(stderr, "Failed to join thread %d: %d\n", i, ret);
62       return 1;
63     }
64   }
65 
66   return 0;
67 }
68