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
switch case with no break.
Passing virtual address to the DMA engine was a worst one, not exactly C related, but i assume that 99% of DMA related stuff written in C so it's kind of match. This small error lead to memory corruption that took me 1.5 month to find.
Having been a Lisp programmer, I was used to indenting closing braces, as in:
(cond
((eq a foo)(bar ...
....
))
)
and I carried this into C programming:
if (a == foo){
bar(...);
....
}
Then I got on a good-size project in C, and another programmer had to make changes in the vicinity of my code. He mis-read my closing brackets, and freed some memory too early. This caused an extremely subtle bug that happened at crunch time. When it was found, he got blamed, badly. But you could say it was my fault. That was not fun, to say the least.
The most dangerous thing I ever did in C was trying to write code which managed my own memory. Effectively, this means the most dangerous thing I ever did in C was write C code. (I hear that you can get around it these days. Hip hip for sanity. Use those approaches whenever appropriate!)
And I do not manage memory.
Someone else manages my memory for me -- someone who can design better than I can, and test better than I can, and code better than I can, and patch when they make critical security-compromising mistakes which only get noticed 10 years later because absolutely everyone who attempts to allocate memory fails some of the time.
Two things comes to mind. Ths first was a function in embedded C (MCU) i tried to have some restrictions on an timer value as a entered a function. so I wrote
if(55000 < my_var < 65000)
My ida was to check like this:
if( (55000<my_var) < 65000)
But this is the equivilant or the result
if( (55000<my_var) || (my_var<65000))
and the result ended up that the if test was always true.
The secound was a pointer mistake. (simplyfied presented here)
get_data(BYTE **dataptr)
{
ubyte* data = malloc(10);
... code ...
*dataptr = &data[1];
}
main()
{
BYTE *data
get_data(&data);
free(data);
}
Thus resulting in a loss of 1 byte of memory for each time the get_data()
function was called
Forgot to put ;
at the end.
Excess }
.
Mistakenly typed a ,
These makes me go nuts for hours finding what stuffs have gone wrong with my codes.