opj_j2k_get_default_thread_count(): validate value of OPJ_NUM_THREADS to fix Coverity 179465 and 179463

This commit is contained in:
Even Rouault 2017-09-21 14:06:03 +02:00
parent 68e596dada
commit 19e157871f
1 changed files with 18 additions and 5 deletions

View File

@ -6434,14 +6434,27 @@ OPJ_BOOL opj_j2k_set_threads(opj_j2k_t *j2k, OPJ_UINT32 num_threads)
static int opj_j2k_get_default_thread_count() static int opj_j2k_get_default_thread_count()
{ {
const char* num_threads = getenv("OPJ_NUM_THREADS"); const char* num_threads_str = getenv("OPJ_NUM_THREADS");
if (num_threads == NULL || !opj_has_thread_support()) { int num_cpus;
int num_threads;
if (num_threads_str == NULL || !opj_has_thread_support()) {
return 0; return 0;
} }
if (strcmp(num_threads, "ALL_CPUS") == 0) { num_cpus = opj_get_num_cpus();
return opj_get_num_cpus(); if (strcmp(num_threads_str, "ALL_CPUS") == 0) {
return num_cpus;
} }
return atoi(num_threads); if (num_cpus == 0) {
num_cpus = 32;
}
num_threads = atoi(num_threads_str);
if (num_threads < 0) {
num_threads = 0;
} else if (num_threads > 2 * num_cpus) {
num_threads = 2 * num_cpus;
}
return num_threads;
} }
/* ----------------------------------------------------------------------- */ /* ----------------------------------------------------------------------- */