diff options
author | Sverker Eriksson <sverker@erlang.org> | 2021-08-24 18:56:52 +0200 |
---|---|---|
committer | Sverker Eriksson <sverker@erlang.org> | 2021-08-24 20:11:18 +0200 |
commit | b867de7c9f9f374c5b6156599c44dfa800a488c6 (patch) | |
tree | db33a3a819ff0f4e8366a6f41f243038810122ad | |
parent | 7866fd7c086f4e63a7cb3940adbf59937d2342ee (diff) | |
download | erlang-b867de7c9f9f374c5b6156599c44dfa800a488c6.tar.gz |
erts: Refactor BINARY_OVERFLOW_CHECK into IS_BINARY_SIZE_OK
Don't like control flow hidden in macros if avoidable.
-rw-r--r-- | erts/emulator/beam/erl_binary.h | 17 |
1 files changed, 8 insertions, 9 deletions
diff --git a/erts/emulator/beam/erl_binary.h b/erts/emulator/beam/erl_binary.h index bd3669d896..ae5c07ee4d 100644 --- a/erts/emulator/beam/erl_binary.h +++ b/erts/emulator/beam/erl_binary.h @@ -358,12 +358,8 @@ erts_free_aligned_binary_bytes(byte* buf) * * This check also ensures, indirectly, that there won't be an overflow when * the size is bumped by CHICKEN_PAD and the binary struct itself. */ -#define BINARY_OVERFLOW_CHECK(BYTE_SIZE) \ - do { \ - if (ERTS_UNLIKELY(BYTE_SIZE > ERTS_UWORD_MAX / CHAR_BIT)) { \ - return NULL; \ - } \ - } while(0) +#define IS_BINARY_SIZE_OK(BYTE_SIZE) \ + ERTS_LIKELY(BYTE_SIZE <= ERTS_UWORD_MAX / CHAR_BIT) /* Explicit extra bytes allocated to counter buggy drivers. ** These extra bytes where earlier (< R13B04) added by an alignment-bug @@ -381,7 +377,8 @@ erts_bin_drv_alloc_fnf(Uint size) Binary *res; Uint bsize; - BINARY_OVERFLOW_CHECK(size); + if (!IS_BINARY_SIZE_OK(size)) + return NULL; bsize = ERTS_SIZEOF_Binary(size) + CHICKEN_PAD; res = (Binary *)erts_alloc_fnf(ERTS_ALC_T_DRV_BINARY, bsize); @@ -414,7 +411,8 @@ erts_bin_nrml_alloc_fnf(Uint size) Binary *res; Uint bsize; - BINARY_OVERFLOW_CHECK(size); + if (!IS_BINARY_SIZE_OK(size)) + return NULL; bsize = ERTS_SIZEOF_Binary(size) + CHICKEN_PAD; res = (Binary *)erts_alloc_fnf(ERTS_ALC_T_BINARY, bsize); @@ -451,8 +449,9 @@ erts_bin_realloc_fnf(Binary *bp, Uint size) type = (bp->intern.flags & BIN_FLAG_DRV) ? ERTS_ALC_T_DRV_BINARY : ERTS_ALC_T_BINARY; ASSERT((bp->intern.flags & BIN_FLAG_MAGIC) == 0); + if (!IS_BINARY_SIZE_OK(size)) + return NULL; - BINARY_OVERFLOW_CHECK(size); bsize = ERTS_SIZEOF_Binary(size) + CHICKEN_PAD; nbp = (Binary *)erts_realloc_fnf(type, (void *) bp, bsize); |