Open
Description
Problem:
Right now, most of the codebase uses int
as the function return value. As documented in s2n_result.c, this has a few problems:
GUARD
ing in a function that returns integer typesGUARD
ing a function that returns integer type- Forgetting to
GUARD
a function that returned an error signal
Solution:
The majority of the codebase should return S2N_RESULT
. This is statically checked to ensure:
-
The code cannot
GUARD
in a function that returns integer types:uint8_t s2n_answer_to_the_ultimate_question() { GUARD(s2n_sleep_for_years(7500000)); /* <- Won't compile since this function doesn't return an S2N_RESULT */ return 42; }
-
The code cannot
GUARD
a function that returns integer types:S2N_RESULT s2n_deep_thought() { GUARD(s2n_answer_to_the_ultimate_question()); /* <- Won't compile since the function being called doesn't return an S2N_RESULT */ return S2N_RESULT_OK; }
-
The code cannot ignore the return value of a function
uint8_t s2n_answer_to_the_ultimate_question() { s2n_sleep_for_years(7500000); /* <- Won't compile since the function being called returns a `S2N_RESULT` isn't `GUARD`ed */ return 42; }
Requirements / Acceptance Criteria:
The following tasks are implemented in a way that will make the transition as painless as possible, especially for any pending PRs.
- Implement S2N_RESULT utils: add s2n_result implementation #1872
- Add a codegen script to consistently generate safety macros for all function contexts utils: add safety_macros codegen script #2423
- Add a codemod script to migrate all of the existing code to use the new naming convention utils: add safety codemod script #2339
- Implement a GitHub Action to ensure the s2n_safety_macros.h is only modified through the codegen script. (see utils: add safety_macros codegen script #2423 (comment))
- Add a
S2N_RESULT_FREE
return type that is allowed to be used in theDEFER_CLEANUP
macro. Fix and simplify psk_param lifecycle #2523 - Apply the codemod script to main and existing PRs utils: apply safety macro codemod script #2441
- Remove the old safety macros, since the codemod has been applied and none of the code is using the old macros anymore. utils: remove deprecated safety macros #2747
- Update the safety macros codegen script to add prefix-less set of macros by duplicating all of the
RESULT_
declarations. This means thatRESULT_GUARD
is now justGUARD
. - Add a codemod script to move all of the
RESULT_
invocations in the codebase, since it's now the default. - Manually update all of the functions in the codebase to return
S2N_RESULT
instead ofint
(see utils: continued result migration #1891 Add S2N_RESULT to a couple functions. #2371)
This can be done per module and replacing all of theint
withS2N_RESULT
and trying to compile it. After working through all of the compiler issues, that module should continue to have the same functionality with the added guarantees thatS2N_RESULT
provides. - Improve comparison macros to ensure values have the same type before comparing