s2n_safety_macros.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. #pragma once
  16. /**
  17. * DO NOT DIRECTLY MODIFY THIS FILE:
  18. *
  19. * The code in this file is generated from scripts/s2n_safety_macros.py and any modifications
  20. * should be in there.
  21. */
  22. /* clang-format off */
  23. #include "error/s2n_errno.h"
  24. #include "utils/s2n_ensure.h"
  25. #include "utils/s2n_result.h"
  26. /**
  27. * The goal of s2n_safety is to provide helpers to perform common
  28. * checks, which help with code readability.
  29. */
  30. /* Success signal value for OpenSSL functions */
  31. #define _OSSL_SUCCESS 1
  32. /**
  33. * Sets the global `s2n_errno` to `error` and returns with an `S2N_RESULT_ERROR`
  34. */
  35. #define RESULT_BAIL(error) do { _S2N_ERROR((error)); __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR); } while (0)
  36. /**
  37. * Ensures the `condition` is `true`, otherwise the function will `RESULT_BAIL` with `error`
  38. */
  39. #define RESULT_ENSURE(condition, error) __S2N_ENSURE((condition), RESULT_BAIL(error))
  40. /**
  41. * Ensures the `condition` is `true`, otherwise the function will `RESULT_BAIL` with `error`
  42. *
  43. * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
  44. * In release mode, the check is removed.
  45. */
  46. #define RESULT_DEBUG_ENSURE(condition, error) __S2N_ENSURE_DEBUG((condition), RESULT_BAIL(error))
  47. /**
  48. * Ensures `s2n_result_is_ok(result)`, otherwise the function will `RESULT_BAIL` with `error`
  49. *
  50. * This can be useful for overriding the global `s2n_errno`
  51. */
  52. #define RESULT_ENSURE_OK(result, error) __S2N_ENSURE(s2n_result_is_ok(result), RESULT_BAIL(error))
  53. /**
  54. * Ensures `a` is greater than or equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
  55. */
  56. #define RESULT_ENSURE_GTE(a, b) __S2N_ENSURE((a) >= (b), RESULT_BAIL(S2N_ERR_SAFETY))
  57. /**
  58. * Ensures `a` is less than or equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
  59. */
  60. #define RESULT_ENSURE_LTE(a, b) __S2N_ENSURE((a) <= (b), RESULT_BAIL(S2N_ERR_SAFETY))
  61. /**
  62. * Ensures `a` is greater than `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
  63. */
  64. #define RESULT_ENSURE_GT(a, b) __S2N_ENSURE((a) > (b), RESULT_BAIL(S2N_ERR_SAFETY))
  65. /**
  66. * Ensures `a` is less than `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
  67. */
  68. #define RESULT_ENSURE_LT(a, b) __S2N_ENSURE((a) < (b), RESULT_BAIL(S2N_ERR_SAFETY))
  69. /**
  70. * Ensures `a` is equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
  71. */
  72. #define RESULT_ENSURE_EQ(a, b) __S2N_ENSURE((a) == (b), RESULT_BAIL(S2N_ERR_SAFETY))
  73. /**
  74. * Ensures `a` is not equal to `b`, otherwise the function will `RESULT_BAIL` with a `S2N_ERR_SAFETY` error
  75. */
  76. #define RESULT_ENSURE_NE(a, b) __S2N_ENSURE((a) != (b), RESULT_BAIL(S2N_ERR_SAFETY))
  77. /**
  78. * Ensures `min <= n <= max`, otherwise the function will `RESULT_BAIL` with `S2N_ERR_SAFETY`
  79. */
  80. #define RESULT_ENSURE_INCLUSIVE_RANGE(min, n, max) \
  81. do { \
  82. __typeof(n) __tmp_n = ( n ); \
  83. __typeof(n) __tmp_min = ( min ); \
  84. __typeof(n) __tmp_max = ( max ); \
  85. RESULT_ENSURE_GTE(__tmp_n, __tmp_min); \
  86. RESULT_ENSURE_LTE(__tmp_n, __tmp_max); \
  87. } while(0)
  88. /**
  89. * Ensures `min < n < max`, otherwise the function will `RESULT_BAIL` with `S2N_ERR_SAFETY`
  90. */
  91. #define RESULT_ENSURE_EXCLUSIVE_RANGE(min, n, max) \
  92. do { \
  93. __typeof(n) __tmp_n = ( n ); \
  94. __typeof(n) __tmp_min = ( min ); \
  95. __typeof(n) __tmp_max = ( max ); \
  96. RESULT_ENSURE_GT(__tmp_n, __tmp_min); \
  97. RESULT_ENSURE_LT(__tmp_n, __tmp_max); \
  98. } while(0)
  99. /**
  100. * Ensures `x` is a readable reference, otherwise the function will `RESULT_BAIL` with `S2N_ERR_NULL`
  101. */
  102. #define RESULT_ENSURE_REF(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_READABLE(x), RESULT_BAIL(S2N_ERR_NULL))
  103. /**
  104. * Ensures `x` is a mutable reference, otherwise the function will `RESULT_BAIL` with `S2N_ERR_NULL`
  105. */
  106. #define RESULT_ENSURE_MUT(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_WRITABLE(x), RESULT_BAIL(S2N_ERR_NULL))
  107. /**
  108. * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
  109. *
  110. * `RESULT_PRECONDITION` should be used at the beginning of a function to make assertions about
  111. * the provided arguments. By default, it is functionally equivalent to `RESULT_GUARD(result)`
  112. * but can be altered by a testing environment to provide additional guarantees.
  113. */
  114. #define RESULT_PRECONDITION(result) RESULT_GUARD(__S2N_ENSURE_PRECONDITION((result)))
  115. /**
  116. * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
  117. *
  118. * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
  119. * In release mode, the check is removed.
  120. *
  121. * `RESULT_POSTCONDITION` should be used at the end of a function to make assertions about
  122. * the resulting state. In debug mode, it is functionally equivalent to `RESULT_GUARD(result)`.
  123. * In production builds, it becomes a no-op. This can also be altered by a testing environment
  124. * to provide additional guarantees.
  125. */
  126. #define RESULT_POSTCONDITION(result) RESULT_GUARD(__S2N_ENSURE_POSTCONDITION((result)))
  127. /**
  128. * Performs a safer memcpy.
  129. *
  130. * The following checks are performed:
  131. *
  132. * * `destination` is non-null
  133. * * `source` is non-null
  134. *
  135. * Callers will still need to ensure the following:
  136. *
  137. * * The size of the data pointed to by both the `destination` and `source` parameters,
  138. * shall be at least `len` bytes.
  139. */
  140. #define RESULT_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), RESULT_ENSURE_REF)
  141. /**
  142. * Performs a safer memset
  143. *
  144. * The following checks are performed:
  145. *
  146. * * `destination` is non-null
  147. *
  148. * Callers will still need to ensure the following:
  149. *
  150. * * The size of the data pointed to by the `destination` parameter shall be at least
  151. * `len` bytes.
  152. */
  153. #define RESULT_CHECKED_MEMSET(destination, value, len) __S2N_ENSURE_SAFE_MEMSET((destination), (value), (len), RESULT_ENSURE_REF)
  154. /**
  155. * Ensures `s2n_result_is_ok(result)`, otherwise the function will return `S2N_RESULT_ERROR`
  156. */
  157. #define RESULT_GUARD(result) __S2N_ENSURE(s2n_result_is_ok(result), __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR))
  158. /**
  159. * Ensures `result == _OSSL_SUCCESS`, otherwise the function will `RESULT_BAIL` with `error`
  160. */
  161. #define RESULT_GUARD_OSSL(result, error) __S2N_ENSURE((result) == _OSSL_SUCCESS, RESULT_BAIL(error))
  162. /**
  163. * Ensures `(result) > S2N_FAILURE`, otherwise the function will return `S2N_RESULT_ERROR`
  164. */
  165. #define RESULT_GUARD_POSIX(result) __S2N_ENSURE((result) > S2N_FAILURE, __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR))
  166. /**
  167. * Ensures `(result) != NULL`, otherwise the function will return `S2N_RESULT_ERROR`
  168. *
  169. * Does not set s2n_errno to S2N_ERR_NULL, so is NOT a direct replacement for RESULT_ENSURE_REF.
  170. */
  171. #define RESULT_GUARD_PTR(result) __S2N_ENSURE((result) != NULL, __S2N_ENSURE_CHECKED_RETURN(S2N_RESULT_ERROR))
  172. /**
  173. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  174. *
  175. * Sets the global `s2n_errno` to `error` and returns with an `S2N_FAILURE`
  176. */
  177. #define POSIX_BAIL(error) do { _S2N_ERROR((error)); __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE); } while (0)
  178. /**
  179. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  180. *
  181. * Ensures the `condition` is `true`, otherwise the function will `POSIX_BAIL` with `error`
  182. */
  183. #define POSIX_ENSURE(condition, error) __S2N_ENSURE((condition), POSIX_BAIL(error))
  184. /**
  185. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  186. *
  187. * Ensures the `condition` is `true`, otherwise the function will `POSIX_BAIL` with `error`
  188. *
  189. * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
  190. * In release mode, the check is removed.
  191. */
  192. #define POSIX_DEBUG_ENSURE(condition, error) __S2N_ENSURE_DEBUG((condition), POSIX_BAIL(error))
  193. /**
  194. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  195. *
  196. * Ensures `(result) > S2N_FAILURE`, otherwise the function will `POSIX_BAIL` with `error`
  197. *
  198. * This can be useful for overriding the global `s2n_errno`
  199. */
  200. #define POSIX_ENSURE_OK(result, error) __S2N_ENSURE((result) > S2N_FAILURE, POSIX_BAIL(error))
  201. /**
  202. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  203. *
  204. * Ensures `a` is greater than or equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
  205. */
  206. #define POSIX_ENSURE_GTE(a, b) __S2N_ENSURE((a) >= (b), POSIX_BAIL(S2N_ERR_SAFETY))
  207. /**
  208. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  209. *
  210. * Ensures `a` is less than or equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
  211. */
  212. #define POSIX_ENSURE_LTE(a, b) __S2N_ENSURE((a) <= (b), POSIX_BAIL(S2N_ERR_SAFETY))
  213. /**
  214. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  215. *
  216. * Ensures `a` is greater than `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
  217. */
  218. #define POSIX_ENSURE_GT(a, b) __S2N_ENSURE((a) > (b), POSIX_BAIL(S2N_ERR_SAFETY))
  219. /**
  220. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  221. *
  222. * Ensures `a` is less than `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
  223. */
  224. #define POSIX_ENSURE_LT(a, b) __S2N_ENSURE((a) < (b), POSIX_BAIL(S2N_ERR_SAFETY))
  225. /**
  226. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  227. *
  228. * Ensures `a` is equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
  229. */
  230. #define POSIX_ENSURE_EQ(a, b) __S2N_ENSURE((a) == (b), POSIX_BAIL(S2N_ERR_SAFETY))
  231. /**
  232. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  233. *
  234. * Ensures `a` is not equal to `b`, otherwise the function will `POSIX_BAIL` with a `S2N_ERR_SAFETY` error
  235. */
  236. #define POSIX_ENSURE_NE(a, b) __S2N_ENSURE((a) != (b), POSIX_BAIL(S2N_ERR_SAFETY))
  237. /**
  238. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  239. *
  240. * Ensures `min <= n <= max`, otherwise the function will `POSIX_BAIL` with `S2N_ERR_SAFETY`
  241. */
  242. #define POSIX_ENSURE_INCLUSIVE_RANGE(min, n, max) \
  243. do { \
  244. __typeof(n) __tmp_n = ( n ); \
  245. __typeof(n) __tmp_min = ( min ); \
  246. __typeof(n) __tmp_max = ( max ); \
  247. POSIX_ENSURE_GTE(__tmp_n, __tmp_min); \
  248. POSIX_ENSURE_LTE(__tmp_n, __tmp_max); \
  249. } while(0)
  250. /**
  251. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  252. *
  253. * Ensures `min < n < max`, otherwise the function will `POSIX_BAIL` with `S2N_ERR_SAFETY`
  254. */
  255. #define POSIX_ENSURE_EXCLUSIVE_RANGE(min, n, max) \
  256. do { \
  257. __typeof(n) __tmp_n = ( n ); \
  258. __typeof(n) __tmp_min = ( min ); \
  259. __typeof(n) __tmp_max = ( max ); \
  260. POSIX_ENSURE_GT(__tmp_n, __tmp_min); \
  261. POSIX_ENSURE_LT(__tmp_n, __tmp_max); \
  262. } while(0)
  263. /**
  264. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  265. *
  266. * Ensures `x` is a readable reference, otherwise the function will `POSIX_BAIL` with `S2N_ERR_NULL`
  267. */
  268. #define POSIX_ENSURE_REF(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_READABLE(x), POSIX_BAIL(S2N_ERR_NULL))
  269. /**
  270. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  271. *
  272. * Ensures `x` is a mutable reference, otherwise the function will `POSIX_BAIL` with `S2N_ERR_NULL`
  273. */
  274. #define POSIX_ENSURE_MUT(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_WRITABLE(x), POSIX_BAIL(S2N_ERR_NULL))
  275. /**
  276. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  277. *
  278. * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
  279. *
  280. * `POSIX_PRECONDITION` should be used at the beginning of a function to make assertions about
  281. * the provided arguments. By default, it is functionally equivalent to `POSIX_GUARD_RESULT(result)`
  282. * but can be altered by a testing environment to provide additional guarantees.
  283. */
  284. #define POSIX_PRECONDITION(result) POSIX_GUARD_RESULT(__S2N_ENSURE_PRECONDITION((result)))
  285. /**
  286. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  287. *
  288. * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
  289. *
  290. * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
  291. * In release mode, the check is removed.
  292. *
  293. * `POSIX_POSTCONDITION` should be used at the end of a function to make assertions about
  294. * the resulting state. In debug mode, it is functionally equivalent to `POSIX_GUARD_RESULT(result)`.
  295. * In production builds, it becomes a no-op. This can also be altered by a testing environment
  296. * to provide additional guarantees.
  297. */
  298. #define POSIX_POSTCONDITION(result) POSIX_GUARD_RESULT(__S2N_ENSURE_POSTCONDITION((result)))
  299. /**
  300. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  301. *
  302. * Performs a safer memcpy.
  303. *
  304. * The following checks are performed:
  305. *
  306. * * `destination` is non-null
  307. * * `source` is non-null
  308. *
  309. * Callers will still need to ensure the following:
  310. *
  311. * * The size of the data pointed to by both the `destination` and `source` parameters,
  312. * shall be at least `len` bytes.
  313. */
  314. #define POSIX_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), POSIX_ENSURE_REF)
  315. /**
  316. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  317. *
  318. * Performs a safer memset
  319. *
  320. * The following checks are performed:
  321. *
  322. * * `destination` is non-null
  323. *
  324. * Callers will still need to ensure the following:
  325. *
  326. * * The size of the data pointed to by the `destination` parameter shall be at least
  327. * `len` bytes.
  328. */
  329. #define POSIX_CHECKED_MEMSET(destination, value, len) __S2N_ENSURE_SAFE_MEMSET((destination), (value), (len), POSIX_ENSURE_REF)
  330. /**
  331. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  332. *
  333. * Ensures `(result) > S2N_FAILURE`, otherwise the function will return `S2N_FAILURE`
  334. */
  335. #define POSIX_GUARD(result) __S2N_ENSURE((result) > S2N_FAILURE, __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE))
  336. /**
  337. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  338. *
  339. * Ensures `result == _OSSL_SUCCESS`, otherwise the function will `POSIX_BAIL` with `error`
  340. */
  341. #define POSIX_GUARD_OSSL(result, error) __S2N_ENSURE((result) == _OSSL_SUCCESS, POSIX_BAIL(error))
  342. /**
  343. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  344. *
  345. * Ensures `s2n_result_is_ok(result)`, otherwise the function will return `S2N_FAILURE`
  346. */
  347. #define POSIX_GUARD_RESULT(result) __S2N_ENSURE(s2n_result_is_ok(result), __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE))
  348. /**
  349. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  350. *
  351. * Ensures `(result) != NULL`, otherwise the function will return `S2N_FAILURE`
  352. *
  353. * Does not set s2n_errno to S2N_ERR_NULL, so is NOT a direct replacement for POSIX_ENSURE_REF.
  354. */
  355. #define POSIX_GUARD_PTR(result) __S2N_ENSURE((result) != NULL, __S2N_ENSURE_CHECKED_RETURN(S2N_FAILURE))
  356. /**
  357. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  358. *
  359. * Sets the global `s2n_errno` to `error` and returns with an `NULL`
  360. */
  361. #define PTR_BAIL(error) do { _S2N_ERROR((error)); __S2N_ENSURE_CHECKED_RETURN(NULL); } while (0)
  362. /**
  363. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  364. *
  365. * Ensures the `condition` is `true`, otherwise the function will `PTR_BAIL` with `error`
  366. */
  367. #define PTR_ENSURE(condition, error) __S2N_ENSURE((condition), PTR_BAIL(error))
  368. /**
  369. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  370. *
  371. * Ensures the `condition` is `true`, otherwise the function will `PTR_BAIL` with `error`
  372. *
  373. * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
  374. * In release mode, the check is removed.
  375. */
  376. #define PTR_DEBUG_ENSURE(condition, error) __S2N_ENSURE_DEBUG((condition), PTR_BAIL(error))
  377. /**
  378. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  379. *
  380. * Ensures `(result) != NULL`, otherwise the function will `PTR_BAIL` with `error`
  381. *
  382. * This can be useful for overriding the global `s2n_errno`
  383. */
  384. #define PTR_ENSURE_OK(result, error) __S2N_ENSURE((result) != NULL, PTR_BAIL(error))
  385. /**
  386. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  387. *
  388. * Ensures `a` is greater than or equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
  389. */
  390. #define PTR_ENSURE_GTE(a, b) __S2N_ENSURE((a) >= (b), PTR_BAIL(S2N_ERR_SAFETY))
  391. /**
  392. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  393. *
  394. * Ensures `a` is less than or equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
  395. */
  396. #define PTR_ENSURE_LTE(a, b) __S2N_ENSURE((a) <= (b), PTR_BAIL(S2N_ERR_SAFETY))
  397. /**
  398. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  399. *
  400. * Ensures `a` is greater than `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
  401. */
  402. #define PTR_ENSURE_GT(a, b) __S2N_ENSURE((a) > (b), PTR_BAIL(S2N_ERR_SAFETY))
  403. /**
  404. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  405. *
  406. * Ensures `a` is less than `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
  407. */
  408. #define PTR_ENSURE_LT(a, b) __S2N_ENSURE((a) < (b), PTR_BAIL(S2N_ERR_SAFETY))
  409. /**
  410. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  411. *
  412. * Ensures `a` is equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
  413. */
  414. #define PTR_ENSURE_EQ(a, b) __S2N_ENSURE((a) == (b), PTR_BAIL(S2N_ERR_SAFETY))
  415. /**
  416. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  417. *
  418. * Ensures `a` is not equal to `b`, otherwise the function will `PTR_BAIL` with a `S2N_ERR_SAFETY` error
  419. */
  420. #define PTR_ENSURE_NE(a, b) __S2N_ENSURE((a) != (b), PTR_BAIL(S2N_ERR_SAFETY))
  421. /**
  422. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  423. *
  424. * Ensures `min <= n <= max`, otherwise the function will `PTR_BAIL` with `S2N_ERR_SAFETY`
  425. */
  426. #define PTR_ENSURE_INCLUSIVE_RANGE(min, n, max) \
  427. do { \
  428. __typeof(n) __tmp_n = ( n ); \
  429. __typeof(n) __tmp_min = ( min ); \
  430. __typeof(n) __tmp_max = ( max ); \
  431. PTR_ENSURE_GTE(__tmp_n, __tmp_min); \
  432. PTR_ENSURE_LTE(__tmp_n, __tmp_max); \
  433. } while(0)
  434. /**
  435. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  436. *
  437. * Ensures `min < n < max`, otherwise the function will `PTR_BAIL` with `S2N_ERR_SAFETY`
  438. */
  439. #define PTR_ENSURE_EXCLUSIVE_RANGE(min, n, max) \
  440. do { \
  441. __typeof(n) __tmp_n = ( n ); \
  442. __typeof(n) __tmp_min = ( min ); \
  443. __typeof(n) __tmp_max = ( max ); \
  444. PTR_ENSURE_GT(__tmp_n, __tmp_min); \
  445. PTR_ENSURE_LT(__tmp_n, __tmp_max); \
  446. } while(0)
  447. /**
  448. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  449. *
  450. * Ensures `x` is a readable reference, otherwise the function will `PTR_BAIL` with `S2N_ERR_NULL`
  451. */
  452. #define PTR_ENSURE_REF(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_READABLE(x), PTR_BAIL(S2N_ERR_NULL))
  453. /**
  454. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  455. *
  456. * Ensures `x` is a mutable reference, otherwise the function will `PTR_BAIL` with `S2N_ERR_NULL`
  457. */
  458. #define PTR_ENSURE_MUT(x) __S2N_ENSURE(S2N_OBJECT_PTR_IS_WRITABLE(x), PTR_BAIL(S2N_ERR_NULL))
  459. /**
  460. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  461. *
  462. * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
  463. *
  464. * `PTR_PRECONDITION` should be used at the beginning of a function to make assertions about
  465. * the provided arguments. By default, it is functionally equivalent to `PTR_GUARD_RESULT(result)`
  466. * but can be altered by a testing environment to provide additional guarantees.
  467. */
  468. #define PTR_PRECONDITION(result) PTR_GUARD_RESULT(__S2N_ENSURE_PRECONDITION((result)))
  469. /**
  470. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  471. *
  472. * Ensures the `result` is `S2N_RESULT_OK`, otherwise the function will return an error signal
  473. *
  474. * NOTE: The condition will _only_ be checked when the code is compiled in debug mode.
  475. * In release mode, the check is removed.
  476. *
  477. * `PTR_POSTCONDITION` should be used at the end of a function to make assertions about
  478. * the resulting state. In debug mode, it is functionally equivalent to `PTR_GUARD_RESULT(result)`.
  479. * In production builds, it becomes a no-op. This can also be altered by a testing environment
  480. * to provide additional guarantees.
  481. */
  482. #define PTR_POSTCONDITION(result) PTR_GUARD_RESULT(__S2N_ENSURE_POSTCONDITION((result)))
  483. /**
  484. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  485. *
  486. * Performs a safer memcpy.
  487. *
  488. * The following checks are performed:
  489. *
  490. * * `destination` is non-null
  491. * * `source` is non-null
  492. *
  493. * Callers will still need to ensure the following:
  494. *
  495. * * The size of the data pointed to by both the `destination` and `source` parameters,
  496. * shall be at least `len` bytes.
  497. */
  498. #define PTR_CHECKED_MEMCPY(destination, source, len) __S2N_ENSURE_SAFE_MEMCPY((destination), (source), (len), PTR_ENSURE_REF)
  499. /**
  500. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  501. *
  502. * Performs a safer memset
  503. *
  504. * The following checks are performed:
  505. *
  506. * * `destination` is non-null
  507. *
  508. * Callers will still need to ensure the following:
  509. *
  510. * * The size of the data pointed to by the `destination` parameter shall be at least
  511. * `len` bytes.
  512. */
  513. #define PTR_CHECKED_MEMSET(destination, value, len) __S2N_ENSURE_SAFE_MEMSET((destination), (value), (len), PTR_ENSURE_REF)
  514. /**
  515. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  516. *
  517. * Ensures `(result) != NULL`, otherwise the function will return `NULL`
  518. */
  519. #define PTR_GUARD(result) __S2N_ENSURE((result) != NULL, __S2N_ENSURE_CHECKED_RETURN(NULL))
  520. /**
  521. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  522. *
  523. * Ensures `result == _OSSL_SUCCESS`, otherwise the function will `PTR_BAIL` with `error`
  524. */
  525. #define PTR_GUARD_OSSL(result, error) __S2N_ENSURE((result) == _OSSL_SUCCESS, PTR_BAIL(error))
  526. /**
  527. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  528. *
  529. * Ensures `s2n_result_is_ok(result)`, otherwise the function will return `NULL`
  530. */
  531. #define PTR_GUARD_RESULT(result) __S2N_ENSURE(s2n_result_is_ok(result), __S2N_ENSURE_CHECKED_RETURN(NULL))
  532. /**
  533. * DEPRECATED: all methods (except those in s2n.h) should return s2n_result.
  534. *
  535. * Ensures `(result) > S2N_FAILURE`, otherwise the function will return `NULL`
  536. */
  537. #define PTR_GUARD_POSIX(result) __S2N_ENSURE((result) > S2N_FAILURE, __S2N_ENSURE_CHECKED_RETURN(NULL))