Work around Travis "virtual memory exhausted" error
[openssl.git] / crypto / bio / bio_lib.c
index 6d958b1e92af650271f7b827cfebb32a69cdfc22..67acac3d28a27591cb0a11f26988ede145627663 100644 (file)
@@ -61,9 +61,6 @@ static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
 
     ret = b->callback(b, oper, argp, argi, argl, inret);
 
-    if (ret > LONG_MAX || ret < LONG_MIN)
-        return -1;
-
     if (ret >= 0 && (HAS_LEN_OPER(bareoper) || bareoper == BIO_CB_PUTS)) {
         *processed = (size_t)ret;
         ret = 1;
@@ -116,7 +113,7 @@ int BIO_free(BIO *a)
     if (a == NULL)
         return 0;
 
-    if (CRYPTO_atomic_add(&a->references, -1, &ret, a->lock) <= 0)
+    if (CRYPTO_DOWN_REF(&a->references, &ret, a->lock) <= 0)
         return 0;
 
     REF_PRINT_COUNT("BIO", a);
@@ -181,7 +178,7 @@ int BIO_up_ref(BIO *a)
 {
     int i;
 
-    if (CRYPTO_atomic_add(&a->references, 1, &i, a->lock) <= 0)
+    if (CRYPTO_UP_REF(&a->references, &i, a->lock) <= 0)
         return 0;
 
     REF_PRINT_COUNT("BIO", a);
@@ -246,64 +243,70 @@ int BIO_method_type(const BIO *b)
 
 /*
  * This is essentially the same as BIO_read_ex() except that it allows
- * 0 or a -ve value to indicate failure (retryable or not) in the return. This
- * is for compatibility with the old style BIO_read(), where existing code may
- * make assumptions about the return value that it might get.
+ * 0 or a negative value to indicate failure (retryable or not) in the return.
+ * This is for compatibility with the old style BIO_read(), where existing code
+ * may make assumptions about the return value that it might get.
  */
-static int bio_read_intern(BIO *b, void *out, size_t outl, size_t *read)
+static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
 {
     int ret;
 
     if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
-        BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNSUPPORTED_METHOD);
+        BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNSUPPORTED_METHOD);
         return -2;
     }
 
     if ((b->callback != NULL || b->callback_ex != NULL) &&
-        ((ret = (int)bio_call_callback(b, BIO_CB_READ, out, outl, 0, 0L, 1L,
-                                       read)) <= 0))
+        ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
+                                       readbytes)) <= 0))
         return ret;
 
     if (!b->init) {
-        BIOerr(BIO_F_BIO_READ_EX, BIO_R_UNINITIALIZED);
+        BIOerr(BIO_F_BIO_READ_INTERN, BIO_R_UNINITIALIZED);
         return -2;
     }
 
-    ret = b->method->bread(b, out, outl, read);
+    ret = b->method->bread(b, data, dlen, readbytes);
 
     if (ret > 0)
-        b->num_read += (uint64_t)*read;
+        b->num_read += (uint64_t)*readbytes;
 
     if (b->callback != NULL || b->callback_ex != NULL)
-        ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, out, outl,
-                                     0, 0L, ret, read);
+        ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
+                                     dlen, 0, 0L, ret, readbytes);
+
+    /* Shouldn't happen */
+    if (ret > 0 && *readbytes > dlen) {
+        BIOerr(BIO_F_BIO_READ_INTERN, ERR_R_INTERNAL_ERROR);
+        return -1;
+    }
 
     return ret;
 }
 
-int BIO_read(BIO *b, void *out, int outl)
+int BIO_read(BIO *b, void *data, int dlen)
 {
-    size_t read;
+    size_t readbytes;
     int ret;
 
-    if (outl < 0)
+    if (dlen < 0)
         return 0;
 
-    ret = bio_read_intern(b, out, (size_t)outl, &read);
+    ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
 
     if (ret > 0) {
-        /* *read should always be <= outl */
-        ret = (int)read;
+        /* *readbytes should always be <= dlen */
+        ret = (int)readbytes;
     }
 
     return ret;
 }
 
-int BIO_read_ex(BIO *b, void *out, size_t outl, size_t *read)
+int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
 {
     int ret;
 
-    ret = bio_read_intern(b, out, outl, read);
+    ret = bio_read_intern(b, data, dlen, readbytes);
 
     if (ret > 0)
         ret = 1;
@@ -313,7 +316,8 @@ int BIO_read_ex(BIO *b, void *out, size_t outl, size_t *read)
     return ret;
 }
 
-static int bio_write_intern(BIO *b, const void *in, size_t inl, size_t *written)
+static int bio_write_intern(BIO *b, const void *data, size_t dlen,
+                            size_t *written)
 {
     int ret;
 
@@ -321,55 +325,55 @@ static int bio_write_intern(BIO *b, const void *in, size_t inl, size_t *written)
         return 0;
 
     if ((b->method == NULL) || (b->method->bwrite == NULL)) {
-        BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNSUPPORTED_METHOD);
+        BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNSUPPORTED_METHOD);
         return -2;
     }
 
     if ((b->callback != NULL || b->callback_ex != NULL) &&
-        ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, in, inl, 0, 0L, 1L,
+        ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
                                        written)) <= 0))
         return ret;
 
     if (!b->init) {
-        BIOerr(BIO_F_BIO_WRITE_EX, BIO_R_UNINITIALIZED);
+        BIOerr(BIO_F_BIO_WRITE_INTERN, BIO_R_UNINITIALIZED);
         return -2;
     }
 
-    ret = b->method->bwrite(b, in, inl, written);
+    ret = b->method->bwrite(b, data, dlen, written);
 
     if (ret > 0)
         b->num_write += (uint64_t)*written;
 
     if (b->callback != NULL || b->callback_ex != NULL)
-        ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl,
-                                     0, 0L, ret, written);
+        ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
+                                     dlen, 0, 0L, ret, written);
 
     return ret;
 }
 
-int BIO_write(BIO *b, const void *in, int inl)
+int BIO_write(BIO *b, const void *data, int dlen)
 {
     size_t written;
     int ret;
 
-    if (inl < 0)
+    if (dlen < 0)
         return 0;
 
-    ret = bio_write_intern(b, in, (size_t)inl, &written);
+    ret = bio_write_intern(b, data, (size_t)dlen, &written);
 
     if (ret > 0) {
-        /* *written should always be <= inl */
+        /* *written should always be <= dlen */
         ret = (int)written;
     }
 
     return ret;
 }
 
-int BIO_write_ex(BIO *b, const void *in, size_t inl, size_t *written)
+int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
 {
     int ret;
 
-    ret = bio_write_intern(b, in, inl, written);
+    ret = bio_write_intern(b, data, dlen, written);
 
     if (ret > 0)
         ret = 1;
@@ -379,7 +383,7 @@ int BIO_write_ex(BIO *b, const void *in, size_t inl, size_t *written)
     return ret;
 }
 
-int BIO_puts(BIO *b, const char *in)
+int BIO_puts(BIO *b, const char *buf)
 {
     int ret;
     size_t written = 0;
@@ -390,7 +394,7 @@ int BIO_puts(BIO *b, const char *in)
     }
 
     if (b->callback != NULL || b->callback_ex != NULL) {
-        ret = (int)bio_call_callback(b, BIO_CB_PUTS, in, 0, 0, 0L, 1L, NULL);
+        ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
         if (ret <= 0)
             return ret;
     }
@@ -400,7 +404,7 @@ int BIO_puts(BIO *b, const char *in)
         return -2;
     }
 
-    ret = b->method->bputs(b, in);
+    ret = b->method->bputs(b, buf);
 
     if (ret > 0) {
         b->num_write += (uint64_t)ret;
@@ -409,31 +413,38 @@ int BIO_puts(BIO *b, const char *in)
     }
 
     if (b->callback != NULL || b->callback_ex != NULL)
-        ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0,
+        ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
                                      0L, ret, &written);
 
     if (ret > 0) {
-        if (written > INT_MAX)
+        if (written > INT_MAX) {
+            BIOerr(BIO_F_BIO_PUTS, BIO_R_LENGTH_TOO_LONG);
             ret = -1;
-        else
+        } else {
             ret = (int)written;
+        }
     }
 
     return ret;
 }
 
-int BIO_gets(BIO *b, char *out, int outl)
+int BIO_gets(BIO *b, char *buf, int size)
 {
     int ret;
-    size_t read = 0;
+    size_t readbytes = 0;
 
     if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
         BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
         return (-2);
     }
 
+    if (size < 0) {
+        BIOerr(BIO_F_BIO_GETS, BIO_R_INVALID_ARGUMENT);
+        return 0;
+    }
+
     if (b->callback != NULL || b->callback_ex != NULL) {
-        ret = (int)bio_call_callback(b, BIO_CB_GETS, out, outl, 0, 0L, 1, NULL);
+        ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
         if (ret <= 0)
             return ret;
     }
@@ -443,22 +454,23 @@ int BIO_gets(BIO *b, char *out, int outl)
         return (-2);
     }
 
-    ret = b->method->bgets(b, out, outl);
+    ret = b->method->bgets(b, buf, size);
 
     if (ret > 0) {
-        read = ret;
+        readbytes = ret;
         ret = 1;
     }
 
     if (b->callback != NULL || b->callback_ex != NULL)
-        ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, out, outl,
-                                     0, 0L, ret, &read);
+        ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
+                                     0, 0L, ret, &readbytes);
 
     if (ret > 0) {
-        if (read > INT_MAX)
+        /* Shouldn't happen */
+        if (readbytes > (size_t)size)
             ret = -1;
         else
-            ret = (int)read;
+            ret = (int)readbytes;
     }
 
     return ret;