[Qt-interest] bug Qt 4.6-stable in qstrncpy
Sylvain Pointeau
sylvain.pointeau at gmail.com
Sun Oct 11 16:09:30 CEST 2009
Hi,
reading again the doc, I think we can understand both ways...
"Copies at most len bytes from src"
-> then I understand you copy len bytes from the src to the dest, right?
"Guarantees that dst is '\0'-terminated"
-> cool, that's why I had been interested to use it.
"This function assumes that dst is at least len characters long."
-> ouch, this function is guarantee to be '\0'-terminated, it copies len
chars from the src,
but the dst size is only "len"... it doesn't make sense, right?
I think you should have len = number of bytes copied
and dst to have at least len+1 chars to put the '\0'
and if you still say that this is intentional,
at least modify the code to copy only len-1 chars from the src:
#else
strncpy(dst, src, *len-1*);
#endif
Cheers,
Sylvain
On Sun, Oct 11, 2009 at 3:53 PM, Sylvain Pointeau <
sylvain.pointeau at gmail.com> wrote:
> Hi,
> I think you are wrong.
>
> extract from the qt documentation:
>
> char * qstrncpy ( char * dst, const char * src, uint len )
> A safe strncpy() function.
> Copies at most len bytes from src (stopping at len or the terminating '\0'
> whichever comes first) into dst and returns a pointer to dst. Guarantees
> that dst is '\0'-terminated. If src or dst is 0, returns 0 immediately.
> This function assumes that dst is at least len characters long.
> beside :-) I use it for making a glue between C++ code and C code...
>
> Cheers,
> Sylvain
>
>
> On Sun, Oct 11, 2009 at 3:34 PM, Thiago Macieira <thiago at kde.org> wrote:
>
>> Em Domingo 11. Outubro 2009, às 15.04.10, Sylvain Pointeau escreveu:
>> > I found a mistake in qstrncpy
>> > it copies only the len-1 bytes
>> > or more exactly it finishes by putting a 0 at len-1 which is obviously
>> > wrong.
>>
>> That is intentional, so it's not a bug. The last parameter is not the
>> length
>> of the string you want to copy, but the size of the destination buffer.
>>
>> const char *original = "This is a very long string";
>> char buf[5];
>>
>> qstrncpy(buf, original, sizeof buf);
>>
>> Result: buf contains "This";
>>
>> Besides, why are you using this function?
>> --
>> Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>> Senior Product Manager - Nokia, Qt Development Frameworks
>> PGP/GPG: 0x6EF45358; fingerprint:
>> E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
>>
>> Qt Developer Days 2009 | Registration Now Open!
>> Munich, Germany: Oct 12 - 14 San Francisco, California: Nov 2 - 4
>> http://qt.nokia.com/qtdevdays2009
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091011/bb3d05fb/attachment.html
More information about the Qt-interest-old
mailing list