From 558be63805357fe874c77fc2c2007433b2787396 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 21 Nov 2022 16:38:13 +0100 Subject: [PATCH 1/4] c: ex2: add solution --- c/ex2.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 c/ex2.c diff --git a/c/ex2.c b/c/ex2.c new file mode 100644 index 0000000..f41d132 --- /dev/null +++ b/c/ex2.c @@ -0,0 +1,30 @@ +#include +#include + +char* f(int m) { + char buf[6]; + int x; + + if (m == 1 && x-- /* variable is read before initialization */) { + strcpy(buf, "AAAAAA"); // null-byte copy overflows buffer at index 7 + return buf; // Return stack allocated array + } else if (m == 2) { + char* msg = (char*)malloc(100); // malloc is declared in + // While technically not UB, as it will be implicitly declared by the + // compiler, this is error-prone and the implicit declaration most + // likely does not match the actual function + strcpy(msg, "BBBBBB"); + return msg; + } + // Missing return value +} + +int main(int argc, char** argv) { + char* m; + m = f(argc); + putchar(m[0]); // if m != 2, either reads garbage stack memory or + // non-sensical pointer due to missing return value + return 0; + // Potential memory leak, if m == 2 + // While not UB, it is frowned upon +} From 13348109d162134ea9b1941e0c69103625885bc2 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 21 Nov 2022 17:03:27 +0100 Subject: [PATCH 2/4] c: ex3: add solution --- c/ex3.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 c/ex3.c diff --git a/c/ex3.c b/c/ex3.c new file mode 100644 index 0000000..2d4d54c --- /dev/null +++ b/c/ex3.c @@ -0,0 +1,33 @@ +#include +#include + +static unsigned char reverse_byte(unsigned char c) { + unsigned char res = 0; + for (size_t i = 0; i < CHAR_BIT; ++i) { + res <<= 1; + res |= c & 1; + c >>= 1; + } + return res; +} + +static void swap_bytes(unsigned char* lhs, unsigned char* rhs) { + unsigned char tmp = *lhs; + *lhs = *rhs; + *rhs = tmp; +} + +void reverse_bytes(unsigned char* buf, size_t n) { + if (!buf || !n) + return; + + // No need to worry about applying reverse_byte twice in the middle by + // mistake if done in a preliminary pass + for (size_t i = 0; i < n; ++i) { + buf[i] = reverse_byte(buf[i]); + } + + for (size_t i = 0; i < ((n + 1) / 2); ++i) { + swap_bytes(&buf[i], &buf[n - 1 - i]); + } +} From 4950125379b78e951850f7035ea688f35063dabf Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 21 Nov 2022 17:11:03 +0100 Subject: [PATCH 3/4] cpp: ex1: add solution --- cpp/ex1.cc | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 cpp/ex1.cc diff --git a/cpp/ex1.cc b/cpp/ex1.cc new file mode 100644 index 0000000..2882562 --- /dev/null +++ b/cpp/ex1.cc @@ -0,0 +1,28 @@ +#ifdef USE_ATOMIC_N +#include +#endif + +class CountInstances { +public: + CountInstances() { + ++n_instances_; + } + + ~CountInstances() { + --n_instances_; + } + + int nobjs() const { + return n_instances_; // If using atomics, load could use a more relaxed + // memory order, such as memory_order_acquire + } + +private: +#ifdef USE_ATOMIC_N + // Can be used in a multi-thread program + static atomic n_instances_; +#else + // Using a bare int mean races if used in a multi-threaded context + static int n_instances_; +#endif +}; From afb500c7caa4cb331d9509d0fd737bc23d29abfc Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 21 Nov 2022 17:12:19 +0100 Subject: [PATCH 4/4] cpp: ex2: add solution --- cpp/ex2.cc | 1 + 1 file changed, 1 insertion(+) create mode 120000 cpp/ex2.cc diff --git a/cpp/ex2.cc b/cpp/ex2.cc new file mode 120000 index 0000000..d810e4b --- /dev/null +++ b/cpp/ex2.cc @@ -0,0 +1 @@ +../c/ex1.c \ No newline at end of file