4 ms·
Could you humor a coding noob--how do you deal with utterly insane inputs like that?
by clark_dent 5mo ago
Could you humor a coding noob--how do you deal with utterly insane inputs like that?
- doubled112 5mo agoCrash and report an error.
- chowells 5mo agoYou report an error and exit cleanly with a proper operating system error code. Crashing is a quick hack, acceptable for throwaway projects but not in software used long-term.
- SoftTalker 5mo agoCrashing (in the sense of "give up and exit with an error") on invalid inputs is valid (and often the best thing) in many cases. Fix your inputs.
- chowells 5mo agoI think you're using "crash" to mean "exit early". I am using "crash" in the sense of "this program did something causing the OS to terminate it externally". I suppose that's a real point of difficulty in communication across different programming languages. We agree that the program should exit early. I think we agree it should do it cleanly and intentionally. I'm adding the constraint that "crash" doesn't necessarily mean "cleanly and intentionally", especially when talking about a C program.
- derefr 5mo agoThere's a position in between "exit cleanly" and "general protection fault, core dumped" where the process essentially does the internal equivalent of SIGKILLing itself. I.e. either intentionally (e.g. tripping an assertion failure), or accidentally due to some logic-failure in exception/error-handling, the process ends up calling the exit(3) syscall without first having run its libc at_exit finalizers that a clean exit(2) would run; or, at a slightly higher runtime abstraction level, the process calls exit(2) or returns from main(), without having run through the appropriate RAII destructors (in C++/Rust), or gracefully signalled will-shutdown to managed threads to allow them to run terminating-state code (in Java/Go/Erlang/Win32/etc), or etc. This kind of "hard abort" often truncates logging output at the point of abort; leaves TCP connections hanging open; leaves lockfiles around on disk; and has the potential to corrupt any data files that were being written to. Basically, it results in the process not executing "should always execute" code to clean up after itself. So, although the OS kernel/scheduler thinks everything went fine, and that it didn't have to step in to forcibly terminate the process's lifecycle (though it did very likely observe a nonzero process exit code), I think most people would still generally call this type of abort a "crash." The process's runtime got into an invalid/broken state and stopped cleaning up, even if the process itself didn't violate any protection rules / resource limits / etc.
- troad 5mo agoInteresting difference in nomenclature. For me, "crash" absolutely includes intentional early termination. A Rust panic, for example. I think that that's by far the dominant usage of crash. It would surprise me if someone used the word crash but intended to exclude panics, etc.
- chowells 5mo agoIt's a very language-dependent meaning. In C, the only type of crash is the OS shutting it down on some sort of trap. Everything else is the result of an explicit code path. Since we're talking about C, it's the definition I'm using. In other contexts, other definitions will apply.
- troad 4mo agoDefinitely! An interesting distinction. I spend much of my time in the BEAM these days, where "let it crash" is a common practice with a very distinct meaning (green threads under supervisor trees, etc). Different strokes.
- matthewkayin 5mo agoYou first ask if you really need to.
- AnimalMuppet 5mo agoUnless you're exposing it to the internet, ever, in the entire future history of the program. Then you kind of have to, in one form or another.
- Someone 5mo agoYou have to, but you probably shouldn’t do it by trying to add the inputs. That opens a door for DDOS attacks. Returning an error on inputs that are too long (for some definition of it) is the way to go.
- wwalexander 5mo agoArbitrary precision arithmetic (GMP, BigInteger, etc). Numbers can take arbitrary amounts of memory, instead of just a single machine word.
- Ekaros 5mo agoAt some point you can just refuse. Too many digits. Well time to quit with error.