Re: svn commit: r1902206 - /apr/apr/trunk/encoding/apr_base64.c

"Evgeny Kotkov via dev" <[email protected]>
Newsgroups gmane.comp.apache.apr.devel
Message-ID <CAP_GPNhhr83Ynmqr0ar5G2m7g4Ehf_FSqV0D8ktrUQmJ4MNDTg@mail.gmail.com>
Yann Ylavic <[email protected]> writes:

> So possibly something like the below:
>
> Index: encoding/apr_base64.c
> ===================================================================
> --- encoding/apr_base64.c    (revision 1908764)
> +++ encoding/apr_base64.c    (working copy)
> @@ -20,8 +20,27 @@
>   * ugly 'len' functions, which is quite a nasty cost.
>   */
>
> -#undef NDEBUG /* always abort() on assert()ion failure */
> +/* An APR__ASSERT()ion failure will abort() with or without printing
> + * the faulting condition (and code location) depending on NDEBUG.
> + */
> +#ifndef NDEBUG  /* use assert()/system's printing before abort() mechanism */
>  #include <assert.h>
> +#define APR__ASSERT(cond) assert(cond)
> +#else           /* just abort() */
> +#if APR_HAVE_STDLIB_H
> +#include <stdlib.h>
> +#endif
> +#if HAVE___BUILTIN_EXPECT
> +#define APR__UNLIKELY(cond) __builtin_expect(!!(cond), 0)
> +#else
> +#define APR__UNLIKELY(cond) (!!(cond))
> +#endif
> +#define APR__ASSERT(cond) do { \
> +    if (APR__UNLIKELY(!(cond))) { \
> +        abort(); \
> +    } \
> +} while (0)
> +#endif

Yes, something along these lines, although I was thinking we could maybe
get away with just adding a small helper that covers all cases, as in the
attached patch.


Thanks,
Evgeny Kotkov
apr-base64-verify-condition-v1.patch.txt (text/plain, 2.8 KB)
Index: encoding/apr_base64.c
===================================================================
--- encoding/apr_base64.c	(revision 1908804)
+++ encoding/apr_base64.c	(working copy)
@@ -20,7 +20,6 @@
  * ugly 'len' functions, which is quite a nasty cost.
  */
 
-#undef NDEBUG /* always abort() on assert()ion failure */
 #include <assert.h>
 
 #include "apr_base64.h"
@@ -28,6 +27,17 @@
 #include "apr_xlate.h"
 #endif                /* APR_CHARSET_EBCDIC */
 
+#if APR_HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+#define verify_condition(cond) do { \
+    if (!(cond)) {                  \
+        assert(0 && #cond);         \
+        abort();                    \
+    }                               \
+} while (0)
+
 /* Above APR_BASE64_ENCODE_MAX length the encoding can't fit in an int >= 0 */
 #define APR_BASE64_ENCODE_MAX 1610612733
 
@@ -124,7 +134,7 @@ APR_DECLARE(int) apr_base64_decode_len(const char
     bufin = (const unsigned char *) bufcoded;
     while (pr2six[*(bufin++)] <= 63);
     nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
-    assert(nprbytes <= APR_BASE64_DECODE_MAX);
+    verify_condition(nprbytes <= APR_BASE64_DECODE_MAX);
 
     return (int)(((nprbytes + 3u) / 4u) * 3u + 1u);
 }
@@ -161,7 +171,7 @@ APR_DECLARE(int) apr_base64_decode_binary(unsigned
     bufin = (const unsigned char *) bufcoded;
     while (pr2six[*(bufin++)] <= 63);
     nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
-    assert(nprbytes <= APR_BASE64_DECODE_MAX);
+    verify_condition(nprbytes <= APR_BASE64_DECODE_MAX);
     nbytesdecoded = (int)(((nprbytes + 3u) / 4u) * 3u);
 
     bufout = (unsigned char *) bufplain;
@@ -206,7 +216,7 @@ static const char basis_64[] =
 
 APR_DECLARE(int) apr_base64_encode_len(int len)
 {
-    assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
+    verify_condition(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
 
     return ((len + 2) / 3 * 4) + 1;
 }
@@ -219,7 +229,7 @@ APR_DECLARE(int) apr_base64_encode(char *encoded,
     int i;
     char *p;
 
-    assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
+    verify_condition(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
 
     p = encoded;
     for (i = 0; i < len - 2; i += 3) {
@@ -258,7 +268,7 @@ APR_DECLARE(int) apr_base64_encode_binary(char *en
     int i;
     char *p;
 
-    assert(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
+    verify_condition(len >= 0 && len <= APR_BASE64_ENCODE_MAX);
 
     p = encoded;
     for (i = 0; i < len - 2; i += 3) {
@@ -292,7 +302,7 @@ APR_DECLARE(char *) apr_pbase64_encode(apr_pool_t
     char *encoded;
     apr_size_t len = strlen(string);
 
-    assert(len <= (apr_size_t)APR_BASE64_ENCODE_MAX);
+    verify_condition(len <= (apr_size_t)APR_BASE64_ENCODE_MAX);
     encoded = (char *) apr_palloc(p, apr_base64_encode_len((int)len));
     apr_base64_encode(encoded, string, (int)len);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.