cidr.lo -MD -MP -MF .deps/cidr.Tpo -c cidr.cpp -fPIC -DPIC -o .libs/cidr.o
cidr.cpp: In member function 'void ost::IPV4Cidr::set(const char*)':
cidr.cpp:201: error: invalid conversion from 'const char*' to 'char*'
cidr.cpp: In member function 'void ost::IPV6Cidr::set(const char*)':
cidr.cpp:329: error: invalid conversion from 'const char*' to 'char*'
make[2]: *** [cidr.lo] Error 1
make[2]: Leaving directory `/build/src/commoncpp2-1.6.2/src'
The problem is the line : ep = strchr(cp, '/');
because strchr is defined in cstring as returning a const char*
if I make ep a const then it fails on this line: *ep = 0;
Can some one help me with a fix?
void IPV4Cidr::set(const char *cp)
{
char cbuf[INET_IPV4_ADDRESS_SIZE];
char *ep;
unsigned dots = 0;
#ifdef WIN32
DWORD addr;
#endif
memset(&netmask, 0, sizeof(netmask));
bitset((bit_t *)&netmask, getMask(cp));
setString(cbuf, sizeof(cbuf), cp);
ep = strchr(cp, '/');
if(ep)
*ep = 0;
cp = cbuf;
while(NULL != (cp = strchr(cp, '.'))) {
++dots;
++cp;
}
while(dots++ < 3)
addString(cbuf, sizeof(cbuf), ".0");
#ifdef WIN32
addr = inet_addr(cbuf);
memcpy(&network, &addr, sizeof(network));
#else
inet_aton(cbuf, &network);
#endif
bitmask((bit_t *)&network, (bit_t *)&netmask, sizeof(network));
}
Little to do with app development under Linux, this is a plain C++ error.
You are trying to use a pointer to a const char in a place where a non-const
pointer is needed.
> The problem is the line : ep = strchr(cp, '/');
> because strchr is defined in cstring as returning a const char*
Okay, first thing to do is to isolate a testcase. Your code contains lots of
stuff that is not necessary to reproduce the problem. This is useful to
understand the core of the problem and common courtesy on the Usenet.
> if I make ep a const then it fails on this line: *ep = 0;
>
> Can some one help me with a fix?
>
> void IPV4Cidr::set(const char *cp)
This declaration promises not to modify what 'cp' points to...
> ep = strchr(cp, '/');
> if(ep)
> *ep = 0;
...this code breaks that contract.
Sorry, there is nothing that can fix this safe a redesign. Modifying input
parameters is simply wrong. Without further info what the function should do
and why you suddenly think you need to modify input parameters it is
impossible to say what is wrong. My best guess is that you need a substring
of the original input string, which is achieved by simply copying it.
Uli
> The problem is the line : ep = strchr(cp, '/');
> because strchr is defined in cstring as returning a const char*
Right.
> if I make ep a const then it fails on this line: *ep = 0;
Exactly.
> Can some one help me with a fix?
>
void IPV4Cidr::set(const char *ccp)
> {
> char cbuf[INET_IPV4_ADDRESS_SIZE];
char *ep, *cp;
> unsigned dots = 0;
> #ifdef WIN32
> DWORD addr;
> #endif
cp=strdup(ccp);
>
> memset(&netmask, 0, sizeof(netmask));
> bitset((bit_t *)&netmask, getMask(cp));
> setString(cbuf, sizeof(cbuf), cp);
> ep = strchr(cp, '/');
> if(ep)
> *ep = 0;
>
> cp = cbuf;
> while(NULL != (cp = strchr(cp, '.'))) {
> ++dots;
> ++cp;
> }
>
> while(dots++ < 3)
> addString(cbuf, sizeof(cbuf), ".0");
>
> #ifdef WIN32
> addr = inet_addr(cbuf);
> memcpy(&network, &addr, sizeof(network));
> #else
> inet_aton(cbuf, &network);
> #endif
> bitmask((bit_t *)&network, (bit_t *)&netmask, sizeof(network));
free(ccp);
> }
You need to modify the input parameter, but you can't because you
promised not to. So you need to make a copy, modify that, and then
free it.
DS