Merge branch 'master' into 'master'

memleak bugfix + cleanup + Makefile

- memleak bugfix as free() was missing
- dont call malloc() manually (the old way getline() always called realloc() anyway because of the way the malloc() was written [did not allocate enought memory for the string])
- small minor cleanup
- adding simple Makefile to build door challenge
This commit is contained in:
gnomus 2014-06-10 13:35:19 +00:00
commit 0e62272c44
6 changed files with 42 additions and 16 deletions

3
.gitignore vendored
View file

@ -1 +1,2 @@
*.out /challenge/*.o
/doorchallenge

27
Makefile Normal file
View file

@ -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)

View file

@ -1,28 +1,26 @@
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int main() { int main() {
int bytes_read; int ret;
size_t nbytes = 10; size_t nbytes = 0;
char **my_string = malloc(sizeof(char**)); char *input_str = NULL;
char *password = "23door42\n"; char *password = "23door42\n";
puts("Please enter Password: "); puts("Please enter Password: ");
ret = getline(&input_str, &nbytes, stdin);
bytes_read = getline(my_string, &nbytes, stdin); if (ret == -1) {
if(bytes_read == -1) {
puts("Error"); puts("Error");
return -1; } else if (strcmp(input_str, password) == 0) {
} else {
if (strcmp(*my_string, password) == 0) {
puts("Success"); puts("Success");
return 0; ret = 0;
} else { } else {
puts("How about no?!"); puts("How about no?!");
return -1; ret = -1;
}
} }
return 0; free(input_str);
return ret;
} }

0
dooropen Normal file → Executable file
View file

0
lock Normal file → Executable file
View file

0
unlock Normal file → Executable file
View file