Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What was the most dangerous programming mistake you have made in C?

Tags:

c

I am an intermediate C programmer. If you have made any coding mistake that you came to know later that it was the most hazardous / harmful to the total application please share that code or description. I want to know this because in future I may come across such situations and I want to have your advice to avoid such mistakes.

like image 480
Manoj Doubts Avatar asked Nov 12 '08 06:11

Manoj Doubts


People also ask

What is the mistake in C?

Errors are the problems or the faults that occur in the program, which makes the behavior of the program abnormal, and experienced developers can also make these faults. Programming errors are also known as the bugs or faults, and the process of removing these bugs is known as debugging.

What is the most dangerous programming language?

C++ and PHP have far more high-severity security flaws than programming languages like JavaScript and Python.


3 Answers

if (c = 1) // insert code here
like image 142
Daniel Kreiseder Avatar answered Nov 16 '22 03:11

Daniel Kreiseder


if(a == true);
{
  //Do sth when it is true. But it is allways executed.
}

Edit: Another variant of the same mistake.

for(i=0; i<max_iterations;i++);
{
  //Do sth but unexpectedly only once
}
like image 30
Fernando Miguélez Avatar answered Nov 16 '22 04:11

Fernando Miguélez


Few years ago I've got a call from my ex-colleague telling me about the problem he had to fix with my code, which was a router for credit card transactions.

Card number prefix consists of 6-digit BIN (Bank Identification Number) and extra few digits that banks use at own discretion, e.g. bank has BIN for Visa Classic card 456789, and reserve 2 extra digits to indicate sub-product, like 01 for student's card, 02 for co-branded card with local department store and so on. In this case card prefix, which is basically product identifier, becomes 8 digits long. When I coded this part, I decided that 9 digits "ought to be enough for everyone". It was running ok for 2 years until one day bank make a new card products with 10-digit-long prefix (have no idea why they needed it). Not too hard to imagine what has happened - router segfaulted, the whole system halted because it cannot function without transaction router, all ATMs of that bank (one of the biggest in the country) became non-operational for few hours, until problem was found and fixed.

I cannot post the code here firstly because I don't have it and secondly it is copyrighted by the company, but it is not hard to imagine the strcpy() without checking size of target buffer.

Just like man strcpy says:

If the destination string of a strcpy() is not large enough (that is, if the programmer was stupid or lazy, and failed to check the size before copying) then anything might happen. Overflowing fixed length strings is a favorite cracker technique.

I was very embarrassed. But I've learned the lesson and do not forget (usually :) ) to check size of target buffer. I wouldn't recommend you to learn it the hard way - just develop a habit to check target buffer before strcpy() and strcat().

Edit: good suggestion from Healthcarel - use strncpy() rather than strcpy(). It doesn't add trailing 0 but I usually use following macro to get around it:

#define STRNCPY(A,B,C) do {strncpy(A,B,C); A[C] = 0; } while (0)

like image 25
qrdl Avatar answered Nov 16 '22 04:11

qrdl