Arunkumar Khannur's Software Testing Knowledge Center

8.4. Common Bugs

Every error, either it is defect or a bug, has solution at code level. It is good to know and address as bugs as possible at code level. This section lists out some common bugs that occur in code. Programmers and also, code reviewer can understand these possibilities in order to improve code.
8.4.1. Bugs with Pointers and Memory

The following are various possible bugs in dealing with pointers and memory.

Memory leaks
A memory leak is a situation, where the memory is allocated to the program which is not freed subsequently. Such a situation can cause ever increasing usage of memory and resulting in an exceptional halt of program because of the lack of free memory.

Temporary values returned
Dynamic allocation of data is very crucial in order to store data in heap else it will be stored in the stack. In order to share data it is good to declare variable as public and use that access common data.

Freeing the already freed resource
Memory is first allocated and de-allocated. The programmer tries to free the already freed resource. This is a common form of error.

NULL dereferencing
Improper initialization, missing the initialization in different paths, or aliases leads to the NULL reference error.

Exposure of private data to un-trusted components
In many situations there is a need to preserve the integrity and security of data and access to external sources shall not be given. This can be achieved with usage of private or protected options to restrict access to data. However programmers with lesser experience are not good at using private or protected options.

8.4.2. Aliases
When there is an unexpected aliasing between parameters, return values, and global variables, errors may be inevitable. Aliasing problems sometimes lead to deallocation errors. Static analysis of all feasible paths in the program can detect possible aliases.

Need of Unique addresses
Aliasing may result in violation of unique addresses when we expect different addresses.

8.4.3. Synchronization Errors
Synchronization errors are hard to find errors with major impact and may occur where multiple threads are accessing some common resources. Synchronization errors are of three types: Deadlocks, Race conditions, and Live lock.

Deadlock is a situation in which one or more threads mutually lock each other, more frequently because of inconsistent locking sequence. In order to detect deadlock situation, we can construct a lock graph, analyze if it has a loop and if loop exists then it represents the presence of a deadlock.

Race Condition
This is an error which results when two threads try to access the same resource and the result depends on the order of the execution of the threads.

Inconsistent synchronization
Error related to inconsistent synchronization may happen because of mix of locked and unlocked accesses in shared variable where some are locked accesses and some other accesses are unlocked.

Incorrect initialization of static field
During synchronization, connection and release semantics are established by initializing a volatile static field. If a non-volatile field that is shared by different threads are improperly initialized then there can be a synchronization problem.

Method spins on field
When a method reads a field, it enters into an infinite loop causing improper synchronization.

8.4.4. Data and Arithmetic Errors
Un-initialized memory
If initialization is not proper after declaration, some possible scenarios and paths may not be executed. Such cases impose problems like un-initialized memory.

Value outside the domain
Typically in type conversions, we may get unexpected result when by mistake we have initialization a variable with a value which is not inside its range.

Buffer overflow/underflow
When code puts data into buffer without having any kind of checking, we may arrive at Buffer Overflow error. A simple buffer overflow is capable of putting the security of the system at risk. It can cause following vulnerabilities like- attacking Stack- Modify the return address to execute malicious code; Pointer overriding to get data on a specific location; an array index out of bounds; heap overflows attack.

Arithmetic exceptions
Arithmetic exceptions sometimes are incomputable resulting in this class of errors. Some examples are: divide by zero, floating point exceptions etc.

Off by one
When loops are under-initialized or over-initialized, or > is written instead of >= etc, we may get these errors.

Enumerated data types
The values of enumerated data types when being used with no care, like indexing an array, may cause errors.

Wrong assumptions on operator precedence
This is one of the commonly occurring errors. When design documents or program specifications do not mention the formula but provide narration, programmer’s may use formula based on their knowledge. Example, in banking application when designers mention “Compute simple rate of interest”, programmer may assume I= (P*T*R) /100 based on his primary school mathematical knowledge. However banks use different ways to calculate rate of interest. This error is more dependent on the programmer’s logic in coding. Also, while writing large and complicated expressions, errors may occur because of precedence rules of the programming language.

Undefined order of side effects
In a program, if too many global variables are used, there is a possibility of side effects. When error is reported in one unit, if it is being addressed there can be another error in some other unit.

String handling errors
While handling strings, string handling functions may fail because the operands may be having NULL. Or operands may not be NULL terminated resulting in errors like buffer overflow
Khannur's Book
Arunkumar Khannur, Software Testing - Techniques and Applications, Published by Pearson Publications, 2011 (ISBN:978-81-317-5836-6; Pages:341 + xxii)
Follow Khannur
Khannur's Company
ISQT Process & Consulting Services Pvt. Ltd., Bangalore, INDIA
Khannur's Software Testing Forum
 Contact Khannur
ISQT Process & Consulting Services Pvt. Ltd.
#732, 1st Floor, 12th Main,
3rd Block, Rajajinagar,
Bangalore - 560010, INDIA
Phone: +91 80 23012511
Skype: arun.isqt