Last active
February 16, 2024 08:14
-
-
Save timClicks/fb7b48ab24be772658cf10a9f269f1de to your computer and use it in GitHub Desktop.
How many bugs can you spot?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// By [Colin Finck] and used by the [Comprehensive Rust] course, | |
// developed by the Android team at Google. | |
// | |
// This code compiles warning-free at the default warning level, | |
// even in the latest GCC version (13.2 as of writing). | |
// | |
// [Colin Finck]: https://colinfinck.de/Master_Thesis_Slides.pdf | |
// [Comprehensive Rust]: https://github.com/google/comprehensive-rust | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/stat.h> | |
int main(int argc, char* argv[]) { | |
char *buf, *filename; | |
FILE *fp; | |
size_t bytes, len; | |
struct stat st; | |
switch (argc) { | |
case 1: | |
printf("Too few arguments!\n"); | |
return 1; | |
case 2: | |
filename = argv[argc]; | |
stat(filename, &st); | |
len = st.st_size; | |
buf = (char*)malloc(len); | |
if (!buf) | |
printf("malloc failed!\n", len); | |
return 1; | |
fp = fopen(filename, "rb"); | |
bytes = fread(buf, 1, len, fp); | |
if (bytes = st.st_size) | |
printf("%s", buf); | |
else | |
printf("fread failed!\n"); | |
case 3: | |
printf("Too many arguments!\n"); | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment