node-addon-api icon indicating copy to clipboard operation
node-addon-api copied to clipboard

GCC warning with -Wconversion and undefined behavior for unknown_array_type

Open zbjornson opened this issue 2 years ago • 6 comments

This line: https://github.com/nodejs/node-addon-api/blob/da2e754a021e418a3ca36f3c10ea134c9541959f/napi.h#L1075 causes a warning with -Wconversion enabled:

 warning: the result of the conversion is unspecified because ‘-1’ is outside the range of type ‘napi_typedarray_type’ [-Wconversion]
  911 |     static const napi_typedarray_type unknown_array_type = static_cast<napi_typedarray_type>(-1);
      |

I don't think this is really an issue, but it would be nice to get rid of the warning.

(https://wiki.sei.cmu.edu/confluence/display/cplusplus/INT50-CPP.+Do+not+cast+to+an+out-of-range+enumeration+value)

zbjornson avatar Sep 07 '21 17:09 zbjornson

@zbjornson would you like to submit a suggested PR? I'd have to think more about whether changing the value would be a problem or not but I assume there are other ways to suppress the warking as well.

mhdawson avatar Sep 08 '21 18:09 mhdawson

Sure. I'll take a look at better solutions, but #pragma GCC diagnostics ignored "-Wconversion" would minimally work. The C++ spec defines this enum to have an implicit size of int, which has at least 16 bits, and the enum has 11 members currently, which is why I said it's probably not an issue. However, since C++17 this line is actually undefined behavior, not just an unspecified value.

node-addon-api could maybe have its own enum that has the same values as napi_typedarray_type plus an unknown type. I think that would be unobtrusive.

zbjornson avatar Sep 08 '21 19:09 zbjornson

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

github-actions[bot] avatar Dec 08 '21 00:12 github-actions[bot]

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

github-actions[bot] avatar Apr 11 '22 00:04 github-actions[bot]

@zbjornson were you still planning to submit a PR on this?

mhdawson avatar Apr 12 '22 18:04 mhdawson

This issue is stale because it has been open many days with no activity. It will be closed soon unless the stale label is removed or a comment is made.

github-actions[bot] avatar Jul 12 '22 00:07 github-actions[bot]

I'm closing the issue because it has been solved with PR https://github.com/nodejs/node-addon-api/pull/1209

NickNaso avatar Nov 03 '22 09:11 NickNaso