[Qt-interest] QString::fromAscii can run past end of string

Arnold Krille arnold at arnoldarts.de
Wed Apr 14 08:32:43 CEST 2010


Hi,

On Wednesday 14 April 2010 06:40:44 Bob Babcock wrote:
> What should QString::fromAscii do if I specify a length longer than the
> ascii string?  I expected it to stop at the end of the string, but that
> doesn't seem to be the case.  Example:
> 	char test[] = "This is a test";
> 	test[4] = 0;
> 	QString qtest = QString::fromAscii(test,14);
> 	QMessageBox::information(this, tr("Debug"), qtest);
> The QMessageBox displays "This is a test" with the first blank replaced by
> a box character.  The documentation doesn't actually say what should happen
> in this case, but I expected to just see "This" because the docs say const
> char * parameters are interpreted as classic C-style '\0'-terminated
> strings.  (I ran into this trying to use size as a maximum length.)

As the docs state rather clearly, if you give the size-parameter it reads size 
characters from the ascii-string. If you omit that parameter, it looks for the 
strlen() which is normally determined by the first \0-character...

Arnold
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100414/0877047c/attachment.bin 


More information about the Qt-interest-old mailing list