defensive-programming

0xDEADBEEF equivalent for 64-bit development?

流过昼夜 提交于 2019-12-02 21:41:10
For C++ development for 32-bit systems (be it Linux, Mac OS or Windows, PowerPC or x86) I have initialised pointers that would otherwise be undefined (e.g. they can not immediately get a proper value) like so: int *pInt = reinterpret_cast<int *>(0xDEADBEEF); (To save typing and being DRY the right-hand side would normally be in a constant, e.g. BAD_PTR.) If pInt is dereferenced before it gets a proper value then it will crash immediately on most systems (instead of crashing much later when some memory is overwritten or going into a very long loop). Of course the behavior is dependent on the

Does wrapping everything in try/catch blocks constitute defensive programming?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 18:54:26
I have been programming for the last 3 years. When I program, I use to handle all known exceptions and alert the user gracefully. I have seen some code recently which has almost all methods wrapped inside try/catch blocks. The author says it is part of defensive programming. I wonder, is this really defensive programming? Do you recommend putting all your code in try blocks? My basic rule is : Unless you can fix the problem which caused the exception, do not catch it, let it bubble up to a level where it can be dealt with. In my experience, 95% of all catch blocks either just ignore the

techniques for obscuring sensitive strings in C++

落花浮王杯 提交于 2019-12-02 16:08:41
I need to store sensitive information (a symmetric encryption key that I want to keep private) in my C++ application. The simple approach is to do this: std::string myKey = "mysupersupersecretpasswordthatyouwillneverguess"; However, running the application through the strings process (or any other that extracts strings from a binary app) will reveal the above string. What techniques should be used to obscure such sensitive data? Edit: OK, so pretty much all of you have said "your executable can be reverse engineered" - of course! This is a pet peeve of mine, so I'm going to rant a bit here:

Test Cases AND assertion statements

∥☆過路亽.° 提交于 2019-12-01 14:07:43
问题 The code in this question made me think assert(value>0); //Precondition if (value>0) { //Doit } I never write the if-statement. Asserting is enough/all you can do. "Crash early, crash often" CodeComplete states: The assert-statement makes the application Correct The if-test makes the application Robust I don't think you've made an application more robust by correcting invalid input values, or skipping code: assert(value >= 0 ); //Precondition assert(value <= 90); //Precondition if(value < 0)

What's the most defensive way to loop through lines in a file with Perl?

↘锁芯ラ 提交于 2019-11-30 11:49:06
I usually loop through lines in a file using the following code: open my $fh, '<', $file or die "Could not open file $file for reading: $!\n"; while ( my $line = <$fh> ) { ... } However, in answering another question , Evan Carroll edited my answer, changing my while statement to: while ( defined( my $line = <$fh> ) ) { ... } His rationale was that if you have a line that's 0 (it'd have to be the last line, else it would have a carriage return) then your while would exit prematurely if you used my statement ( $line would be set to "0" , and the return value from the assignment would thus also

Defensive programming [closed]

空扰寡人 提交于 2019-11-30 03:09:20
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 4 years ago . When writing code do you consciously program defensively to ensure high program quality and to avoid the possibility of your code being exploited maliciously, e.g. through buffer overflow exploits or code injection ? What's the "minimum" level of quality you'll always apply

Checklist for Web Site Programming Vulnerabilities

和自甴很熟 提交于 2019-11-29 22:29:18
Watching SO come online has been quite an education for me. I'd like to make a checklist of various vunerabilities and exploits used against web sites, and what programming techniques can be used to defend against them. What categories of vunerabilities? crashing site breaking into server breaking into other people's logins spam sockpuppeting , meatpuppeting etc... What kind of defensive programming techniques? etc... From the Open Web Application Security Project : The OWASP Top Ten vulnerabilities (pdf) For a more painfully exhaustive list: Category:Vulnerability The top ten are: Cross-site

Erlang's let-it-crash philosophy - applicable elsewhere?

北战南征 提交于 2019-11-29 19:46:31
Erlang's (or Joe Armstrong's?) advice NOT to use defensive programming and to let processes crash (rather than pollute your code with needless guards trying to keep track of the wreckage) makes so much sense to me now that I wonder why I wasted so much effort on error handling over the years! What I wonder is - is this approach only applicable to platforms like Erlang? Erlang has a VM with simple native support for process supervision trees and restarting processes is really fast. Should I spend my development efforts (when not in the Erlang world) on recreating supervision trees rather than

Checklist for Web Site Programming Vulnerabilities

好久不见. 提交于 2019-11-28 19:06:59
问题 Watching SO come online has been quite an education for me. I'd like to make a checklist of various vunerabilities and exploits used against web sites, and what programming techniques can be used to defend against them. What categories of vunerabilities? crashing site breaking into server breaking into other people's logins spam sockpuppeting, meatpuppeting etc... What kind of defensive programming techniques? etc... 回答1: From the Open Web Application Security Project: The OWASP Top Ten

C++ always use explicit constructor [closed]

自古美人都是妖i 提交于 2019-11-28 16:15:56
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . After reading the following blog : http://xania.org/200711/ambiguous-overloading I started asking myself "should I not always explicit