s2n_result.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include <stdbool.h>
  17. #include "api/s2n.h"
  18. /* A value which indicates the outcome of a function */
  19. typedef struct {
  20. int __error_signal;
  21. } s2n_result;
  22. /* used to signal a successful function return */
  23. #define S2N_RESULT_OK ((s2n_result){ S2N_SUCCESS })
  24. /* used to signal an error while executing a function */
  25. #define S2N_RESULT_ERROR ((s2n_result){ S2N_FAILURE })
  26. #if defined(__clang__) || defined(__GNUC__)
  27. #define S2N_RESULT_MUST_USE __attribute__((warn_unused_result))
  28. #else
  29. #define S2N_RESULT_MUST_USE
  30. #endif
  31. /* returns true when the result is S2N_RESULT_OK */
  32. S2N_RESULT_MUST_USE bool s2n_result_is_ok(s2n_result result);
  33. /* returns true when the result is S2N_RESULT_ERROR */
  34. S2N_RESULT_MUST_USE bool s2n_result_is_error(s2n_result result);
  35. /**
  36. * Ignores the returned result of a function
  37. *
  38. * Generally, function results should always be checked. Using this function
  39. * could cause the system to behave in unexpected ways. As such, this function
  40. * should only be used in scenarios where the system state is not affected by
  41. * errors.
  42. */
  43. void s2n_result_ignore(s2n_result result);
  44. /* used in function declarations to signal function fallibility */
  45. #define S2N_RESULT S2N_RESULT_MUST_USE s2n_result
  46. /* The DEFER_CLEANUP macro discards the result of its cleanup function.
  47. * We need a version of s2n_result which can be ignored.
  48. */
  49. #define S2N_CLEANUP_RESULT s2n_result