CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

String limit on a define?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

String limit on a define?
PostPosted: Tue May 31, 2016 1:06 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue May 31, 2016 1:19 pm     Reply with quote

No.

However you could probably limit the output, when it is used.
E_Blue



Joined: 13 Apr 2011
Posts: 417

View user's profile Send private message

PostPosted: Tue May 31, 2016 1:48 pm     Reply with quote

That's what I want to avoid. Laughing
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Tue May 31, 2016 2:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 01, 2016 9:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 01, 2016 7:46 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jun 01, 2016 11:31 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 02, 2016 9:43 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jun 02, 2016 9:47 am     Reply with quote

jeremiah thanks for your answer and code, I'm reading it.
_________________
Electric Blue
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Fri Jun 03, 2016 1:33 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group