473,765 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compile Time Error Checking?

I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!

Sep 4 '07 #1
11 4275
On Sep 4, 9:07 am, Bryan Crouse <crou...@ieee.o rgwrote:
I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!
Whoops... An article on compile time assertions can be found here:
http://www.ddj.com/architect/184401873

Sep 4 '07 #2
Bryan Crouse wrote:
I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!
Say the serial number has 9 positions:

char SerialNumnber[] = "12345678";

Then, declare an array of 1 position like this

int m[sizeof(SerialNu mber) == 9];

If sizeof(SerialNu mber) != 8 the expression will yield
zero, and you can't declare an array of negative
size or zero.

--------------------------------------------

I hope it works!
Sep 4 '07 #3
Bryan Crouse wrote On 09/04/07 10:07,:
I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.
Here's one horrid hack:

char serial[] = "..."; /* should be 42 characters */

/* If the following line produces an error, it means
* that `serial' (above) does not have the expected
* length. Pay no attention to the text of the error
* message the compiler issues; the problem is with
* the definition of `serial'.
*/
static char fake[ (sizeof serial == 42 + 1) * 2 - 1 ];

If the serial number is indeed 42 characters long (plus one
for the trailing '\0'), fake[1] is a legal array declaration.
If the length is something other than 42, you get fake[-1] and
an error message.

It seems to me, though, that you're attacking the problem
at the wrong place. This hack can check the length and can
maybe be extended to check a few other things, but it's not
going to be easy (or maintainable) to get more thorough
validation from it. If you've got a rule like "The first
two characters are upper-case letters, followed by five
digits and three letters or by six digits and two letters,
followed by ..." then this technique will be far more trouble
than it's worth. Instead, consider arranging your build
procedure so the serial number gets validated by a program
which then runs the build using that number. (For example,
it might write the validated number to a small .c file that
then gets compiled and linked in with the rest, or it might
compile everything with a `-DSERIAL=AB1234Z X999' option, or
something of that sort.) I think you'll find this more
reliable than telling the builders "Edit the file serial.c
and then rebuild the product. Be sure no one else is trying
to build it at the same time ..."

--
Er*********@sun .com
Sep 4 '07 #4
jacob navia wrote, On 04/09/07 15:28:
Bryan Crouse wrote:
>I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!

Say the serial number has 9 positions:

char SerialNumnber[] = "12345678";

Then, declare an array of 1 position like this

int m[sizeof(SerialNu mber) == 9];

If sizeof(SerialNu mber) != 8 the expression will yield
zero, and you can't declare an array of negative
size or zero.

--------------------------------------------

I hope it works!
It doesn't work and does not do what you claim. It won't error if the
string is too long, only if it is too short. A string being too long can
be just as serious an error. See Eric's post for a solution that will do
what you claimed.
--
Flash Gordon
Sep 4 '07 #5
Flash Gordon wrote:
jacob navia wrote, On 04/09/07 15:28:
>Bryan Crouse wrote:
>>I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!

Say the serial number has 9 positions:

char SerialNumnber[] = "12345678";

Then, declare an array of 1 position like this

int m[sizeof(SerialNu mber) == 9];

If sizeof(SerialNu mber) != 8 the expression will yield
zero, and you can't declare an array of negative
size or zero.

--------------------------------------------

I hope it works!

It doesn't work and does not do what you claim. It won't error if the
string is too long, only if it is too short. A string being too long can
be just as serious an error. See Eric's post for a solution that will do
what you claimed.
I forgot the -1
char SerialNumber[]="123456789" ;

int m[(sizeof(SerialN umber) == 9)-1];
Sep 4 '07 #6
jacob navia wrote, On 04/09/07 19:26:
Flash Gordon wrote:
>jacob navia wrote, On 04/09/07 15:28:
>>Bryan Crouse wrote:
I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!
Say the serial number has 9 positions:

char SerialNumnber[] = "12345678";

Then, declare an array of 1 position like this

int m[sizeof(SerialNu mber) == 9];

If sizeof(SerialNu mber) != 8 the expression will yield
zero, and you can't declare an array of negative
size or zero.

--------------------------------------------

I hope it works!

It doesn't work and does not do what you claim. It won't error if the
string is too long, only if it is too short. A string being too long
can be just as serious an error. See Eric's post for a solution that
will do what you claimed.

I forgot the -1
char SerialNumber[]="123456789" ;

int m[(sizeof(SerialN umber) == 9)-1];
Actually, I misread your code and reported it as wrong for the wrong reason.
--
Flash Gordon
Sep 4 '07 #7
Flash Gordon wrote On 09/04/07 13:53,:
jacob navia wrote, On 04/09/07 15:28:
>>Bryan Crouse wrote:
>>>I am looking a way to do error checking on a string at compile time,
and if the string isn't the correct length have then have the compiler
throw an error.

I am working an embedded software that will require individual builds
for each device so that the device serial number is contained in the
program memory. To do this, the C application must be compiled with
the serial number assigned to a variable within the source code file.
I would like to provide compile time error checking within the .c file
if possible so that if the length of the string is not correct, then
the build process will fail and there is no risk of having an
executable that has a bad serial number.

Has anyone heard of this or done this sort of thing? Any advice would
be greatly appreciated.

Thanks!

Say the serial number has 9 positions:

char SerialNumnber[] = "12345678";

Then, declare an array of 1 position like this

int m[sizeof(SerialNu mber) == 9];

If sizeof(SerialNu mber) != 8 the expression will yield
zero, and you can't declare an array of negative
size or zero.

--------------------------------------------

I hope it works!


It doesn't work and does not do what you claim. It won't error if the
string is too long, only if it is too short. A string being too long can
be just as serious an error. See Eric's post for a solution that will do
what you claimed.
I see a minor typo in Jacob's solution (oscillating
between 8 and 9, or between size and length), but no more.
The idea is the same in both cases. I see no basis for
claiming that Jacob's method fails on too-long strings.

The only serious difference I see is that Jacob's
error-provoker is a [0] dimension and mine is a [-1].
Both are illegal and will produce diagnostics from a
conforming compiler. Mine also attempts to arouse the
ire of a non-conforming compiler (see the recent thread
"memcpy() where assignment would do?" for a report of
a compiler that did not complain about a [0] dimension).

Now that I think of it, there's another difference:
I made the array static to ensure that it could not be a
C99-style variable-length array. I don't use VLA's and
am not conversant with the rules, so I made sure to avoid
them just in case a zero-element VLA is in fact legal, or
is an error that wouldn't crop up until run-time.

--
Er*********@sun .com
Sep 4 '07 #8
Eric Sosman wrote, On 04/09/07 21:51:
Flash Gordon wrote On 09/04/07 13:53,:
>jacob navia wrote, On 04/09/07 15:28:
<snip>
I see a minor typo in Jacob's solution (oscillating
between 8 and 9, or between size and length), but no more.
The idea is the same in both cases. I see no basis for
claiming that Jacob's method fails on too-long strings.
It's not my day. I spotted that I had misread Jacob's code when I saw
his correction and posted to that effect.
--
Flash Gordon
Sep 4 '07 #9
On Sep 5, 8:51 am, Eric Sosman <Eric.Sos...@su n.comwrote:
jacob navia wrote, On 04/09/07 15:28:
>int m[sizeof(SerialNu mber) == 9];

Now that I think of it, there's another difference:
I made the array static to ensure that it could not be a
C99-style variable-length array. I don't use VLA's and
am not conversant with the rules, so I made sure to avoid
them just in case a zero-element VLA is in fact legal, or
is an error that wouldn't crop up until run-time.
These issues can be avoided by making the array
a typedef (this also prevents dumb compilers from
actually wasting memory for the array).

Sep 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3331
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
17
3132
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the absolute address of alpha to access to it. What confuses me is that when the variable is dynamically allocated, how does the compiler implement it? We know the address of the variable until run-time. During the compilation, how can we access to the...
2
3965
by: Abhishek Saksena | last post by:
Is it possible using Boost mpl library:- Assume any class implementing a function "connect" with two arugments of fixed types class protocol1 { connect(T0 & t0, T1 &t1 ){..} //fixed types T0 and T1 };
22
4918
by: Qopit | last post by:
Hi there, I'm pretty new to Python and am trying to figure out how to get "will this code compile?"-like code checking. To me this is a pretty basic language/environment requirement, especially when working with large projects. It is *much* better to catch errors at "compile-time" rather than at run-time. One thing I've "found" is the PyChecker module (conveniently embedded in SPE), but it doesn't seem to do that great of a job. ...
2
4645
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a potential issue. For example, in the assembly I would like to add a constraint that states a particular property member of the class can not be equal to one other property. In standard coding I can throw an exception during run-time, but I would rather...
4
2944
by: Dave Rahardja | last post by:
I have the following program that uses an array of chars to simulate a bit set: --------- // An out-of-bounds exception class BoundsException {}; template <int bits = 1> class Bitset
14
2589
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one struct is larger than another struct, etc. I think of something like #define CHECK(expr) static int dummy CHECK(sizeof(struct foo) % 4 == 0);
12
2766
by: Ioannis Vranos | last post by:
Perhaps a mechanism can be introduced in the C++0x/1x standard, something simple like defining a function as: void somefunc(void) throw() { // ... }
2
6361
by: akhilesh.noida | last post by:
I am trying to compile glibc-2.5 for ARM based board. But I am getting errors while configuring it. Please check and give your inputs for resolving this. configure command : $ ../glibc-2.5/configure --prefix=/mnt/new/Mars/glibc_HQ_test/GLIBC/ install/ --with-__thread --enable-kernel=2.6.11 --enable-shared
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5276
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3924
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.