opj_j2k_get_default_thread_count(): validate value of OPJ_NUM_THREADS to fix Coverity...
authorEven Rouault <even.rouault@spatialys.com>
Thu, 21 Sep 2017 12:06:03 +0000 (14:06 +0200)
committerEven Rouault <even.rouault@spatialys.com>
Thu, 21 Sep 2017 12:06:03 +0000 (14:06 +0200)
src/lib/openjp2/j2k.c

index cda722b46c65d23bf4c7eae7d59a06f5789c871b..f517ff882466a207dcb0f0992d83251a9f183678 100644 (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()
 {
-    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;
 }
 
 /* ----------------------------------------------------------------------- */