Skip to content

napi: zero length typedarray in 12 vs 13 #32089

Closed
@mafintosh

Description

@mafintosh

We just hit this bug, which may be a feature, but at least not documented where in Node.js
13, the napi_get_typedarray_info returns NULL as the data pointer for 0 length typedarrays but Node.js 12 returns a pointer.

This C program shows the difference:

#include <node_api.h>
#include <stdio.h>

napi_value print (napi_env env, napi_callback_info info) {
  napi_value argv[1];
  size_t argc = 1;

  napi_get_cb_info(env, info, &argc, argv, NULL, NULL);

  // https://nodejs.org/dist/latest-v13.x/docs/api/n-api.html#n_api_napi_get_typedarray_info

  napi_typedarray_type type;
  napi_value array_buffer;
  size_t len;
  void *data;
  size_t offset;
  napi_get_typedarray_info(env, argv[0], &type, &len, &data, &array_buffer, &offset);

  printf("buffer is %zu\n", data);

  return NULL;
}

napi_value init_all (napi_env env, napi_value exports) {
  napi_value print_fn;
  napi_create_function(env, NULL, 0, print, NULL, &print_fn);
  napi_set_named_property(env, exports, "print", print_fn);
  return exports;
}

NAPI_MODULE(NODE_GYP_MODULE_NAME, init_all)

Compile it and run it with

require('./binding...').print(Buffer.alloc(0))

Node.js 12 returns a pointer and 13 NULL.

Metadata

Metadata

Assignees

No one assigned

    Labels

    node-apiIssues and PRs related to the Node-API.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions