CPP Best Practices and Notes -- Part 9

Notes taken from https://www.learncpp.com/

9.2 Arrays II

  • Explicitly initialize arrays, even if they would be initialized without an initializer list.

  • When passing arrays to functions, C++ does not copy an array. The actual array is passed.

    • To ensure a function does not modify the array passed into it, make the array const:
      1
      2
      3
      void functionWithArray(const int arr[5]) {
      std::size(arr); // note that this will cause error!!
      }
  • When you are in the same function that a array is
    declared in, sizeof() will return the total size of the array (array length multiplied by element size)

    • When sizeof is used on an array that has been passed to a function, it doesn’t error
      out like std::size() does. Instead, it returns the size of a pointer.
Read more

Set modules to false in babel

Version 2 of webpack supports ES6 modules syntax natively, meaning you can use import and export without a tool like babel to handle this for you.

So when we config babel-preset-env we can set “modules” to false, instead of using babel to compiling then into other module type.

NodeJS poll phrase blocking logic

As mentioned in the NodeJS doc, in poll phrase, node will block when appropriate. This post will discuss the block conditions and why this phrase blocks as Node should perfom a non-blocking I/O operations.

There are several logics in libuv core.c

First is the uv_run function, which operates the process of entire event loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
int uv_run(uv_loop_t* loop, uv_run_mode mode) {
int timeout;
int r;
int ran_pending;

r = uv__loop_alive(loop);
if (!r)
uv__update_time(loop);

while (r != 0 && loop->stop_flag == 0) {
uv__update_time(loop);
uv__run_timers(loop);
ran_pending = uv__run_pending(loop);
uv__run_idle(loop);
uv__run_prepare(loop);

timeout = 0;
if ((mode == UV_RUN_ONCE && !ran_pending) || mode == UV_RUN_DEFAULT)
timeout = uv_backend_timeout(loop);

uv__io_poll(loop, timeout);
uv__run_check(loop);
uv__run_closing_handles(loop);

if (mode == UV_RUN_ONCE) {
/* UV_RUN_ONCE implies forward progress: at least one callback must have
* been invoked when it returns. uv__io_poll() can return without doing
* I/O (meaning: no callbacks) when its timeout expires - which means we
* have pending timers that satisfy the forward progress constraint.
*
* UV_RUN_NOWAIT makes no guarantees about progress so it's omitted from
* the check.
*/
uv__update_time(loop);
uv__run_timers(loop);
}

r = uv__loop_alive(loop);
if (mode == UV_RUN_ONCE || mode == UV_RUN_NOWAIT)
break;
}

/* The if statement lets gcc compile it to a conditional store. Avoids
* dirtying a cache line.
*/
if (loop->stop_flag != 0)
loop->stop_flag = 0;

return r;
}
Read more

Padding Oracle Attack

Requirement

The system must use CBC mode with PKCS7 for the padding block.

This is a chosen-ciphertext attack, The adversary can submit encrypted messages to the system, and the system will return valid or invalid as response. Assume block size is 8.

Background

Encryption Decryption
Ci = Encrypt(Pi ⊕ Ci-1) Pi = Decrypt(Ci ⊕ Ci-1)
Read more