Skip to content

Commit b8df7ec

Browse files
committed
fix: use std::array instead of C-arrays
1 parent f1fb6b2 commit b8df7ec

File tree

5 files changed

+22
-13
lines changed

5 files changed

+22
-13
lines changed

src/module.cc

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11

2+
#include <array>
3+
24
#include "./module.h"
35

46
#include "./context.h"
@@ -58,16 +60,19 @@ Napi::Object Capabilities(const Napi::Env& env) {
5860
}
5961

6062
Napi::Value CurveKeyPair(const Napi::CallbackInfo& info) {
61-
char public_key[41];
62-
char secret_key[41];
63-
if (zmq_curve_keypair(public_key, secret_key) < 0) {
63+
static constexpr auto max_key_length = 41;
64+
65+
std::array<char, max_key_length> public_key{};
66+
std::array<char, max_key_length> secret_key{};
67+
68+
if (zmq_curve_keypair(public_key.data(), secret_key.data()) < 0) {
6469
ErrnoException(info.Env(), zmq_errno()).ThrowAsJavaScriptException();
6570
return info.Env().Undefined();
6671
}
6772

6873
auto result = Napi::Object::New(info.Env());
69-
result["publicKey"] = Napi::String::New(info.Env(), public_key);
70-
result["secretKey"] = Napi::String::New(info.Env(), secret_key);
74+
result["publicKey"] = Napi::String::New(info.Env(), public_key.data());
75+
result["secretKey"] = Napi::String::New(info.Env(), secret_key.data());
7176
return result;
7277
}
7378

src/poller.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ class Poller {
2424
ZMQ style edge-triggered, with READABLE state indicating that ANY
2525
event may be present on the corresponding ZMQ socket. */
2626
int32_t Initialize(
27-
Napi::Env env, uv_os_sock_t& fd, std::function<void()> finalizer = nullptr) {
27+
Napi::Env env, uv_os_sock_t& file_descriptor, std::function<void()> finalizer = nullptr) {
2828
auto* loop = UvLoop(env);
2929

3030
poll->data = this;
31-
if (auto err = uv_poll_init_socket(loop, poll.get(), fd); err != 0) {
31+
if (auto err = uv_poll_init_socket(loop, poll.get(), file_descriptor); err != 0) {
3232
return err;
3333
}
3434

src/socket.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <cstdint>
55
#include <limits>
66
#include <unordered_set>
7+
#include <array>
78

89
#include "./context.h"
910
#include "./incoming_msg.h"
@@ -778,18 +779,20 @@ Napi::Value Socket::GetSockOpt<char*>(const Napi::CallbackInfo& info) {
778779

779780
auto const option = info[0].As<Napi::Number>();
780781

781-
char value[1024];
782-
size_t length = sizeof(value) - 1;
783-
if (zmq_getsockopt(socket, option, value, &length) < 0) {
782+
static constexpr auto max_value_length = 1024; //+
783+
auto value = std::array<char, max_value_length>(); //+
784+
size_t length = value.size(); //+
785+
if (zmq_getsockopt(socket, option, value.data(), &length) < 0) { //+
784786
ErrnoException(Env(), zmq_errno()).ThrowAsJavaScriptException();
785787
return Env().Undefined();
786788
}
787789

788790
if (length == 0 || (length == 1 && value[0] == 0)) {
789791
return Env().Null();
790792
}
793+
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
791794
value[length] = '\0';
792-
return Napi::String::New(Env(), value);
795+
return Napi::String::New(Env(), value.data());
793796
}
794797

795798
template <>

src/util/arguments.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct Not {
2828

2929
template <typename... F>
3030
class Verify {
31-
const std::string_view msg;
31+
/* const */ std::string_view msg;
3232

3333
public:
3434
constexpr explicit Verify(std::string_view msg) noexcept : msg(msg) {}

src/util/electron_helper.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ inline bool hasElectronMemoryCage(const Napi::Env& env) {
3636
.ToString()
3737
.Utf8Value();
3838
int const majorVer = stoi(first_component(electronVers));
39-
if (majorVer >= 21) {
39+
static constexpr auto electron_memory_cage_version = 21;
40+
if (majorVer >= electron_memory_cage_version) {
4041
hasElectronMemoryCageCache = true;
4142
}
4243
}

0 commit comments

Comments
 (0)