[Kde-bindings] Fwd: Qyoto/Kimono: A 64 bit unsafe function, build trouble on Debian unstable

Richard Dale rdale at foton.es
Wed May 23 10:57:49 UTC 2007


On Tuesday 22 May 2007, Arno Rehn wrote:
> > [  2%] Building CXX object CMakeFiles/qyoto.dir/src/qyoto.o
> > /home/kaare/source/qyoto/src/qyoto.cpp: In function 'long int
> > QyotoHash(void*)':
> > /home/kaare/source/qyoto/src/qyoto.cpp:1706: error: cast from 'void*' to
> > 'int' loses precision
> > make[2]: *** [CMakeFiles/qyoto.dir/src/qyoto.o] Error 1
> > make[1]: *** [CMakeFiles/qyoto.dir/all] Error 2
> > make: *** [all] Error 2
> >
> > To solve the above problem, I changed the two instances of "return (int)"
> > to "return (long)" in the QyotoHash() function.  However, I haven't
> > examined whether this change is correct, as I have next to no C++
> > experience.
>
> Ah yes, it seems like we have to add a check whether void* is equal to long
> or int.

int 
QyotoHash(void * obj)
{
	smokeqyoto_object *o = (smokeqyoto_object*) (*GetSmokeObject)(obj);
	(*FreeGCHandle)(obj);

	if (sizeof(void*) > sizeof(int)) {
		qint64 key = (qint64) o->ptr;
		return (int) ((key >> (8 * sizeof(int) - 1)) ^ key);
	} else {
		return (int) o->ptr;
	}
}

The code will work at runtime because on an EMT64T machine the test 'if 
(sizeof(void*) > sizeof(int))' will be true, and it won't go to the else part 
of the branch that the compiler is complaining about. So perhaps it should be 
some kind of macro instead where a similar condition is tested at compile 
time. Or can the cast be changed to keep the 64 bit compiler happy? It 
shouldn't be a long though - we want to return a 32 bit int on both 32 bit 
and 64 bit machines.

-- Richard



More information about the Kde-bindings mailing list