From 320c09285c8aff5cc679160e92908da6336f314f Mon Sep 17 00:00:00 2001 From: anthraxx Date: Tue, 10 Jun 2014 12:39:36 +0200 Subject: [PATCH 1/6] fixing memleak of input char* - adding missing free() calls - dont malloc ourselves as getline() also does a malloc (and anyway called realloc before if the size_t was smaller then the actual len(input-line) to behaviour is the same!) --- challenge/challenge.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/challenge/challenge.c b/challenge/challenge.c index 7354425..b2ca61a 100644 --- a/challenge/challenge.c +++ b/challenge/challenge.c @@ -3,23 +3,26 @@ int main() { int bytes_read; - size_t nbytes = 10; - char **my_string = malloc(sizeof(char**)); + size_t nbytes = 0; + char *my_string = NULL; char *password = "23door42\n"; puts("Please enter Password: "); - bytes_read = getline(my_string, &nbytes, stdin); + bytes_read = getline(&my_string, &nbytes, stdin); if(bytes_read == -1) { puts("Error"); + free(my_string); return -1; } else { - if (strcmp(*my_string, password) == 0) { + if (strcmp(my_string, password) == 0) { puts("Success"); + free(my_string); return 0; } else { puts("How about no?!"); + free(my_string); return -1; } } From bf5f98649dfd204a0d8332c435365cd2d4e0d202 Mon Sep 17 00:00:00 2001 From: anthraxx Date: Tue, 10 Jun 2014 12:46:18 +0200 Subject: [PATCH 2/6] explicitly import string.h for strcmp() - implicitly declaring library function is bad manner :) --- challenge/challenge.c | 1 + 1 file changed, 1 insertion(+) diff --git a/challenge/challenge.c b/challenge/challenge.c index b2ca61a..a674424 100644 --- a/challenge/challenge.c +++ b/challenge/challenge.c @@ -1,3 +1,4 @@ +#include #include #include From ea0b0d95e630867fdb5ea86f92820d4040a96445 Mon Sep 17 00:00:00 2001 From: anthraxx Date: Tue, 10 Jun 2014 13:01:34 +0200 Subject: [PATCH 3/6] reduce complexity + just free() in a single place - therefor if the program gets extended, its unlikly that someone will forget to call free() - also this gets rid of copy-paste free() calls (which look stupid) --- challenge/challenge.c | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/challenge/challenge.c b/challenge/challenge.c index a674424..604f1a5 100644 --- a/challenge/challenge.c +++ b/challenge/challenge.c @@ -3,30 +3,24 @@ #include int main() { - int bytes_read; + int ret; size_t nbytes = 0; char *my_string = NULL; char *password = "23door42\n"; puts("Please enter Password: "); + ret = getline(&my_string, &nbytes, stdin); - bytes_read = getline(&my_string, &nbytes, stdin); - - if(bytes_read == -1) { + if (ret == -1) { puts("Error"); - free(my_string); - return -1; + } else if (strcmp(my_string, password) == 0) { + puts("Success"); + ret = 0; } else { - if (strcmp(my_string, password) == 0) { - puts("Success"); - free(my_string); - return 0; - } else { - puts("How about no?!"); - free(my_string); - return -1; - } + puts("How about no?!"); + ret = -1; } - return 0; + free(my_string); + return ret; } From 6a3da8153d6cbe030077af38eb120952e05e83f4 Mon Sep 17 00:00:00 2001 From: anthraxx Date: Tue, 10 Jun 2014 13:03:34 +0200 Subject: [PATCH 4/6] rename my_string to input_str for better naming --- challenge/challenge.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/challenge/challenge.c b/challenge/challenge.c index 604f1a5..c100906 100644 --- a/challenge/challenge.c +++ b/challenge/challenge.c @@ -5,15 +5,15 @@ int main() { int ret; size_t nbytes = 0; - char *my_string = NULL; + char *input_str = NULL; char *password = "23door42\n"; puts("Please enter Password: "); - ret = getline(&my_string, &nbytes, stdin); + ret = getline(&input_str, &nbytes, stdin); if (ret == -1) { puts("Error"); - } else if (strcmp(my_string, password) == 0) { + } else if (strcmp(input_str, password) == 0) { puts("Success"); ret = 0; } else { @@ -21,6 +21,6 @@ int main() { ret = -1; } - free(my_string); + free(input_str); return ret; } From 6128aef57a114284e4f030f80f351895b1a8e49d Mon Sep 17 00:00:00 2001 From: anthraxx Date: Tue, 10 Jun 2014 14:00:21 +0200 Subject: [PATCH 5/6] adding +x to shell-scripts --- dooropen | 0 lock | 0 unlock | 0 3 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 dooropen mode change 100644 => 100755 lock mode change 100644 => 100755 unlock diff --git a/dooropen b/dooropen old mode 100644 new mode 100755 diff --git a/lock b/lock old mode 100644 new mode 100755 diff --git a/unlock b/unlock old mode 100644 new mode 100755 From e5931c0a4146b006c7b71001c49e977af1674553 Mon Sep 17 00:00:00 2001 From: anthraxx Date: Tue, 10 Jun 2014 14:40:54 +0200 Subject: [PATCH 6/6] adding simple makefile to build door challenge --- .gitignore | 3 ++- Makefile | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index f47cb20..bebb0b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -*.out +/challenge/*.o +/doorchallenge diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..465fd0d --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +TARGET=doorchallenge + +CC:=gcc +LD:=$(CC) + +LDLIBS= +LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro +CFLAGS=-Wall \ + -Wextra \ + -Winit-self \ + -Wuninitialized \ + -Wfloat-equal \ + -Wint-to-pointer-cast \ + -pedantic \ + -O2 \ + -fstack-protector-strong + +all: $(TARGET) + +$(TARGET): challenge/challenge.o + $(LD) $(LDFLAGS) -o $@ $^ $(LDLIBS) + +challenge/%.o: challenge/%.c + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + $(RM) -rf challenge/*.o $(TARGET)