View previous topic :: View next topic |
Author |
Message |
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
String limit on a define? |
Posted: Tue May 31, 2016 1:06 pm |
|
|
There's a way to limit the size of a string when is defined?
The scenario is as follow, I have a code(program) that are send to a production environment, they customize some #define on a .h file and compile, until now nothing has happened, but now I wondering how to limit some of those strings to avoid errors.
Can be done at precompiler level?
I don't want to check every string, because those string have not the same size limits and are more than 50 parameters and will use a more ROM/FLASH memory, most of them are strings.
I'm talking about this coin of strings
Code: | #define Name "?????" |
_________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue May 31, 2016 1:19 pm |
|
|
No.
However you could probably limit the output, when it is used. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Tue May 31, 2016 1:48 pm |
|
|
That's what I want to avoid. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Tue May 31, 2016 2:04 pm |
|
|
Why?.
Just have your own output or use function, that accepts a constant string, and a size. Define sizes for your strings, and when they are passed to the function, have it count through the characters, till it sees a NULL, or reaches the size limit.
If you need to have different length limits, you are going to have to have defined sizes. Use them. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Wed Jun 01, 2016 9:27 am |
|
|
I don't have any size defined.
When the PIC start read a specific bytes from an external EEPROM, if those bytes are 0xFF or incongruous, it will go to default configuration that is copied from ROM to the external EEPROM and in that process the subroutine only copy
Code: | MemoTxBuffer[0]=sprintf(&MemoTxBuffer[1],DefaultPassword1);
WriteBufferEE(Password1,MemoTxBuffer,(MemoTxBuffer[0]+1)); |
I have a lot of lines like this one with almost 20 different parameters.
On index zero is saved the size of the string, but if "DefaultPassword" is too long for the buffer the RAM will be corrupted by sprintf function, that's why I don't want to do it whit the µC, I need a precompiler level check. _________________ Electric Blue |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1349
|
|
Posted: Wed Jun 01, 2016 7:46 pm |
|
|
Well I can't give you compile time safety, but I did make a "poor man's" snprintf which gives you runtime safety.
https://www.ccsinfo.com/forum/viewtopic.php?t=53652
You can put the max size as a parameter and the snprintf function won't go any further. It does a return a little different than an sprintf because it returns the number of chars written OR the number of chars it would have written if you had the correct size buffer. You can mod the code to make it just return the number of chars written if you like.
It isn't very efficient (though no printf variation is normally anyways).
I know it isn't exactly what you asked for, but maybe could help.
EDIT: Also, it requires a version 5 compiler for the __VA_ARGS__ extension |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Wed Jun 01, 2016 11:31 pm |
|
|
The other point is that you must have a size defined. Problem is that without a size, how is the compiler or code to know the size is too large?. Telepathy?.
Jeremiah's routine shows a way of implementing what I was talking about for an output routine. |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu Jun 02, 2016 9:43 am |
|
|
Ttelmah wrote: | The other point is that you must have a size defined. Problem is that without a size, how is the compiler or code to know the size is too large?. Telepathy?.
|
Ok, if I have the following define, How I define its maximum size?
Code: | #define Name "?????" |
That's what I'm asking for.
If I write
Code: |
#define NameMaxLen 40
#define Name "?????"
|
They aren't connected, for the precompiler they are two different defines. _________________ Electric Blue |
|
|
E_Blue
Joined: 13 Apr 2011 Posts: 417
|
|
Posted: Thu Jun 02, 2016 9:47 am |
|
|
jeremiah thanks for your answer and code, I'm reading it. _________________ Electric Blue |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19515
|
|
Posted: Fri Jun 03, 2016 1:33 am |
|
|
E_Blue wrote: | Ttelmah wrote: | The other point is that you must have a size defined. Problem is that without a size, how is the compiler or code to know the size is too large?. Telepathy?.
|
Ok, if I have the following define, How I define its maximum size?
Code: | #define Name "?????" |
That's what I'm asking for.
If I write
Code: |
#define NameMaxLen 40
#define Name "?????"
|
They aren't connected, for the precompiler they are two different defines. |
Yes, but you then join them.
So when you use the name, you use it through a 'length limiting' function (like Jeremiah's), with:
snprintf(buffer,NameMaxLen,Name);
Or a similar encapsulation for putc etc..
By using the #defined number (rather than the buffer length as Jermiah shows), you can specify the length for each string if required. |
|
|
|