[great-users] Small bug - SetStrValue is not checked...

Attila Vizhanyo viza at isis.vanderbilt.edu
Wed Apr 7 18:23:19 CDT 2004


Hi All,

The implicit conversion of C const char pointers to C++ strings
has been approved by the C++ Standard Committee for C
compatibility reasons: i.e. they wanted to allow programmers change
their legacy function signatures from "const char*" to "const
std::string&"
without the imposition of updating the char array string literals on the
calling side.  
Since then many people don't agree with this decision, because if a
caller
supply 0 for a function argument of type "const std::string&", compilers
will silently perform the type conversion. 
Other people say, if you inadvertently call the function with zero,
it is the same kind of heresy, as calling the function that takes 
"const char*", consequently you, the caller, are responsible for this
error,
and you will get your runtime exception for your careless actions.

The problem actually roots in the problem that there is no unambiguous
notion of NULL in many C++ libraries; they are simply a macro for 0,
consequently the type-safety is lost. However, even then if there were
some foolproof NULL available, callers could still omit it, and go with
the numeric constant 0. (Note, that you cannot pass any other number
(1,2,1000), or other character literal 'a', because there is no implicit
conversion from these to pointers!) 

The bottom line is that the only solution to protect from this error is
the introduction of an user type for representing Udm strings, and do
not allow the implicit type conversion from "const char*". You loose
some convenience in use, but gain the advantage of turning a runtime
error into a compile time error.

-Attila



-----Original Message-----
From: Gabor Karsai 
Sent: Wednesday, April 07, 2004 15:03
To: great-users
Subject: RE: [great-users] Small bug - SetStrValue is not checked...

Tivadar,

How about the "1-step silent type conversion" feature of C++?
I'm sure that "string" has a constructor "string(const char*)" and 0
type-matches const char*.
Try this:
#include <string>

void foo(const std::string & clunk)
{
	std::string me = clunk;
}

void bar()
{
	foo(0);
}

Cheers,
-- Gabor
> -----Original Message-----
> From: Tivadar Szemethy 
> Sent: Wednesday, April 07, 2004 1:45 PM
> To: great-users
> Subject: RE: [great-users] Small bug - SetStrValue is not checked...
> 
> 
> > I'm using the officially unreleased version of GReAT. I was not
> careful
> > enough and tried the following thing in an attribute mapping:
> > 
> > <something>.SetStrVale("something", 0);
> > 
> > Obviously it should be:
> > 
> > <something>.SetStrVale("something", "0");
> > 
> > No errors are reported during configuration and the 
> execution of this 
> > rule crashes GME.
> 
> This is kind of strange. SetStrValue has signature
> bool SetStrValue(string strAttrName, const string& value);
> 
> and the compiler MUST NOT accept an integer zero for &value, 
> since 0 cannot be cast into a string reference.
> 
> Even if it was not a zero, you should get some kind of error 
> unless you cast explicitly, and even so you have to figure 
> out a trikcy cast to outwit the compiler. If I try something 
> similar, I get the expected error:
> 
> Gz__SMOLES2UPPAAL-gr.cpp(37) : error C2664: 'GetStrValue' : 
> cannot convert parameter 2 from 'const int' to 'class 
> _STL::basic_string<char,struct std::char_traits<char>,class 
> _STL::allocator<char> > &'
>         A reference that is not to 'const' cannot be bound to 
> a non-lvalue NMAKE : fatal error U1077: 'cl.exe' : return code '0x2'
> 
> 
> >[Adi]
> >The AttributeMapping and Guard code is compiled into a dll by GRE and
> >called at run time. Since the code you wrote is valid C++ code it is
> >successfully compiled and linked. In your case it's a semantic error
> >that even the C++ compiler cannot catch.
> 
> I disagree, see above.
> 
> Tivadar
> 
> _______________________________________________
> great-users mailing list
> great-users at list.isis.vanderbilt.edu
> http://list.isis.vanderbilt.edu/mailman/listinfo/great-users
> 
_______________________________________________
great-users mailing list
great-users at list.isis.vanderbilt.edu
http://list.isis.vanderbilt.edu/mailman/listinfo/great-users


More information about the great-users mailing list