123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611 |
- /*
- * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License").
- * You may not use this file except in compliance with the License.
- * A copy of the License is located at
- *
- * http://aws.amazon.com/apache2.0
- *
- * or in the "license" file accompanying this file. This file is distributed
- * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
- * express or implied. See the License for the specific language governing
- * permissions and limitations under the License.
- */
- #pragma once
- /**
- * DO NOT DIRECTLY MODIFY THIS FILE:
- *
- * The code in this file is generated from scripts/s2n_safety_macros.py and any modifications
- * should be in there.
- */
- /* clang-format off */
- #include "error/s2n_errno.h"
- #include "utils/s2n_ensure.h"
- #include "utils/s2n_result.h"
- /**
- * The goal of s2n_safety is to provide helpers to perform common
- * checks, which help with code readability.
- */
- /* Success signal value for OpenSSL functions */
- #define _OSSL_SUCCESS 1
- /**
- * Sets the global `s2n_errno` to `error` and returns with an `S2N_RESULT_ERROR`
- */
- #define RESULT_BAIL(error) do { _S2N_ERROR((error)); __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR); } while (0)
- /**
- * Ensures the `condition` is `true`, otherwise the function will `RESULT_BAIL` with `error`
- */
- #define RESULT_ENSURE(condition, error) __S2N_ENSURE((condition), RESULT_BAIL(error))
- /**
- * Ensures the `condition` is `true`, otherwise the function will `RESULT_BAIL` with `error`
- *
- * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
- * In release mode, the check is removed.
- */
- #define RESULT_DEBUG_ENSURE(condition, error) __S2N_ENSURE_DEBUG((condition), RESULT_BAIL(error))
- /**
- * Ensures `s2n_result_is_ok(result)`, otherwise the function will `RESULT_BAIL` with `error`
- *
- * This can be useful for overriding the global `s2n_errno`
- */
- #define RESULT_ENSURE_OK(result, error) __S2N_ENSURE(s2n_result_is_ok(result), RESULT_BAIL(error))
- /**
- * Ensures `a` is greater than or equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define RESULT_ENSURE_GTE(a, b) __S2N_ENSURE((a) >= (b), RESULT_BAIL(S2N_ERR_SAFETY))
- /**
- * Ensures `a` is less than or equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define RESULT_ENSURE_LTE(a, b) __S2N_ENSURE((a) <= (b), RESULT_BAIL(S2N_ERR_SAFETY))
- /**
- * Ensures `a` is greater than `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define RESULT_ENSURE_GT(a, b) __S2N_ENSURE((a) > (b), RESULT_BAIL(S2N_ERR_SAFETY))
- /**
- * Ensures `a` is less than `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define RESULT_ENSURE_LT(a, b) __S2N_ENSURE((a) < (b), RESULT_BAIL(S2N_ERR_SAFETY))
- /**
- * Ensures `a` is equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define RESULT_ENSURE_EQ(a, b) __S2N_ENSURE((a) == (b), RESULT_BAIL(S2N_ERR_SAFETY))
- /**
- * Ensures `a` is not equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define RESULT_ENSURE_NE(a, b) __S2N_ENSURE((a) != (b), RESULT_BAIL(S2N_ERR_SAFETY))
- /**
- * Ensures `min <= n <= max`, otherwise the function will `RESULT_BAIL` with `S2N_ERR_SAFETY`
- */
- #define RESULT_ENSURE_INCLUSIVE_RANGE(min, n, max) \
- do { \
- __typeof(n) __tmp_n = ( n ); \
- __typeof(n) __tmp_min = ( min ); \
- __typeof(n) __tmp_max = ( max ); \
- RESULT_ENSURE_GTE(__tmp_n, __tmp_min); \
- RESULT_ENSURE_LTE(__tmp_n, __tmp_max); \
- } while(0)
- /**
- * Ensures `min < n < max`, otherwise the function will `RESULT_BAIL` with `S2N_ERR_SAFETY`
- */
- #define RESULT_ENSURE_EXCLUSIVE_RANGE(min, n, max) \
- do { \
- __typeof(n) __tmp_n = ( n ); \
- __typeof(n) __tmp_min = ( min ); \
- __typeof(n) __tmp_max = ( max ); \
- RESULT_ENSURE_GT(__tmp_n, __tmp_min); \
- RESULT_ENSURE_LT(__tmp_n, __tmp_max); \
- } while(0)
- /**
- * Ensures `x` is a readable reference, otherwise the function will `RESULT_BAIL` with `S2N_ERR_NULL`
- */
- #define RESULT_ENSURE_REF(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_READABLE(x), RESULT_BAIL(S2N_ERR_NULL))
- /**
- * Ensures `x` is a mutable reference, otherwise the function will `RESULT_BAIL` with `S2N_ERR_NULL`
- */
- #define RESULT_ENSURE_MUT(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_WRITABLE(x), RESULT_BAIL(S2N_ERR_NULL))
- /**
- * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
- *
- * `RESULT_PRECONDITION` should be used at the beginning of a function to make assertions about
- * the provided arguments. By default, it is functionally equivalent to `RESULT_GUARD(result)`
- * but can be altered by a testing environment to provide additional guarantees.
- */
- #define RESULT_PRECONDITION(result) RESULT_GUARD(__S2N_ENSURE_PRECONDITION((result)))
- /**
- * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
- *
- * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
- * In release mode, the check is removed.
- *
- * `RESULT_POSTCONDITION` should be used at the end of a function to make assertions about
- * the resulting state. In debug mode, it is functionally equivalent to `RESULT_GUARD(result)`.
- * In production builds, it becomes a no-op. This can also be altered by a testing environment
- * to provide additional guarantees.
- */
- #define RESULT_POSTCONDITION(result) RESULT_GUARD(__S2N_ENSURE_POSTCONDITION((result)))
- /**
- * Performs a safer memcpy.
- *
- * The following checks are performed:
- *
- * * `destination` is non-null
- * * `source` is non-null
- *
- * Callers will still need to ensure the following:
- *
- * * The size of the data pointed to by both the `destination` and `source` parameters,
- * shall be at least `len` bytes.
- */
- #define RESULT_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), RESULT_ENSURE_REF)
- /**
- * Performs a safer memset
- *
- * The following checks are performed:
- *
- * * `destination` is non-null
- *
- * Callers will still need to ensure the following:
- *
- * * The size of the data pointed to by the `destination` parameter shall be at least
- * `len` bytes.
- */
- #define RESULT_CHECKED_MEMSET(destination, value, len) __S2N_ENSURE_SAFE_MEMSET((destination), (value), (len), RESULT_ENSURE_REF)
- /**
- * Ensures `s2n_result_is_ok(result)`, otherwise the function will return `S2N_RESULT_ERROR`
- */
- #define RESULT_GUARD(result) __S2N_ENSURE(s2n_result_is_ok(result), __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR))
- /**
- * Ensures `result == _OSSL_SUCCESS`, otherwise the function will `RESULT_BAIL` with `error`
- */
- #define RESULT_GUARD_OSSL(result, error) __S2N_ENSURE((result) == _OSSL_SUCCESS, RESULT_BAIL(error))
- /**
- * Ensures `(result) > S2N_FAILURE`, otherwise the function will return `S2N_RESULT_ERROR`
- */
- #define RESULT_GUARD_POSIX(result) __S2N_ENSURE((result) > S2N_FAILURE, __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR))
- /**
- * Ensures `(result) != NULL`, otherwise the function will return `S2N_RESULT_ERROR`
- *
- * Does not set s2n_errno to S2N_ERR_NULL, so is NOT a direct replacement for RESULT_ENSURE_REF.
- */
- #define RESULT_GUARD_PTR(result) __S2N_ENSURE((result) != NULL, __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Sets the global `s2n_errno` to `error` and returns with an `S2N_FAILURE`
- */
- #define POSIX_BAIL(error) do { _S2N_ERROR((error)); __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE); } while (0)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `condition` is `true`, otherwise the function will `POSIX_BAIL` with `error`
- */
- #define POSIX_ENSURE(condition, error) __S2N_ENSURE((condition), POSIX_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `condition` is `true`, otherwise the function will `POSIX_BAIL` with `error`
- *
- * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
- * In release mode, the check is removed.
- */
- #define POSIX_DEBUG_ENSURE(condition, error) __S2N_ENSURE_DEBUG((condition), POSIX_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `(result) > S2N_FAILURE`, otherwise the function will `POSIX_BAIL` with `error`
- *
- * This can be useful for overriding the global `s2n_errno`
- */
- #define POSIX_ENSURE_OK(result, error) __S2N_ENSURE((result) > S2N_FAILURE, POSIX_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is greater than or equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define POSIX_ENSURE_GTE(a, b) __S2N_ENSURE((a) >= (b), POSIX_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is less than or equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define POSIX_ENSURE_LTE(a, b) __S2N_ENSURE((a) <= (b), POSIX_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is greater than `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define POSIX_ENSURE_GT(a, b) __S2N_ENSURE((a) > (b), POSIX_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is less than `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define POSIX_ENSURE_LT(a, b) __S2N_ENSURE((a) < (b), POSIX_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define POSIX_ENSURE_EQ(a, b) __S2N_ENSURE((a) == (b), POSIX_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is not equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define POSIX_ENSURE_NE(a, b) __S2N_ENSURE((a) != (b), POSIX_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `min <= n <= max`, otherwise the function will `POSIX_BAIL` with `S2N_ERR_SAFETY`
- */
- #define POSIX_ENSURE_INCLUSIVE_RANGE(min, n, max) \
- do { \
- __typeof(n) __tmp_n = ( n ); \
- __typeof(n) __tmp_min = ( min ); \
- __typeof(n) __tmp_max = ( max ); \
- POSIX_ENSURE_GTE(__tmp_n, __tmp_min); \
- POSIX_ENSURE_LTE(__tmp_n, __tmp_max); \
- } while(0)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `min < n < max`, otherwise the function will `POSIX_BAIL` with `S2N_ERR_SAFETY`
- */
- #define POSIX_ENSURE_EXCLUSIVE_RANGE(min, n, max) \
- do { \
- __typeof(n) __tmp_n = ( n ); \
- __typeof(n) __tmp_min = ( min ); \
- __typeof(n) __tmp_max = ( max ); \
- POSIX_ENSURE_GT(__tmp_n, __tmp_min); \
- POSIX_ENSURE_LT(__tmp_n, __tmp_max); \
- } while(0)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `x` is a readable reference, otherwise the function will `POSIX_BAIL` with `S2N_ERR_NULL`
- */
- #define POSIX_ENSURE_REF(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_READABLE(x), POSIX_BAIL(S2N_ERR_NULL))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `x` is a mutable reference, otherwise the function will `POSIX_BAIL` with `S2N_ERR_NULL`
- */
- #define POSIX_ENSURE_MUT(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_WRITABLE(x), POSIX_BAIL(S2N_ERR_NULL))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
- *
- * `POSIX_PRECONDITION` should be used at the beginning of a function to make assertions about
- * the provided arguments. By default, it is functionally equivalent to `POSIX_GUARD_RESULT(result)`
- * but can be altered by a testing environment to provide additional guarantees.
- */
- #define POSIX_PRECONDITION(result) POSIX_GUARD_RESULT(__S2N_ENSURE_PRECONDITION((result)))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
- *
- * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
- * In release mode, the check is removed.
- *
- * `POSIX_POSTCONDITION` should be used at the end of a function to make assertions about
- * the resulting state. In debug mode, it is functionally equivalent to `POSIX_GUARD_RESULT(result)`.
- * In production builds, it becomes a no-op. This can also be altered by a testing environment
- * to provide additional guarantees.
- */
- #define POSIX_POSTCONDITION(result) POSIX_GUARD_RESULT(__S2N_ENSURE_POSTCONDITION((result)))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Performs a safer memcpy.
- *
- * The following checks are performed:
- *
- * * `destination` is non-null
- * * `source` is non-null
- *
- * Callers will still need to ensure the following:
- *
- * * The size of the data pointed to by both the `destination` and `source` parameters,
- * shall be at least `len` bytes.
- */
- #define POSIX_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), POSIX_ENSURE_REF)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Performs a safer memset
- *
- * The following checks are performed:
- *
- * * `destination` is non-null
- *
- * Callers will still need to ensure the following:
- *
- * * The size of the data pointed to by the `destination` parameter shall be at least
- * `len` bytes.
- */
- #define POSIX_CHECKED_MEMSET(destination, value, len) __S2N_ENSURE_SAFE_MEMSET((destination), (value), (len), POSIX_ENSURE_REF)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `(result) > S2N_FAILURE`, otherwise the function will return `S2N_FAILURE`
- */
- #define POSIX_GUARD(result) __S2N_ENSURE((result) > S2N_FAILURE, __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `result == _OSSL_SUCCESS`, otherwise the function will `POSIX_BAIL` with `error`
- */
- #define POSIX_GUARD_OSSL(result, error) __S2N_ENSURE((result) == _OSSL_SUCCESS, POSIX_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `s2n_result_is_ok(result)`, otherwise the function will return `S2N_FAILURE`
- */
- #define POSIX_GUARD_RESULT(result) __S2N_ENSURE(s2n_result_is_ok(result), __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `(result) != NULL`, otherwise the function will return `S2N_FAILURE`
- *
- * Does not set s2n_errno to S2N_ERR_NULL, so is NOT a direct replacement for POSIX_ENSURE_REF.
- */
- #define POSIX_GUARD_PTR(result) __S2N_ENSURE((result) != NULL, __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Sets the global `s2n_errno` to `error` and returns with an `NULL`
- */
- #define PTR_BAIL(error) do { _S2N_ERROR((error)); __S2N_ENSURE_CHECKED_RETURN(NULL); } while (0)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `condition` is `true`, otherwise the function will `PTR_BAIL` with `error`
- */
- #define PTR_ENSURE(condition, error) __S2N_ENSURE((condition), PTR_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `condition` is `true`, otherwise the function will `PTR_BAIL` with `error`
- *
- * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
- * In release mode, the check is removed.
- */
- #define PTR_DEBUG_ENSURE(condition, error) __S2N_ENSURE_DEBUG((condition), PTR_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `(result) != NULL`, otherwise the function will `PTR_BAIL` with `error`
- *
- * This can be useful for overriding the global `s2n_errno`
- */
- #define PTR_ENSURE_OK(result, error) __S2N_ENSURE((result) != NULL, PTR_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is greater than or equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define PTR_ENSURE_GTE(a, b) __S2N_ENSURE((a) >= (b), PTR_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is less than or equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define PTR_ENSURE_LTE(a, b) __S2N_ENSURE((a) <= (b), PTR_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is greater than `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define PTR_ENSURE_GT(a, b) __S2N_ENSURE((a) > (b), PTR_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is less than `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define PTR_ENSURE_LT(a, b) __S2N_ENSURE((a) < (b), PTR_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define PTR_ENSURE_EQ(a, b) __S2N_ENSURE((a) == (b), PTR_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `a` is not equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
- */
- #define PTR_ENSURE_NE(a, b) __S2N_ENSURE((a) != (b), PTR_BAIL(S2N_ERR_SAFETY))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `min <= n <= max`, otherwise the function will `PTR_BAIL` with `S2N_ERR_SAFETY`
- */
- #define PTR_ENSURE_INCLUSIVE_RANGE(min, n, max) \
- do { \
- __typeof(n) __tmp_n = ( n ); \
- __typeof(n) __tmp_min = ( min ); \
- __typeof(n) __tmp_max = ( max ); \
- PTR_ENSURE_GTE(__tmp_n, __tmp_min); \
- PTR_ENSURE_LTE(__tmp_n, __tmp_max); \
- } while(0)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `min < n < max`, otherwise the function will `PTR_BAIL` with `S2N_ERR_SAFETY`
- */
- #define PTR_ENSURE_EXCLUSIVE_RANGE(min, n, max) \
- do { \
- __typeof(n) __tmp_n = ( n ); \
- __typeof(n) __tmp_min = ( min ); \
- __typeof(n) __tmp_max = ( max ); \
- PTR_ENSURE_GT(__tmp_n, __tmp_min); \
- PTR_ENSURE_LT(__tmp_n, __tmp_max); \
- } while(0)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `x` is a readable reference, otherwise the function will `PTR_BAIL` with `S2N_ERR_NULL`
- */
- #define PTR_ENSURE_REF(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_READABLE(x), PTR_BAIL(S2N_ERR_NULL))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `x` is a mutable reference, otherwise the function will `PTR_BAIL` with `S2N_ERR_NULL`
- */
- #define PTR_ENSURE_MUT(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_WRITABLE(x), PTR_BAIL(S2N_ERR_NULL))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
- *
- * `PTR_PRECONDITION` should be used at the beginning of a function to make assertions about
- * the provided arguments. By default, it is functionally equivalent to `PTR_GUARD_RESULT(result)`
- * but can be altered by a testing environment to provide additional guarantees.
- */
- #define PTR_PRECONDITION(result) PTR_GUARD_RESULT(__S2N_ENSURE_PRECONDITION((result)))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
- *
- * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
- * In release mode, the check is removed.
- *
- * `PTR_POSTCONDITION` should be used at the end of a function to make assertions about
- * the resulting state. In debug mode, it is functionally equivalent to `PTR_GUARD_RESULT(result)`.
- * In production builds, it becomes a no-op. This can also be altered by a testing environment
- * to provide additional guarantees.
- */
- #define PTR_POSTCONDITION(result) PTR_GUARD_RESULT(__S2N_ENSURE_POSTCONDITION((result)))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Performs a safer memcpy.
- *
- * The following checks are performed:
- *
- * * `destination` is non-null
- * * `source` is non-null
- *
- * Callers will still need to ensure the following:
- *
- * * The size of the data pointed to by both the `destination` and `source` parameters,
- * shall be at least `len` bytes.
- */
- #define PTR_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), PTR_ENSURE_REF)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Performs a safer memset
- *
- * The following checks are performed:
- *
- * * `destination` is non-null
- *
- * Callers will still need to ensure the following:
- *
- * * The size of the data pointed to by the `destination` parameter shall be at least
- * `len` bytes.
- */
- #define PTR_CHECKED_MEMSET(destination, value, len) __S2N_ENSURE_SAFE_MEMSET((destination), (value), (len), PTR_ENSURE_REF)
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `(result) != NULL`, otherwise the function will return `NULL`
- */
- #define PTR_GUARD(result) __S2N_ENSURE((result) != NULL, __S2N_ENSURE_CHECKED_RETURN(NULL))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `result == _OSSL_SUCCESS`, otherwise the function will `PTR_BAIL` with `error`
- */
- #define PTR_GUARD_OSSL(result, error) __S2N_ENSURE((result) == _OSSL_SUCCESS, PTR_BAIL(error))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `s2n_result_is_ok(result)`, otherwise the function will return `NULL`
- */
- #define PTR_GUARD_RESULT(result) __S2N_ENSURE(s2n_result_is_ok(result), __S2N_ENSURE_CHECKED_RETURN(NULL))
- /**
- * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
- *
- * Ensures `(result) > S2N_FAILURE`, otherwise the function will return `NULL`
- */
- #define PTR_GUARD_POSIX(result) __S2N_ENSURE((result) > S2N_FAILURE, __S2N_ENSURE_CHECKED_RETURN(NULL))
|