Use the return value from write(2)
[openssl.git] / crypto / ex_data.c
index 4a3201a9535591f062bbd752698782673eb542ec..22c4d3d9b904d73c5db4117faf59467e272de597 100644 (file)
@@ -287,7 +287,14 @@ int CRYPTO_dup_ex_data(int class_index, CRYPTO_EX_DATA *to,
         CRYPTOerr(CRYPTO_F_CRYPTO_DUP_EX_DATA, ERR_R_MALLOC_FAILURE);
         return 0;
     }
-    if (!CRYPTO_set_ex_data(to, mx - 1, NULL))
+    /*
+     * Make sure the ex_data stack is at least |mx| elements long to avoid
+     * issues in the for loop that follows; so go get the |mx|'th element
+     * (if it does not exist CRYPTO_get_ex_data() returns NULL), and assign
+     * to itself. This is normally a no-op; but ensures the stack is the
+     * proper size
+     */
+    if (!CRYPTO_set_ex_data(to, mx - 1, CRYPTO_get_ex_data(to, mx - 1)))
         goto err;
 
     for (i = 0; i < mx; i++) {