2

I am using Renesas V850 series microcontroller in my project. My product uses a Non-Volatile memory blocks(NVRam blocks) location which is separate from the main program section. During runtime, these NVRam blocks are monitored to verify that they are not corrupted. This check is done with a code similar to given below:

Logic 1

if((NULL != pBlock_One_Pointer) &&  (BLOCK_ONE_ID != *(((const tUI8*)pBlock_One_Pointer) + ID_OFFSET))) 
{
.....Do some corrective action....
}

The problem with this code is , if pointer "pBlock_One_Pointer" somehow gets corrupted with value "NULL" , the Block_ID check(2nd portion of "if" statement is not done).

One way to avoid this situation is to remove the first part of "if" condition where it checks the Block_ID irrespective of pointer is "NULL" or not as given below

Logic 2

if (BLOCK_ONE_ID != *(((const tUI8*)pBlock_One_Pointer) + ID_OFFSET))

But if "pBlock_One_Pointer" points to NULL, will it cause a exception ?

So basically I have 2 questions:

  1. Is there a chance that pointer becomes NULL pointer due to some corruption during runtime?
  2. If so , will Logic 2 help me to overcome it?
Mat
  • 202,337
  • 40
  • 393
  • 406
Vivek
  • 59
  • 4
  • Did you notice the issue with formatting? There is a handy "Preview" section when you are editing your question. – Eugene Sh. Dec 19 '17 at 14:50
  • 1
    "pBlock_One_Pointer" somehow gets corrupted with value "NULL" so the `if()` which "Do some corrective action"` should also accept `NULL`. Example `if((NULL == pBlock_One_Pointer) || ...` – chux - Reinstate Monica Dec 19 '17 at 15:18
  • 2
    Re 1, It will likley only erroneously become NULL if your code contains an error that makes it so - there are however probably 4 billion other values it could take that are equally incorrect, more likely and not checked for. The real solution is not to write code that corrupts data, then you have no need to check for corruption. Think about it - you have written broken software, so you are going to write _more_ software to check for the effects of the broken software - what is wrong with this picture!? – Clifford Dec 19 '17 at 16:27

2 Answers2

2

But if "pBlock_One_Pointer" points to NULL, will it cause a exception ?

It causes undefined behavior.

Per 6.5.3.2 Address and indirection operators of the C standard:

The unary * operator denotes indirection. If the operand points to a function, the result is a function designator; if it points to an object, the result is an lvalue designating the object.
If the operand has type ‘‘pointer to type ’’, the result has type ‘‘ type ’’. If an invalid value has been assigned to the pointer, the behavior of the unary * operator is undefined.

So:

Is there a chance that pointer becomes NULL pointer due to some corruption during runtime?

Yes. It's possible.

If so , will Logic 2 help me to overcome it?

No. How could it? The location of the memory you want to check for corruption is lost.

Andrew Henle
  • 32,625
  • 3
  • 24
  • 56
  • 1
    “It causes undefined behavior” is not correct or helpful since the question is not about the C language, but about a specific implementation. The behavior of `*NULL` **on a Renesas V850** is well-defined (perhaps depending on the exact hardware and software configuration). – Gilles 'SO- stop being evil' Dec 20 '17 at 08:09
-1

Is there a chance that pointer becomes NULL pointer due to some corruption during runtime?

Yes, there are several things that could cause this. Software issues such as pointer bugs, runaway code, stack overflow etc. And in addition, failing memory hardware because of data retention, EMI (nowadays less likely) or ambient radiation/cosmic rays.

If so , will Logic 2 help me to overcome it?

No. There are ways to detect corrupt RAM, most commonly CRC checksums.

To detect failing memory hardware, there are other ways like "walking patterns" where you cycle cells by writing 1 and 0 to them at regular intervals. In modern embedded systems however, memory with built-in ECC is used, so that the software need not bother about memory hardware integrity.

Lundin
  • 195,001
  • 40
  • 254
  • 396