Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entering any code that outputs causes a crash #2

Open
MineRobber9000 opened this issue Jan 17, 2017 · 6 comments
Open

Entering any code that outputs causes a crash #2

MineRobber9000 opened this issue Jan 17, 2017 · 6 comments

Comments

@MineRobber9000
Copy link
Owner

Any code that isn't supposed to output outputs a gender symbol.

@MineRobber9000
Copy link
Owner Author

Erroneous code probably in bf.c

@kitlith
Copy link

kitlith commented Jan 18, 2017

https://github.com/MineRobber9000/bf3ds/blob/master/src/bf.c#L29

strcat concatinates a string to another string, not a character to a string. Moreover, I believe the behavior is supposed to be that you print each character as they arrive, so that you can print a dialog requesting something for input, and then request input... As it stands it'll only print output when it returns.

I'd reccomend using putchar(*ptr), or, if you want it to be usable for other, more fancy outputs, define a custom function that takes a char and make the user define it elsewhere.

EDIT 2: Even bigger issue, even if strcat worked how you thought it did... you are not initializing output. Which means you're pointing to random memory and writing to it. Oooooops.

Finally, unrelated to the actual issue, I feel this would be better served by a switch statement with a default clause that does nothing. Up to you, of course.

EDIT: You are also not initializing the tape to 0, so there might be any junk there.

@MineRobber9000
Copy link
Owner Author

Answering your questions and remarks:

strcat concatinates a string to another string, not a character to a string.

You are also not initializing the tape to 0, so there might be any junk there.

I didn't catch these, thanks.

Even bigger issue, even if strcat worked how you thought it did... you are not initializing output. Which means you're pointing to random memory and writing to it. Oooooops.

https://github.com/MineRobber9000/bf3ds/blob/master/src/bf.c#L11

Maybe I should change char * output to char output[4096]?

@kitlith
Copy link

kitlith commented Jan 19, 2017

https://github.com/MineRobber9000/bf3ds/blob/master/src/bf.c#L11

Even bigger issue, even if strcat worked how you thought it did... you are not initializing output. Which means you're pointing to random memory and writing to it. Oooooops.

char * output;

This does not initialize output. It declares it, and allocates a 4 byte vaule on the stack.
It's a pointer, which is currently whatever garbage was previously on stack.
You need to initialize the pointer to a useful memory location, for example, via allocation.
Something like char *output = malloc(4096); memset(output, 0, 4096);
You would then free it after use.

Maybe I should change char * output to char output[4096]?
Ah, but then the output buffer is allocated on the stack, and when you return, you return a pointer to something that ends up being deallocated... thus once again leading to undefined behavior.

Once again, I do not reccomend returning the output.
If you want to do so for some other reason, also print each character somehow as they come along. This way, when you implement input, you can have dialogs that ask for input... that aren't printed way too late.

@MineRobber9000
Copy link
Owner Author

MineRobber9000 commented Jan 20, 2017

I'm not implementing input. I thought it over when I began the project and decided it just isn't worth it. I would have to implement a touchscreen keyboard for the input (as you're only allowed to input 1 character) and it would take too much work. Besides, the point of Brainfuck is to be challenging, right? 😃

@kitlith
Copy link

kitlith commented Jan 20, 2017

Heh. You could queue up input characters. But I see what you're going for, I guess. Good luck.

techfreek added a commit to techfreek/bf3ds that referenced this issue Oct 8, 2019
Allocates a buffer for output and initializes it to 0. Change the appending method to use strncat of length 1 which should provide similar results while ensuring we pass in valid input.

Have not tested
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants