From ca7e62a73fec9e8c2d3536bc220bacfdfa090f36 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Fri, 30 May 2014 23:58:40 +0200 Subject: [PATCH 1/3] extended buffer overflow example --- c_pointers/chapter_5.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/c_pointers/chapter_5.c b/c_pointers/chapter_5.c index b58a7b1..407588f 100644 --- a/c_pointers/chapter_5.c +++ b/c_pointers/chapter_5.c @@ -97,7 +97,7 @@ int main(void) { // better: char *command = malloc(11); if (command != NULL) { - printf("%s: ", "Enter a command"); + printf("%s: ", "Enter a command (max. 10 chars): "); scanf("%10s", command); printf("You entered: %s with length: %zu\n", command, strlen(command)); free(command); @@ -107,7 +107,10 @@ int main(void) { // executes without warnings or errors on osx :D char *mem = malloc(11); if (mem != NULL) { - memset(mem, 0x42, 64); + memset(mem, 0x42, 16); + for (size_t i = 0; i < 16; i++) { + printf("mem[%zu] Address: %p : %c\n", i, &mem[i], mem[i]); + } free(mem); } From ef09dca3b163b56c06294c1399bd31436ce06d2f Mon Sep 17 00:00:00 2001 From: tmeissner Date: Sun, 24 Aug 2014 15:23:37 +0200 Subject: [PATCH 2/3] added __pycache__ directory to .gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b912230..265acb1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ *.swp *.*#* .idea +__pycache__ \ No newline at end of file From 94484938c31c5c41431d9dd27ed9d357c2e502e4 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Sun, 24 Aug 2014 18:45:55 +0200 Subject: [PATCH 3/3] initial commit of chapter 2 case study --- python_3_oop/chapter02/menu.py | 69 ++++++++++++++++++++++++++++++ python_3_oop/chapter02/notebook.py | 69 ++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 python_3_oop/chapter02/menu.py create mode 100644 python_3_oop/chapter02/notebook.py diff --git a/python_3_oop/chapter02/menu.py b/python_3_oop/chapter02/menu.py new file mode 100644 index 0000000..c18c0a8 --- /dev/null +++ b/python_3_oop/chapter02/menu.py @@ -0,0 +1,69 @@ +import sys +from notebook import Notebook, Note + +class Menu: + '''Display a menu and respond to choices when run.''' + + def __init__(self): + self.notebook = Notebook() + self.choices = { + "1": self.show_notes, + "2": self.search_notes, + "3": self.add_note, + "4": self.modify_note, + "5": self.quit + } + + def display_menu(self): + print(""" +Notebook Menu +1. Show all Notes +2. Search Notes +3. Add Note +4. Modify Note +5. Quit """) + + def run(self): + '''Display the menu and respond to choices.''' + while True: + self.display_menu() + choice = input("Enter an option: ") + action = self.choices.get(choice) + if action: + action() + else: + print("{0} is not a valid choice".format(choice)) + + def show_notes(self, notes=None): + if not notes: + notes = self.notebook.notes + for note in notes: + print("{0}: {1}\n{2}".format(note.id, note.tags, note.memo)) + + def search_notes(self): + filter = input("Search for: ") + notes = self.notebook.search(filter) + self.show_notes(notes) + + def add_note(self): + memo = input("Enter a memo: ") + self.notebook.new_note(memo) + print("Your note has been added.") + + def modify_note(self): + id = input("Enter a note id: ") + memo = input("Enter a memo: ") + tags = input("Enter tags: ") + if memo: + if not self.notebook.modify_memo(id, memo): + print("Note with id {0} doesn't exist.".format(id)) + if tags: + if not self.notebook.modify_tags(id, tags): + print("Note with id {0} doesn't exist.".format(id)) + + def quit(self): + print("Thank you for using your notebook today.") + sys.exit(0) + +if __name__ == "__main__": + Menu().run() diff --git a/python_3_oop/chapter02/notebook.py b/python_3_oop/chapter02/notebook.py new file mode 100644 index 0000000..87c5042 --- /dev/null +++ b/python_3_oop/chapter02/notebook.py @@ -0,0 +1,69 @@ +import datetime + +# Store the next available id for all new notes +last_id = 0 + +class Note: + '''Represent a note in the notebook. Match against a + string in searches and store tags for each note.''' + + def __init__(self, memo, tags=''): + '''initialize a note with memo and optional + space-separated tags. Automatically set the note's + creation date and a unique id.''' + self.memo = memo + self.tags = tags + self.creation_date = datetime.date.today() + global last_id + last_id += 1 + self.id = last_id + + def match(self, filter): + '''Determine if this note matches the filter + text. Return True if it matches, False otherwise. + Search is case sensitive and matches both text and + tags.''' + return filter in self.memo or filter in self.tags + + +class Notebook: + '''Represent a collection of notes that can be tagged, + modified, and searched.''' + + def __init__(self): + '''Initialize a notebook with an empty list.''' + self.notes = [] + + def new_note(self, memo, tags=''): + '''Create a new note and add it to the list.''' + self.notes.append(Note(memo, tags)) + + def modify_memo(self, note_id, memo): + '''Find the note with the given id and change its + memo to the given value.''' + note = self._find_note(note_id) + if note: + note.memo = memo + return True + return False + + def modify_tags(self, note_id, tags): + '''Find the note with the given id and change its tags + to the given value.''' + note = self._find_note(note_id) + if note: + note.tags = tags + return True + return False + + def search(self, filter): + '''Find all notes that match the given filter string.''' + return [note for note in self.notes if note.match(filter)] + + def _find_note(self, note_id): + '''Locate the note with the given id.''' + for note in self.notes: + if str(note.id) == str(note_id): + return note + return None +