Brainfuck Interpreter in C - the third day

Hello again

First of all, we need to remind what are we should going to do in our interpreter. In the first topic of writing this program, I wrote some important functionalities. Now it's the time to show you what we did and what we have to do in the future.


These functionalities our program actually has:
- a file has to be read as an argument
- react if the file is not specified
- react if the file extension is not right
- react if the file is not found
- read brainfuck instructions from a file
- check if the char from the file is brainfuck instruction
- if it is brainfuck instruction then add it to the linked list

So as you see we did such a great job, but we have a lot of work to end this project. Today we will add to the project these important things:
- prints all of the instructions (to check if our linked list with an instructions works correctly)
- clear the memory (more precisely the cheap memory) from the elements of the linked list

So let's write a function that prints all of the instructions to the standard output.


Let's test this function before analyze. Call looks like this:


My file with brainfuck instructions has this:

But when we execute interpreter with the specified file, the output will be different from example file.


It doesn't print the last instruction. Let's try to fix it.

ERROR:
The print_instructions function doesn't print the last instruction

SOLUTION:
We can assume that the issue in this function appears in the loop because first instruction only set the current_instr_ptr to the first element of the linked list.
Now we need to understand what the expression in the while loop actually does. 

current_instr_ptr->next_element basically means "take the next element of the linked list (more precisely takes the pointer to the next element)"

This is simply iteration on the linked list. The last element of this list sets the current_instr_ptr->next_element pointer to the NULL. So our iteration will stop at the last brainfuck instruction - this is why the last instruction is not on the screen.

The solution is to write: *current_instr_ptr != NULL instead of (*current_instr_ptr)->next_element != NULL.  


Now we should test if this really works fine.


Everything is ok. Now we already know that add_instruction_to_the_list and is_bf_instruction functions work correctly. So now we can write another important function which will clear the memory.


This function creates the pointer to the element of our linked list and sets the pointer to the current instruction to the first instruction in the list. After this, we have a loop which iterates all over the list. earlier_element is set to the current instruction. current_instr_ptr is set to the next instruction and free(earlier_element) simply removes the element of the linked list from the memory. Let's test this function and check if we think correctly.


Implementation of clearing the memory works fine. Amazing!

The whole code of the program:
 

Comments

Popular posts from this blog

PicoCTF 2018 - Reverse Engineering writeups

Learning of malware analysis. Solving labs from the "Analyzing malicious Windows programs" chapter from the "Practical Malware Anlysis" book

Learning of malware analysis. Solving 9-1 lab from the "OllyDbg" chapter. ("Practical Malware Analysis" book)