Skip to content

Commit 1948149

Browse files
committed
src,crypto: adjust crypto_bio files for formatter/linter
Run clang-format on the files and provide needed additional comments for the linter. Refs: nodejs#42665 (comment)
1 parent be01185 commit 1948149

File tree

2 files changed

+38
-89
lines changed

2 files changed

+38
-89
lines changed

src/crypto/crypto_bio.cc

Lines changed: 27 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
#include "crypto/crypto_bio.h"
23+
#include "allocated_buffer-inl.h"
2324
#include "base_object-inl.h"
2425
#include "memory_tracker-inl.h"
25-
#include "allocated_buffer-inl.h"
2626
#include "util-inl.h"
2727

2828
#include <openssl/bio.h>
@@ -35,17 +35,14 @@ namespace crypto {
3535

3636
BIOPointer NodeBIO::New(Environment* env) {
3737
BIOPointer bio(BIO_new(GetMethod()));
38-
if (bio && env != nullptr)
39-
NodeBIO::FromBIO(bio.get())->env_ = env;
38+
if (bio && env != nullptr) NodeBIO::FromBIO(bio.get())->env_ = env;
4039
return bio;
4140
}
4241

43-
4442
BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
4543
BIOPointer bio = New(env);
4644

47-
if (!bio ||
48-
len > INT_MAX ||
45+
if (!bio || len > INT_MAX ||
4946
BIO_write(bio.get(), data, len) != static_cast<int>(len) ||
5047
BIO_set_mem_eof_return(bio.get(), 0) != 1) {
5148
return BIOPointer();
@@ -54,18 +51,15 @@ BIOPointer NodeBIO::NewFixed(const char* data, size_t len, Environment* env) {
5451
return bio;
5552
}
5653

57-
5854
int NodeBIO::New(BIO* bio) {
5955
BIO_set_data(bio, new NodeBIO());
6056
BIO_set_init(bio, 1);
6157

6258
return 1;
6359
}
6460

65-
6661
int NodeBIO::Free(BIO* bio) {
67-
if (bio == nullptr)
68-
return 0;
62+
if (bio == nullptr) return 0;
6963

7064
if (BIO_get_shutdown(bio)) {
7165
if (BIO_get_init(bio) && BIO_get_data(bio) != nullptr) {
@@ -77,7 +71,6 @@ int NodeBIO::Free(BIO* bio) {
7771
return 1;
7872
}
7973

80-
8174
int NodeBIO::Read(BIO* bio, char* out, int len) {
8275
BIO_clear_retry_flags(bio);
8376

@@ -94,13 +87,11 @@ int NodeBIO::Read(BIO* bio, char* out, int len) {
9487
return bytes;
9588
}
9689

97-
9890
char* NodeBIO::Peek(size_t* size) {
9991
*size = read_head_->write_pos_ - read_head_->read_pos_;
10092
return read_head_->data_ + read_head_->read_pos_;
10193
}
10294

103-
10495
size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
10596
Buffer* pos = read_head_;
10697
size_t max = *count;
@@ -127,7 +118,6 @@ size_t NodeBIO::PeekMultiple(char** out, size_t* size, size_t* count) {
127118
return total;
128119
}
129120

130-
131121
int NodeBIO::Write(BIO* bio, const char* data, int len) {
132122
BIO_clear_retry_flags(bio);
133123

@@ -136,27 +126,22 @@ int NodeBIO::Write(BIO* bio, const char* data, int len) {
136126
return len;
137127
}
138128

139-
140129
int NodeBIO::Puts(BIO* bio, const char* str) {
141130
return Write(bio, str, strlen(str));
142131
}
143132

144-
145133
int NodeBIO::Gets(BIO* bio, char* out, int size) {
146134
NodeBIO* nbio = FromBIO(bio);
147135

148-
if (nbio->Length() == 0)
149-
return 0;
136+
if (nbio->Length() == 0) return 0;
150137

151138
int i = nbio->IndexOf('\n', size);
152139

153140
// Include '\n', if it's there. If not, don't read off the end.
154-
if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length())
155-
i++;
141+
if (i < size && i >= 0 && static_cast<size_t>(i) < nbio->Length()) i++;
156142

157143
// Shift `i` a bit to nullptr-terminate string later
158-
if (size == i)
159-
i--;
144+
if (size == i) i--;
160145

161146
// Flush read data
162147
nbio->Read(out, i);
@@ -166,8 +151,9 @@ int NodeBIO::Gets(BIO* bio, char* out, int size) {
166151
return i;
167152
}
168153

169-
170-
long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
154+
long NodeBIO::Ctrl(BIO* bio, // NOLINT(runtime/int)
155+
int cmd,
156+
long num, // NOLINT(runtime/int)
171157
void* ptr) {
172158
NodeBIO* nbio;
173159
long ret; // NOLINT(runtime/int)
@@ -187,8 +173,7 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
187173
break;
188174
case BIO_CTRL_INFO:
189175
ret = nbio->Length();
190-
if (ptr != nullptr)
191-
*reinterpret_cast<void**>(ptr) = nullptr;
176+
if (ptr != nullptr) *reinterpret_cast<void**>(ptr) = nullptr;
192177
break;
193178
case BIO_C_SET_BUF_MEM:
194179
CHECK(0 && "Can't use SET_BUF_MEM_PTR with NodeBIO");
@@ -222,7 +207,6 @@ long NodeBIO::Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
222207
return ret;
223208
}
224209

225-
226210
const BIO_METHOD* NodeBIO::GetMethod() {
227211
// This is called from InitCryptoOnce() to avoid race conditions during
228212
// initialization.
@@ -242,7 +226,6 @@ const BIO_METHOD* NodeBIO::GetMethod() {
242226
return method;
243227
}
244228

245-
246229
void NodeBIO::TryMoveReadHead() {
247230
// `read_pos_` and `write_pos_` means the position of the reader and writer
248231
// inside the buffer, respectively. When they're equal - its safe to reset
@@ -256,12 +239,10 @@ void NodeBIO::TryMoveReadHead() {
256239

257240
// Move read_head_ forward, just in case if there're still some data to
258241
// read in the next buffer.
259-
if (read_head_ != write_head_)
260-
read_head_ = read_head_->next_;
242+
if (read_head_ != write_head_) read_head_ = read_head_->next_;
261243
}
262244
}
263245

264-
265246
size_t NodeBIO::Read(char* out, size_t size) {
266247
size_t bytes_read = 0;
267248
size_t expected = Length() > size ? size : Length();
@@ -271,8 +252,7 @@ size_t NodeBIO::Read(char* out, size_t size) {
271252
while (bytes_read < expected) {
272253
CHECK_LE(read_head_->read_pos_, read_head_->write_pos_);
273254
size_t avail = read_head_->write_pos_ - read_head_->read_pos_;
274-
if (avail > left)
275-
avail = left;
255+
if (avail > left) avail = left;
276256

277257
// Copy data
278258
if (out != nullptr)
@@ -295,16 +275,12 @@ size_t NodeBIO::Read(char* out, size_t size) {
295275
return bytes_read;
296276
}
297277

298-
299278
void NodeBIO::FreeEmpty() {
300-
if (write_head_ == nullptr)
301-
return;
279+
if (write_head_ == nullptr) return;
302280
Buffer* child = write_head_->next_;
303-
if (child == write_head_ || child == read_head_)
304-
return;
281+
if (child == write_head_ || child == read_head_) return;
305282
Buffer* cur = child->next_;
306-
if (cur == write_head_ || cur == read_head_)
307-
return;
283+
if (cur == write_head_ || cur == read_head_) return;
308284

309285
Buffer* prev = child;
310286
while (cur != read_head_) {
@@ -318,7 +294,6 @@ void NodeBIO::FreeEmpty() {
318294
prev->next_ = cur;
319295
}
320296

321-
322297
size_t NodeBIO::IndexOf(char delim, size_t limit) {
323298
size_t bytes_read = 0;
324299
size_t max = Length() > limit ? limit : Length();
@@ -328,8 +303,7 @@ size_t NodeBIO::IndexOf(char delim, size_t limit) {
328303
while (bytes_read < max) {
329304
CHECK_LE(current->read_pos_, current->write_pos_);
330305
size_t avail = current->write_pos_ - current->read_pos_;
331-
if (avail > left)
332-
avail = left;
306+
if (avail > left) avail = left;
333307

334308
// Walk through data
335309
char* tmp = current->data_ + current->read_pos_;
@@ -358,7 +332,6 @@ size_t NodeBIO::IndexOf(char delim, size_t limit) {
358332
return max;
359333
}
360334

361-
362335
void NodeBIO::Write(const char* data, size_t size) {
363336
size_t offset = 0;
364337
size_t left = size;
@@ -371,13 +344,11 @@ void NodeBIO::Write(const char* data, size_t size) {
371344
CHECK_LE(write_head_->write_pos_, write_head_->len_);
372345
size_t avail = write_head_->len_ - write_head_->write_pos_;
373346

374-
if (to_write > avail)
375-
to_write = avail;
347+
if (to_write > avail) to_write = avail;
376348

377349
// Copy data
378-
memcpy(write_head_->data_ + write_head_->write_pos_,
379-
data + offset,
380-
to_write);
350+
memcpy(
351+
write_head_->data_ + write_head_->write_pos_, data + offset, to_write);
381352

382353
// Move pointers
383354
left -= to_write;
@@ -400,18 +371,15 @@ void NodeBIO::Write(const char* data, size_t size) {
400371
CHECK_EQ(left, 0);
401372
}
402373

403-
404374
char* NodeBIO::PeekWritable(size_t* size) {
405375
TryAllocateForWrite(*size);
406376

407377
size_t available = write_head_->len_ - write_head_->write_pos_;
408-
if (*size == 0 || available <= *size)
409-
*size = available;
378+
if (*size == 0 || available <= *size) *size = available;
410379

411380
return write_head_->data_ + write_head_->write_pos_;
412381
}
413382

414-
415383
void NodeBIO::Commit(size_t size) {
416384
write_head_->write_pos_ += size;
417385
length_ += size;
@@ -429,18 +397,14 @@ void NodeBIO::Commit(size_t size) {
429397
}
430398
}
431399

432-
433400
void NodeBIO::TryAllocateForWrite(size_t hint) {
434401
Buffer* w = write_head_;
435402
Buffer* r = read_head_;
436403
// If write head is full, next buffer is either read head or not empty.
437-
if (w == nullptr ||
438-
(w->write_pos_ == w->len_ &&
439-
(w->next_ == r || w->next_->write_pos_ != 0))) {
440-
size_t len = w == nullptr ? initial_ :
441-
kThroughputBufferLength;
442-
if (len < hint)
443-
len = hint;
404+
if (w == nullptr || (w->write_pos_ == w->len_ &&
405+
(w->next_ == r || w->next_->write_pos_ != 0))) {
406+
size_t len = w == nullptr ? initial_ : kThroughputBufferLength;
407+
if (len < hint) len = hint;
444408

445409
// If there is a one time allocation size hint, use it.
446410
if (allocate_hint_ > len) {
@@ -461,10 +425,8 @@ void NodeBIO::TryAllocateForWrite(size_t hint) {
461425
}
462426
}
463427

464-
465428
void NodeBIO::Reset() {
466-
if (read_head_ == nullptr)
467-
return;
429+
if (read_head_ == nullptr) return;
468430

469431
while (read_head_->read_pos_ != read_head_->write_pos_) {
470432
CHECK(read_head_->write_pos_ > read_head_->read_pos_);
@@ -479,10 +441,8 @@ void NodeBIO::Reset() {
479441
CHECK_EQ(length_, 0);
480442
}
481443

482-
483444
NodeBIO::~NodeBIO() {
484-
if (read_head_ == nullptr)
485-
return;
445+
if (read_head_ == nullptr) return;
486446

487447
Buffer* current = read_head_;
488448
do {
@@ -495,12 +455,10 @@ NodeBIO::~NodeBIO() {
495455
write_head_ = nullptr;
496456
}
497457

498-
499458
NodeBIO* NodeBIO::FromBIO(BIO* bio) {
500459
CHECK_NOT_NULL(BIO_get_data(bio));
501460
return static_cast<NodeBIO*>(BIO_get_data(bio));
502461
}
503462

504-
505463
} // namespace crypto
506464
} // namespace node

src/crypto/crypto_bio.h

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class NodeBIO : public MemoryRetainer {
4747

4848
// NewFixed takes a copy of `len` bytes from `data` and returns a BIO that,
4949
// when read from, returns those bytes followed by EOF.
50-
static BIOPointer NewFixed(const char* data, size_t len,
50+
static BIOPointer NewFixed(const char* data,
51+
size_t len,
5152
Environment* env = nullptr);
5253

5354
// Move read head to next buffer if needed
@@ -89,11 +90,8 @@ class NodeBIO : public MemoryRetainer {
8990
// PeekWritable().
9091
void Commit(size_t size);
9192

92-
9393
// Return size of buffer in bytes
94-
inline size_t Length() const {
95-
return length_;
96-
}
94+
inline size_t Length() const { return length_; }
9795

9896
// Provide a hint about the size of the next pending set of writes. TLS
9997
// writes records of a maximum length of 16k of data plus a 5-byte header,
@@ -110,17 +108,11 @@ class NodeBIO : public MemoryRetainer {
110108
}
111109
}
112110

113-
inline void set_eof_return(int num) {
114-
eof_return_ = num;
115-
}
111+
inline void set_eof_return(int num) { eof_return_ = num; }
116112

117-
inline int eof_return() {
118-
return eof_return_;
119-
}
113+
inline int eof_return() { return eof_return_; }
120114

121-
inline void set_initial(size_t initial) {
122-
initial_ = initial;
123-
}
115+
inline void set_initial(size_t initial) { initial_ = initial; }
124116

125117
static NodeBIO* FromBIO(BIO* bio);
126118

@@ -138,7 +130,9 @@ class NodeBIO : public MemoryRetainer {
138130
static int Write(BIO* bio, const char* data, int len);
139131
static int Puts(BIO* bio, const char* str);
140132
static int Gets(BIO* bio, char* out, int size);
141-
static long Ctrl(BIO* bio, int cmd, long num, // NOLINT(runtime/int)
133+
static long Ctrl(BIO* bio, // NOLINT(runtime/int)
134+
int cmd,
135+
long num, // NOLINT(runtime/int)
142136
void* ptr);
143137

144138
static const BIO_METHOD* GetMethod();
@@ -149,11 +143,8 @@ class NodeBIO : public MemoryRetainer {
149143

150144
class Buffer {
151145
public:
152-
Buffer(Environment* env, size_t len) : env_(env),
153-
read_pos_(0),
154-
write_pos_(0),
155-
len_(len),
156-
next_(nullptr) {
146+
Buffer(Environment* env, size_t len)
147+
: env_(env), read_pos_(0), write_pos_(0), len_(len), next_(nullptr) {
157148
data_ = new char[len];
158149
if (env_ != nullptr)
159150
env_->isolate()->AdjustAmountOfExternalAllocatedMemory(len);

0 commit comments

Comments
 (0)