Security of using \ 0 to the end of character arrays

I am writing a driver for an embedded system that launches a custom version of modified linux (this is manual work). The manufacturer provides a specialized Eclipse Juno distribution with several libraries and inbound examples.

The result that I get from the compiler comes in the form of a standard array of characters. I use individual characters in the array to convey information (error identifiers and error codes) as follows:

if (tmp[i] == 250]) 

Where tmp is an array of characters in the form char tmp[500]; , which is first initialized to 0 , and then populated with input from comport.

My question is:

Assuming I repeat every part of the array, is it possible to use 0 (as in \0 ) at any point to the end of the array? I assume that I:

  • Do not treat it as a string (iterate and use it as an int array)
  • In the knowledge of what will be there, and what exactly this random \ 0 in the middle of it should mean.

The reason I ask is because I had several employees who told me that they should never ever use an array of characters containing \0 to the end, regardless of the circumstances.

My code executing this now is executing as expected, but not sure if this might occur later.

Rewriting it to avoid such behavior would be a non-trivial part of the job.

+5
source share
1 answer

Using a char array as an array of small integers is excellent. Just be careful not to pass it to any function that expects "strings".

And if you want to be more frank in this question, and also make sure the array uses unsigned char , you can use uint8_t .

+7
source

Source: https://habr.com/ru/post/1235696/


All Articles