C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Prevent objects being passed to sprintf?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
jf
Guest





PostPosted: Thu Dec 08, 2005 12:47 pm    Post subject: Prevent objects being passed to sprintf? Reply with quote



Is there anything I can do in a class implementation to stop people from
passing an object of that class to sprintf in it's variable argument list?
A compile warning would be better than nothing, but passing objects in a
variable parameter list just seems to bypass any type checking. If I could
just force an overloaded operator to be called, for example.

I know we shouldn't be using sprintf anymore, but in practice it does get
used occasionally.

Thanks,
JF

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron Natalie
Guest





PostPosted: Thu Dec 08, 2005 4:16 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote



jf wrote:
Quote:
Is there anything I can do in a class implementation to stop people from
passing an object of that class to sprintf in it's variable argument list?

No, unfortunately not. It would be nice if there was an operator... to
allow you to either prohibit this or to provide a conversion to some
type that is legal for vararg'd functions, but it doesn't exist.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Pete Becker
Guest





PostPosted: Thu Dec 08, 2005 6:19 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote



jf wrote:

Quote:
Is there anything I can do in a class implementation to stop people from
passing an object of that class to sprintf in it's variable argument list?


Remove its copy constructor.

Personally, this is on my list of things not to worry about.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Hrayr BABAJANYAN
Guest





PostPosted: Thu Dec 08, 2005 8:13 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

Hi!

The '...' (ellipsis) is not an operator, so it cannot be overloaded.
The only operator that can be overloaded in this case is the ','
(comma) operator, one can make it in a way to produce a warning, but
this will affect not only the sprintf call...
So this solution is pretty silly... :)

I think the use of ellipsis destroys all the type checking stuff.
So there is no way do what you want.

Cheers!


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Rob
Guest





PostPosted: Thu Dec 08, 2005 11:47 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

Pete Becker wrote:

Quote:
jf wrote:

Is there anything I can do in a class implementation to stop people
from passing an object of that class to sprintf in it's variable
argument list?


Remove its copy constructor.

I'm not sure if that is actually a solution to the probkem.
Unfortunately, even if it is, it has a few additional side-effects that
may be undesirable .....

Quote:

Personally, this is on my list of things not to worry about.

I agree. If someone is passing an object to sprintf(), there are
almost certainly other problems in their code which will be much more
significant.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dietmar Kuehl
Guest





PostPosted: Fri Dec 09, 2005 10:50 am    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

Hrayr BABAJANYAN wrote:
Quote:
The only operator that can be overloaded in this case is the ','
(comma) operator,

.... and even this wouldn't help: the comma operator is not considered
were it could be confused with the comma separating function arguments.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rob
Guest





PostPosted: Fri Dec 09, 2005 10:58 am    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

Pete Becker wrote:

Quote:
jf wrote:

Is there anything I can do in a class implementation to stop people
from passing an object of that class to sprintf in it's variable
argument list?


Remove its copy constructor.

I'm not sure if that is actually a solution to the probkem.
Unfortunately, even if it is, it has a few additional side-effects that
may be undesirable .....

Quote:

Personally, this is on my list of things not to worry about.

I agree. If someone is passing an object to sprintf(), there are
almost certainly other problems in their code which will be much more
significant.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
jf
Guest





PostPosted: Fri Dec 09, 2005 11:01 am    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

In article <4398ae25 (AT) duster (DOT) adelaide.on.net>, [email]nospam (AT) nonexistant (DOT) com[/email] (Rob)
wrote:

Quote:
I agree. If someone is passing an object to sprintf(), there are
almost certainly other problems in their code which will be much more
significant.

Sometimes it can be just a typo, passing a string instead of the return
from it's c_str() method. Especially when integrating with older code.
It's very irritating that in such a type safe environment, this can slip
through without even a compiler warning.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
antonmuhin
Guest





PostPosted: Fri Dec 09, 2005 3:15 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

I'm not sure that it might be finished as an expected solution, but
maybe something along this lines will do (especially, if you don't use
sprintf often).

Redefine sprintf as a macro to your own function. As was pointed out
one cannot control vargs function, but you can produce enougth number
of overloads to match all the cases (and maybe add vargs overload to
produce compile time warning, if used). These overloads can be shaped
as below:

template int mySprintf(char const * fmt, T1 p1, T2) {
compile-time check for T1 --- quite easy with template magic
compile-time check for T2
call to old sprintf
}

Of course, you might want a script to generate these overloads.

hth,
anton.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
kanze
Guest





PostPosted: Fri Dec 09, 2005 3:59 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

jf wrote:

Quote:
Is there anything I can do in a class implementation to stop
people from passing an object of that class to sprintf in it's
variable argument list?

If by object, you mean an object of class type, I don't
understand the problem. According to the standard, "if the
argument has a non-POD class type, the behvior is undefined.",
and I don't think I've ever seen a compiler which didn't
generate a warning (although I'll admit, it's not something that
I verify very often).

Quote:
A compile warning would be better than nothing, but passing
objects in a variable parameter list just seems to bypass any
type checking. If I could just force an overloaded operator
to be called, for example.

I know we shouldn't be using sprintf anymore, but in practice
it does get used occasionally.

In practice, you probably never should have been using sprintf.
Like all functions which write to a buffer without being passed
the buffer's length, it is an invitation to buffer overrun
problems. In the case of sprintf, there are ways of avoiding
them, but they are all so complicated that it isn't worth the
bother.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Paavo Helde
Guest





PostPosted: Sat Dec 10, 2005 12:54 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

[email]hotmail (AT) hotmail (DOT) com[/email] (jf) wrote in
news:memo.20051209093842.2864A (AT) crabtree (DOT) gmail.com:

Quote:
Sometimes it can be just a typo, passing a string instead of the
return from it's c_str() method. Especially when integrating with
older code. It's very irritating that in such a type safe environment,
this can slip through without even a compiler warning.

So couldn't you just compile your code with g++ -Wall? Even if you are not
targeting Linux, this might be a good catch for this and possibly for other
errors as well.

hth
Paavo

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
jf
Guest





PostPosted: Mon Dec 12, 2005 4:54 pm    Post subject: Re: Prevent objects being passed to sprintf? Reply with quote

In article <Xns97281743D8782paavo256 (AT) 194 (DOT) 126.101.124>, [email]nobody (AT) ebi (DOT) ee[/email]
(Paavo Helde) wrote:

Quote:
So couldn't you just compile your code with g++ -Wall? Even if you are
not
targeting Linux, this might be a good catch for this and possibly for
other
errors as well.

I'm sure we could use some other tool (g++ in this case) to detect these
types of problems, but I was after something that I could do in class
design to avoid the problem in the first place.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
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, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.