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

n-api: Add optional length to functions accepting const char * #15108

Closed
digitalinfinity opened this issue Aug 31, 2017 · 5 comments
Closed

n-api: Add optional length to functions accepting const char * #15108

digitalinfinity opened this issue Aug 31, 2017 · 5 comments
Labels
feature request Issues that request new features to be added to Node.js. node-api Issues and PRs related to the Node-API.

Comments

@digitalinfinity
Copy link
Contributor

  • Version: 8
  • Platform: all
  • Subsystem: n-api

Migrating nodejs/abi-stable-node#254 opened by @RReverser to this repo for better visibility:

As discussed in the last meeting, zero-terminated const char*, while popular in C land, might be problematic for consumers in other languages having different string representation (for example, Rust with non-zero-terminated string slices represented as data + length).

Some N-API functions already accept optional length that, when set to -1, would mean a length calculated from zero-terminated string and explicit byte length otherwise.

It's worth to go through a list of functions that accept const char* and add length parameter where it's missing, especially for functions that don't provide variants with napi_value as a string, namely:

  • napi_create_function
  • napi_define_class
  • napi_module_register -> napi_module struct
@digitalinfinity digitalinfinity changed the title Add optional length to functions accepting const char * n-api: Add optional length to functions accepting const char * Aug 31, 2017
@mscdex mscdex added node-api Issues and PRs related to the Node-API. feature request Issues that request new features to be added to Node.js. labels Aug 31, 2017
@jasongin
Copy link
Member

jasongin commented Sep 5, 2017

The following APIs would be affected by this change:

These APIs would (presumably) not be changed, because they have functionally equivalent alternatives that take napi_value:

  • napi_get_named_property() -> napi_get_property()
  • napi_set_named_property() -> napi_set_property()
  • napi_has_named_property() -> napi_has_property()
  • napi_throw_error() -> napi_create_error() + napi_throw()
  • napi_throw_type_error() -> napi_create_type_error() + napi_throw()
  • napi_throw_range_error() -> napi_create_range_error() + napi_throw()

The char* parameters in these APIs already have corresponding size_t length parameters:

  • napi_create_string_latin1()
  • napi_create_string_utf8()
  • napi_create_string_utf16()
  • napi_get_value_string_latin1()
  • napi_get_value_string_utf8()
  • napi_get_value_string_utf16()

Also, the napi_extended_error_info struct returned by napi_get_last_error_info() has a char* member, but since it is only consumed by the caller (never provided) I assume it would be easy enough to determine the length to the null-terminator using the equivalent of strlen().

@sampsongao
Copy link

#15343
Not sure what should I do in the napi_fatal_error. node::FatalError doesn't have alternative of accepting string length. Should I create a new zero-terminated string before pass them to node::FatalError?

@mhdawson
Copy link
Member

Discussed with @sampsongao will likely just use stack allocated string to add null terminator if needed.

@digitalinfinity
Copy link
Contributor Author

@mhdawson this can be closed now that #15343 has landed? Or are we waiting on #15508 too?

@bnoordhuis
Copy link
Member

Both PRs have landed so it's looks like this can be closed out now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. node-api Issues and PRs related to the Node-API.
Projects
None yet
Development

No branches or pull requests

6 participants