2048.cpp icon indicating copy to clipboard operation
2048.cpp copied to clipboard

Sanitise name input to prevent incorrect behaviour

Open straymonk opened this issue 7 years ago • 13 comments
trafficstars

test

The highscore page goes into the endless cycle and prints new lines.

upd: Name length issue. Can be reproduced by entering name with size >18.

straymonk avatar Sep 20 '18 15:09 straymonk

I can't seem to reproduce the bug. In any case, sanitisation of input should be implemented. If you are willing to, a PR is most welcome for its implementation. If and when I get time, I'll fix the issue. Thanks for bringing it to notice.

plibither8 avatar Sep 20 '18 15:09 plibither8

Ah yes, I can reproduce it now. Seems like a trivial fix, thank you

plibither8 avatar Sep 28 '18 05:09 plibither8

Hey!! Anyone working on this issue? I would like to do something with this repo because

  1. It is CPP and I just finished learning the language
  2. It is a game

mayurdw avatar Oct 01 '18 21:10 mayurdw

Go ahead! As far as I know, there are no takers yet :)

plibither8 avatar Oct 02 '18 03:10 plibither8

Sweet!!

mayurdw avatar Oct 03 '18 05:10 mayurdw

Hey! So I finally got around to working on this. Sorry about the delay.

What is the solution you are thinking about here? I don't want to try and implement something you don't think is needed

mayurdw avatar Oct 08 '18 09:10 mayurdw

Hey, no worries! A PR was made (#44) that addressed this issue, though it cannot be merged right now due to conflicts. You can view it and make suggestions, or if you believe you have a better, more efficient solution, create a separate PR!

The solution I had in mind was a simple check to see whether the characters the person enters are alphanumeric only.

plibither8 avatar Oct 08 '18 10:10 plibither8

The effect is because of the way data is read from the input. It is using common console reading. But, in my opinion, you should consider using something with a better approach: reading directly from the terminal, using ncurses for *nix systems and console.h for Windows systems. Also, I could notice that CTRL C breaks the app. It should consider using signal handling to make the app close in a controller manner instead of relying on the default signal handler which kills the application.

ronflima avatar Oct 21 '18 13:10 ronflima

@ronflima wouldn't using ncurses be a slight overkill? Moreover, this would increase the requirement of separation of code for *nix systems and Windows.

plibither8 avatar Oct 21 '18 16:10 plibither8

It depends on your objectives. If you want to keep the code 100% portable, you must rely on STDC++ library and that’s it. However you will have a lot of usability issues since OS interface offered by STDC++ is really high level due to its generality.

ronflima avatar Oct 21 '18 18:10 ronflima

Makes sense, though I haven't discarded the idea of using ncurses and console.h – just lacking a Windows environment to work and test on 😅. Could you elaborate on the usability issues?

plibither8 avatar Oct 23 '18 01:10 plibither8

system("clear"); works only on *nix systems, for instance. On Windows, you have to use something from the old conio.h or from console.h (I'm kind of outdated on this matter), or rely on Cygwin or other POSIX layer.

If the user presses CTRL-C, the game simply dies. If you press CTRL-J, which is an invalid keystroke, it will be echoed back to the terminal. You could be using a better menu based on up and down arrow keys instead to have the user to enter letters representing the menu items.

But, again, it all depends on your objectives. As much you control the console, more system dependent code is needed. The way to do so is to mimic STDC library: create general interfaces, letting the implementation to be compiled according to the host operating system.

ronflima avatar Oct 23 '18 01:10 ronflima

Using ncurses to improve user interactions too, just press a single key without enter.

tiendq avatar Oct 26 '18 08:10 tiendq