summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorEven Rouault <even.rouault@spatialys.com>2017-09-21 14:06:03 +0200
committerEven Rouault <even.rouault@spatialys.com>2017-09-21 14:06:03 +0200
commit19e157871ff28bb5e8296f9e9f9aee6a17a37ace (patch)
treee60f28e059471f7d13648cc2fed451989e062d0e /src/lib
parent68e596dada2ee38a66b01494e5a425e623688300 (diff)
opj_j2k_get_default_thread_count(): validate value of OPJ_NUM_THREADS to fix Coverity 179465 and 179463
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/openjp2/j2k.c23
1 files changed, 18 insertions, 5 deletions
diff --git a/src/lib/openjp2/j2k.c b/src/lib/openjp2/j2k.c
index cda722b4..f517ff88 100644
--- a/src/lib/openjp2/j2k.c
+++ b/src/lib/openjp2/j2k.c
@@ -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()
{
- const char* num_threads = getenv("OPJ_NUM_THREADS");
- if (num_threads == NULL || !opj_has_thread_support()) {
+ const char* num_threads_str = getenv("OPJ_NUM_THREADS");
+ int num_cpus;
+ int num_threads;
+
+ if (num_threads_str == NULL || !opj_has_thread_support()) {
return 0;
}
- if (strcmp(num_threads, "ALL_CPUS") == 0) {
- return opj_get_num_cpus();
+ num_cpus = opj_get_num_cpus();
+ if (strcmp(num_threads_str, "ALL_CPUS") == 0) {
+ return num_cpus;
+ }
+ 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 atoi(num_threads);
+ return num_threads;
}
/* ----------------------------------------------------------------------- */