Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make globalcontext_make_atom safe #1443

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added `net:gethostname/0` on platforms with gethostname(3).
- Added `socket:getopt/2`

### Changed
- `globalcontext_make_atom` might return an invalid term when memory allocation fails. Therefore
return value must be checked (e.g. using `term_is_invalid_term()`)

### Fixed
- ESP32: improved sntp sync speed from a cold boot.

Expand Down
6 changes: 5 additions & 1 deletion src/libAtomVM/globalcontext.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,11 +412,15 @@ static inline bool globalcontext_is_term_equal_to_atom_string(GlobalContext *glo
* global context.
* @param glb pointer to the global context
* @param string an AtomString
* @return an atom term formed from the supplied atom string.
* @return an atom term formed from the supplied atom string, or an invalid term when out of memory
*/
static inline term globalcontext_make_atom(GlobalContext *glb, AtomString string)
{
int global_atom_index = globalcontext_insert_atom(glb, string);
if (UNLIKELY(global_atom_index < 0)) {
return term_invalid_term();
}

return term_from_atom_index(global_atom_index);
}

Expand Down
6 changes: 6 additions & 0 deletions src/libAtomVM/nifs.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ extern "C" {
ctx->x[1] = (error_type_atom); \
return term_invalid_term();

#define REQUIRE_ATOM(name, lenstr, str, glb) \
term name = globalcontext_make_atom(glb, lenstr str); \
if (UNLIKELY(term_is_invalid_term(name))) { \
RAISE_ERROR(OUT_OF_MEMORY_ATOM); \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is perfect for nifs, but if this is used in a port is raising the error what we really want? Maybe we need a separate macro for use in ports that would just return the out_of_memory atom? Ideally it would be {error, out_of_memory} (for ports), but in this case it would be unlikely there was enough ram to construct the error tuple.

}

const struct Nif *nifs_get(AtomString module, AtomString function, int arity);

#ifdef __cplusplus
Expand Down
Loading