Commit d897015d authored by Sergey Vojtovich's avatar Sergey Vojtovich

MDEV-8399 - [PATCH] Missing Sanity Checks for memory allocation in MariaDB

- since param is quite small store it on stack
- fixed a few memory leaks
parent cb3a71d2
...@@ -694,7 +694,7 @@ static void *test_thread(void *arg) ...@@ -694,7 +694,7 @@ static void *test_thread(void *arg)
thread_count--; thread_count--;
mysql_cond_signal(&COND_thread_count); /* Tell main we are ready */ mysql_cond_signal(&COND_thread_count); /* Tell main we are ready */
mysql_mutex_unlock(&LOCK_thread_count); mysql_mutex_unlock(&LOCK_thread_count);
free((uchar*) arg); my_thread_end();
return 0; return 0;
} }
...@@ -771,7 +771,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) ...@@ -771,7 +771,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
{ {
pthread_t tid; pthread_t tid;
pthread_attr_t thr_attr; pthread_attr_t thr_attr;
int i,*param,error; int i, param[2], error;
sigset_t set; sigset_t set;
ALARM_INFO alarm_info; ALARM_INFO alarm_info;
MY_INIT(argv[0]); MY_INIT(argv[0]);
...@@ -815,12 +815,11 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) ...@@ -815,12 +815,11 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
printf("Main thread: %s\n",my_thread_name()); printf("Main thread: %s\n",my_thread_name());
for (i=0 ; i < 2 ; i++) for (i=0 ; i < 2 ; i++)
{ {
param=(int*) malloc(sizeof(int)); param[i]= i;
*param= i;
mysql_mutex_lock(&LOCK_thread_count); mysql_mutex_lock(&LOCK_thread_count);
if ((error= mysql_thread_create(0, if ((error= mysql_thread_create(0,
&tid, &thr_attr, test_thread, &tid, &thr_attr, test_thread,
(void*) param))) (void*) &param[i])))
{ {
printf("Can't create thread %d, error: %d\n",i,error); printf("Can't create thread %d, error: %d\n",i,error);
exit(1); exit(1);
...@@ -851,6 +850,9 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) ...@@ -851,6 +850,9 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
alarm_info.active_alarms, alarm_info.max_used_alarms, alarm_info.active_alarms, alarm_info.max_used_alarms,
alarm_info.next_alarm_time); alarm_info.next_alarm_time);
printf("Test succeeded\n"); printf("Test succeeded\n");
mysql_cond_destroy(&COND_thread_count);
mysql_mutex_destroy(&LOCK_thread_count);
my_end(MY_CHECK_ERROR);
return 0; return 0;
} }
......
...@@ -1726,7 +1726,7 @@ static void *test_thread(void *arg) ...@@ -1726,7 +1726,7 @@ static void *test_thread(void *arg)
thread_count--; thread_count--;
mysql_cond_signal(&COND_thread_count); /* Tell main we are ready */ mysql_cond_signal(&COND_thread_count); /* Tell main we are ready */
mysql_mutex_unlock(&LOCK_thread_count); mysql_mutex_unlock(&LOCK_thread_count);
free((uchar*) arg); my_thread_end();
return 0; return 0;
} }
...@@ -1735,7 +1735,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) ...@@ -1735,7 +1735,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
{ {
pthread_t tid; pthread_t tid;
pthread_attr_t thr_attr; pthread_attr_t thr_attr;
int *param,error; int param[array_elements(lock_counts)], error;
uint i; uint i;
MY_INIT(argv[0]); MY_INIT(argv[0]);
if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '#') if (argc > 1 && argv[1][0] == '-' && argv[1][1] == '#')
...@@ -1791,8 +1791,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) ...@@ -1791,8 +1791,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
#endif #endif
for (i=0 ; i < array_elements(lock_counts) ; i++) for (i=0 ; i < array_elements(lock_counts) ; i++)
{ {
param=(int*) malloc(sizeof(int)); param[i]= i;
*param=i;
if ((error= mysql_mutex_lock(&LOCK_thread_count))) if ((error= mysql_mutex_lock(&LOCK_thread_count)))
{ {
...@@ -1802,7 +1801,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) ...@@ -1802,7 +1801,7 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
} }
if ((error= mysql_thread_create(0, if ((error= mysql_thread_create(0,
&tid, &thr_attr, test_thread, &tid, &thr_attr, test_thread,
(void*) param))) (void*) &param[i])))
{ {
fprintf(stderr, "Got error: %d from mysql_thread_create (errno: %d)\n", fprintf(stderr, "Got error: %d from mysql_thread_create (errno: %d)\n",
error, errno); error, errno);
...@@ -1831,6 +1830,9 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused))) ...@@ -1831,6 +1830,9 @@ int main(int argc __attribute__((unused)),char **argv __attribute__((unused)))
else else
#endif #endif
printf("Test succeeded\n"); printf("Test succeeded\n");
mysql_cond_destroy(&COND_thread_count);
mysql_mutex_destroy(&LOCK_thread_count);
my_end(MY_CHECK_ERROR);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment