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

Thiago Macieira thiago at kde.org
Wed Apr 14 08:52:42 CEST 2010


Em Quarta-feira 14. Abril 2010, às 06.40.44, Bob Babcock escreveu:
> 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.)

That's not what the documentation says.

The documentation says the second parameter *is* the size, not the maximum 
length.

It also says that "If size is -1 (default), it is taken to be qstrlen(str)."

So when you do:
	fromAscii("This\0isatest", 14);

You get a Unicode string of 14 codepoints, with a U+0000 in the fifth position.

-- 
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100414/dd028a70/attachment.bin 


More information about the Qt-interest-old mailing list