SDD

University of London CM 2010 Software Design and Development


Project maintained by ccy05327 Hosted on GitHub Pages — Theme by mattgraham

Topic 3 Defensive coding, debugging and exception handling

Week 9 Assertion and parameter checking

Learning objectives

Readings

Opinion 1: Always use assertions

Opinion 2: In industry, assertions are often removed in release builds

For a great discussion of various aspects: What are assertions?

Assertions

Assume in runtime assertion where the program is running. Other types of assertions: unit tests, compile-time assertions.

An assertion is a check in your code that evaluates a boolean expression. It checks whether the program is in a desirable state.

int gameScore;
...
assert(gameScore >= 0);

An if statement controls the flow of the program; An assertion checks the state.

What should you do if a run-time assertion fails?

Assertions & SDD lifecycle

Test that the loop has not iterated more than 10,000 times.

Before devision operation, check you are not dividing by zero.

lifecycle (Simplified version)

Demonstration of Assertions


Week 10 Secure programming

Learning Objectives

Reading

(PDF) Classic paper from 1975: Saltzer, J.H. and M. D. Schroeder ‘The protection of information in computer systems’, Proceedings of the IEEE 63(9) 1975, pp.1278–308.

(PDF) David Wheeler’s book is a great resource. It is Unix focused but applicable to all platforms: Wheeler, D.A. Secure programming for Linux and Unix HOWTO (1999).

Some interesting articles can be found in Security and Privacy.

(PDF) For an analysis of software companies’ approach to secure software, read Geer, D. ‘Are companies actually using secure development life cycles?’, Computer 43(6) 2010, pp.12–16.

Links:

Secure programming overview

Security goals

Sucure Programming Hit List

The software development lifecycle (SDLC)

…a structure for the various software development activities to be performed within a project.

i.e. test-driven development

Microsoft Security Development Lifecycle (SDL)

…consists of a set of practices that support security assurance and compliance requirements. It helps developers build more secure software by reducing the number and severity of vulnerabilities in software, while reducing development cost.

  1. Provide Training
  2. Define Security Requirements
  3. Define Metrics and Compliance Reporting
  4. Perform Threat Modeling
  5. Establish Design Requirements
  6. Define and Use Cryptography Standards
  7. Manage the Security Risk of Using Third-Party Components
    1. Inventory
    2. Perform security analysis
    3. Keep up to date
  8. Use Approved Tools
  9. Perform Static Analysis Security Testing (SAST)
  10. Looking at the source code (automated tools or human) to evalute problems.
  11. Perform Dynamic Analysis Security Testing (DAST)
  12. Test on a running software for security.
  13. Perform Penetration Testing
  14. Establish a Standard Incident Response Process

Alternatives to MS SDL

Static analysis in Python (bandit)

Testing third-party libraries

  1. pip install tornado
  2. Locate library
  3. Navigate the library location bandit *.py to test all .py files in that library (bandit -r . if the command doesn’t work)
  4. bandit -ll *.py to show only medium & high severity (bandit -rll . works)

Week 11 Exception handling

Learning Objectives

Different types of errors

Exceptions

  1. Event that causes suspension of normal program execution

  2. Indication that an operation request was not performed successfully

The foundamental idea is to separate detection of an error (which shuold be done in a called function) from the handling of an error (which should be done in the calling function) while ensuring that a detected error cannot be ignored.

Bjarne Stroustrup - Programming: principles and practice using C++, 2014

Assertion

  1. logical expression specifying a program state that must exist or a set of conditions that program variables must satisfy at a particular point during program execution.

  2. function or macro that complains loudly if a design assumption on which the code is based is not true

assertion vs. exception image
Assertion vs. Exception

Exceptions should be used to catch errors, control flow has similar syntax and function, but shouldn’t be used as such.

Try and catch

assertion vs. exception image
Try and catch

Try and catch in JavaScript

try {
  verifyUser();
  console.log('After verifyUser');
} catch (ex) {
  console.log('Exception caught');
  console.log('Name: ' + ex.name);
  console.log('Message: ' + ex.message);
}
console.log('I am still running...');

Throw in JavaScript

Exception Handling in JavaScript (code)

Programming exercise

Here is a reference for the built-in errors in JavaScript:

Can you write an example program that generates one of these errors in JavaScript?

Exercise (code)


Week 12 Using a debugger

Learning Objectives

Intro to Debugger

Debugging is simply the process of removing errors from your code. A debugger is a tool that you can use to inspect your program as it is running.

Using print, console.log, and std::cout are a kind of manual debugger.

Debugger allows dynamic analysis.

Debugging with GDB

Debug in C++ (code)

Reading

Here is an advanced article which summarises some of the state of the art work circa 2016 regarding debugging:

(PDF) Wong, W.E., R. Gao, Y. Li, R. Abreu and F. Wotawa ‘A survey on software fault localization’, IEEE Transactions on Software Engineering 42(8) 2016, pp. 707-740.