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 +}