diff options
author | Oskar <[email protected]> | 2024-08-14 19:58:58 +0200 |
---|---|---|
committer | Oskar <[email protected]> | 2024-08-14 19:58:58 +0200 |
commit | 19f1a70edbf2b08536c114e12f7b3c1e77469730 (patch) | |
tree | 1cb1162c8e205cdb2bcddfbcb1d6a09f2d2ff915 | |
parent | c5587e2392d55b3e0dc322c0545ead4a314132c0 (diff) |
Forgot how painful input buffers are
-rw-r--r-- | 3p37.cpp | 26 | ||||
-rw-r--r-- | 3p38.cpp | 17 | ||||
-rw-r--r-- | 3p39.cpp | 51 | ||||
-rw-r--r-- | 3p40.cpp | 24 | ||||
-rw-r--r-- | UBasan-test.cpp | 1 |
5 files changed, 118 insertions, 1 deletions
diff --git a/3p37.cpp b/3p37.cpp new file mode 100644 index 0000000..968ce86 --- /dev/null +++ b/3p37.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.37 + * + * + */ + +int main () { + + const char ca[] = {'H', 'e', 'l', 'l', 'o'/*, '\0'*/}; + const char *cp = ca; + while(*cp) { + std::cout << *cp << std::endl; + ++cp; + } + /* + * This causes a buffer overflow because there is no null character at the end of the string + * If we put a null-character at the end of the string we don't get an overflow. + */ + return 0; +} diff --git a/3p38.cpp b/3p38.cpp new file mode 100644 index 0000000..f3e320c --- /dev/null +++ b/3p38.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.38 + * + * Adding two memory addresses together will give us + * some completely unknown memory address we know + * nothing about + */ + +int main () { + return 0; +} diff --git a/3p39.cpp b/3p39.cpp new file mode 100644 index 0000000..6f25e86 --- /dev/null +++ b/3p39.cpp @@ -0,0 +1,51 @@ +#include <iostream> +#include <vector> +#include "sales_data.hpp" +#include "sales_item.hpp" +#include <cstring> +#include <string> +#include <limits> + +/* + * + * 3.39 + * + * + */ + +int main () { + + std::string s1; + std::string s2; + if(std::cin >> s1 >> s2) {} else { return -1; } + if(s1 == s2) { + std::cout << "Both strings are equal!" << std::endl; + } else { + std::cout << "The strings are not equal!" << std::endl; + } + + // DUDE i forgot how painful input buffers can be!!! THIS SUCKS! This input sucks and we can only input the maximum characters but whatever im not spending more time on this exercise + constexpr size_t cstring_size = 10; + char cs1[cstring_size]; + char cs2[cstring_size]; + cs1[0] = '\0'; + cs2[0] = '\0'; + for(size_t i = 0 ; i != cstring_size-1 ; ++i) { + std::cin >> cs1[i]; + cs1[i+1] = '\0'; + } + + std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); + for(size_t i = 0 ; i != cstring_size-1 ; ++i) { + std::cin >> cs2[i]; + cs2[i+1] = '\0'; + } + + if(std::strcmp(cs1, cs2) == 0) { + std::cout << "Both strings are equal!" << std::endl; + } else { + std::cout << "The strings are not equal!" << std::endl; + } + + return 0; +} diff --git a/3p40.cpp b/3p40.cpp new file mode 100644 index 0000000..0ca64fd --- /dev/null +++ b/3p40.cpp @@ -0,0 +1,24 @@ +#include <iostream> +#include <vector> +#include <cstring> +#include "sales_data.hpp" +#include "sales_item.hpp" + +/* + * + * 3.40 + * + * Is this overkill? + */ + +int main () { + + char ch1[] = "Hello, "; + char ch2[] = "World!"; + char chfinal[1000]; + chfinal[0] = '\0'; + std::strcpy(chfinal, ch1); + std::strcat(chfinal, ch2); + std::cout << chfinal << std::endl; + return 0; +} diff --git a/UBasan-test.cpp b/UBasan-test.cpp index 1cbcfc7..b5ca4bd 100644 --- a/UBasan-test.cpp +++ b/UBasan-test.cpp @@ -20,7 +20,6 @@ int main () { std::cin >> a; double b = 10 / a; std::cout << b << std::endl; - int c = 10; std::cout << "Input a 0: " << std::endl; std::cin >> c; |