DevHeads.net

Review Request: Check KFileItem d pointer for validity in KFileItem methods

Review request for kdelibs and David Faure.

Description
Check the d pointer for validity since it is set to 0 by the defaut KFileItem ctor.

Patch based on similar code from QImage.

I'd like to draw your attention to these issues:

1) KFileItem::setUDSEntry()
For a null KFileItem, this should probably create a new d and initialize it. Ideally, we'd have a dedicated public function to do that (see QImage::create()), but since we can not change the public API (or can we?), I put the new in setUDSEntry(). Anyway, this is now the only interface function that can init a null KFileItem (before the patch, there had been none). All the other set* functions do not new the d, but rather return on d == 0.
2) KFileItem::cmp()
What should this return for a null KFileItem? I return false, but still, it feels wrong to me.
3) operator << / operator >>
<< returns "null KFileItem" for d == 0 and >> does nothing, returning the original stream unmodified. operator << thus duplicates the public operator <<(QDebug&).
4) This also overrides dfaure's patch for KFileItem::metaInfo(). There is a

return d->metaInfo()

at the end of the function, so this can crash as well.

All the classes' objects that are default-constructed and then returned in the case of d == 0 can be checked for validity, looked it up in the code.

Diffs
kio/kio/kfileitem.cpp 8ab5a1d

Diff: <a href="http://git.reviewboard.kde.org/r/106902/diff/" title="http://git.reviewboard.kde.org/r/106902/diff/">http://git.reviewboard.kde.org/r/106902/diff/</a>

Testing
Builds fine.

Thanks,

Ignat Semenov

Comments

Re: Review Request: Check KFileItem d pointer for validity in KF

By Ignat Semenov at 10/15/2012 - 14:52

(Updated Oct. 15, 2012, 7:52 p.m.)

Review request for kdelibs and David Faure.

Description
Check the d pointer for validity since it is set to 0 by the defaut KFileItem ctor.

Patch based on similar code from QImage.

I'd like to draw your attention to these issues:

1) KFileItem::setUDSEntry()
For a null KFileItem, this should probably create a new d and initialize it. Ideally, we'd have a dedicated public function to do that (see QImage::create()), but since we can not change the public API (or can we?), I put the new in setUDSEntry(). Anyway, this is now the only interface function that can init a null KFileItem (before the patch, there had been none). All the other set* functions do not new the d, but rather return on d == 0.
2) KFileItem::cmp()
What should this return for a null KFileItem? I return false, but still, it feels wrong to me.
3) operator << / operator >>
<< returns "null KFileItem" for d == 0 and >> does nothing, returning the original stream unmodified. operator << thus duplicates the public operator <<(QDebug&).
4) This also overrides dfaure's patch for KFileItem::metaInfo(). There is a

return d->metaInfo()

at the end of the function, so this can crash as well.

All the classes' objects that are default-constructed and then returned in the case of d == 0 can be checked for validity, looked it up in the code.

This addresses bug 299726.
<a href="http://bugs.kde.org/show_bug.cgi?id=299726" title="http://bugs.kde.org/show_bug.cgi?id=299726">http://bugs.kde.org/show_bug.cgi?id=299726</a>

Diffs
kio/kio/kfileitem.cpp 8ab5a1d

Diff: <a href="http://git.reviewboard.kde.org/r/106902/diff/" title="http://git.reviewboard.kde.org/r/106902/diff/">http://git.reviewboard.kde.org/r/106902/diff/</a>

Testing
Builds fine.

Thanks,

Ignat Semenov

Re: Review Request: Check KFileItem d pointer for validity in KF

By Ignat Semenov at 10/15/2012 - 17:15

(Updated Oct. 15, 2012, 10:15 p.m.)

Review request for kdelibs and David Faure.

Changes
Do not new the d in KFileItem::setUDSEntry().

Description
Check the d pointer for validity since it is set to 0 by the defaut KFileItem ctor.

Patch based on similar code from QImage.

I'd like to draw your attention to these issues:

1) KFileItem::setUDSEntry()
For a null KFileItem, this should probably create a new d and initialize it. Ideally, we'd have a dedicated public function to do that (see QImage::create()), but since we can not change the public API (or can we?), I put the new in setUDSEntry(). Anyway, this is now the only interface function that can init a null KFileItem (before the patch, there had been none). All the other set* functions do not new the d, but rather return on d == 0.
2) KFileItem::cmp()
What should this return for a null KFileItem? I return false, but still, it feels wrong to me.
3) operator << / operator >>
<< returns "null KFileItem" for d == 0 and >> does nothing, returning the original stream unmodified. operator << thus duplicates the public operator <<(QDebug&).
4) This also overrides dfaure's patch for KFileItem::metaInfo(). There is a

return d->metaInfo()

at the end of the function, so this can crash as well.

All the classes' objects that are default-constructed and then returned in the case of d == 0 can be checked for validity, looked it up in the code.

This addresses bug 299726.
<a href="http://bugs.kde.org/show_bug.cgi?id=299726" title="http://bugs.kde.org/show_bug.cgi?id=299726">http://bugs.kde.org/show_bug.cgi?id=299726</a>

Diffs (updated)
kio/kio/kfileitem.cpp 8ab5a1d

Diff: <a href="http://git.reviewboard.kde.org/r/106902/diff/" title="http://git.reviewboard.kde.org/r/106902/diff/">http://git.reviewboard.kde.org/r/106902/diff/</a>

Testing
Builds fine.

Thanks,

Ignat Semenov

Re: Review Request: Check KFileItem d pointer for validity in KF

By Ignat Semenov at 10/16/2012 - 09:43

(Updated Oct. 16, 2012, 2:43 p.m.)

Review request for kdelibs and David Faure.

Changes
Fixed the 3 issues.

Description
Check the d pointer for validity since it is set to 0 by the defaut KFileItem ctor.

Patch based on similar code from QImage.

I'd like to draw your attention to these issues:

1) KFileItem::setUDSEntry()
For a null KFileItem, this should probably create a new d and initialize it. Ideally, we'd have a dedicated public function to do that (see QImage::create()), but since we can not change the public API (or can we?), I put the new in setUDSEntry(). Anyway, this is now the only interface function that can init a null KFileItem (before the patch, there had been none). All the other set* functions do not new the d, but rather return on d == 0.
2) KFileItem::cmp()
What should this return for a null KFileItem? I return false, but still, it feels wrong to me.
3) operator << / operator >>
<< returns "null KFileItem" for d == 0 and >> does nothing, returning the original stream unmodified. operator << thus duplicates the public operator <<(QDebug&).
4) This also overrides dfaure's patch for KFileItem::metaInfo(). There is a

return d->metaInfo()

at the end of the function, so this can crash as well.

All the classes' objects that are default-constructed and then returned in the case of d == 0 can be checked for validity, looked it up in the code.

This addresses bug 299726.
<a href="http://bugs.kde.org/show_bug.cgi?id=299726" title="http://bugs.kde.org/show_bug.cgi?id=299726">http://bugs.kde.org/show_bug.cgi?id=299726</a>

Diffs (updated)
kio/kio/kfileitem.cpp 8ab5a1d

Diff: <a href="http://git.reviewboard.kde.org/r/106902/diff/" title="http://git.reviewboard.kde.org/r/106902/diff/">http://git.reviewboard.kde.org/r/106902/diff/</a>

Testing
Builds fine.

Thanks,

Ignat Semenov

Re: Review Request: Check KFileItem d pointer for validity in KF

By Ignat Semenov at 10/16/2012 - 11:15

(Updated Oct. 16, 2012, 4:15 p.m.)

Review request for kdelibs and David Faure.

Changes
Fix issues.

Description
Check the d pointer for validity since it is set to 0 by the defaut KFileItem ctor.

Patch based on similar code from QImage.

I'd like to draw your attention to these issues:

1) KFileItem::setUDSEntry()
For a null KFileItem, this should probably create a new d and initialize it. Ideally, we'd have a dedicated public function to do that (see QImage::create()), but since we can not change the public API (or can we?), I put the new in setUDSEntry(). Anyway, this is now the only interface function that can init a null KFileItem (before the patch, there had been none). All the other set* functions do not new the d, but rather return on d == 0.
2) KFileItem::cmp()
What should this return for a null KFileItem? I return false, but still, it feels wrong to me.
3) operator << / operator >>
<< returns "null KFileItem" for d == 0 and >> does nothing, returning the original stream unmodified. operator << thus duplicates the public operator <<(QDebug&).
4) This also overrides dfaure's patch for KFileItem::metaInfo(). There is a

return d->metaInfo()

at the end of the function, so this can crash as well.

All the classes' objects that are default-constructed and then returned in the case of d == 0 can be checked for validity, looked it up in the code.

This addresses bug 299726.
<a href="http://bugs.kde.org/show_bug.cgi?id=299726" title="http://bugs.kde.org/show_bug.cgi?id=299726">http://bugs.kde.org/show_bug.cgi?id=299726</a>

Diffs (updated)
kio/kio/kfileitem.cpp 8ab5a1d

Diff: <a href="http://git.reviewboard.kde.org/r/106902/diff/" title="http://git.reviewboard.kde.org/r/106902/diff/">http://git.reviewboard.kde.org/r/106902/diff/</a>

Testing
Builds fine.

Thanks,

Ignat Semenov

Re: Review Request: Check KFileItem d pointer for validity in KF

By Ignat Semenov at 10/16/2012 - 11:22

(Updated Oct. 16, 2012, 4:22 p.m.)

Review request for kdelibs and David Faure.

Description
Check the d pointer for validity since it is set to 0 by the defaut KFileItem ctor.

Patch based on similar code from QImage.

I'd like to draw your attention to these issues:

1) KFileItem::setUDSEntry()
For a null KFileItem, this should probably create a new d and initialize it. Ideally, we'd have a dedicated public function to do that (see QImage::create()), but since we can not change the public API (or can we?), I put the new in setUDSEntry(). Anyway, this is now the only interface function that can init a null KFileItem (before the patch, there had been none). All the other set* functions do not new the d, but rather return on d == 0.
2) KFileItem::cmp()
What should this return for a null KFileItem? I return false, but still, it feels wrong to me.
3) operator << / operator >>
<< returns "null KFileItem" for d == 0 and >> does nothing, returning the original stream unmodified. operator << thus duplicates the public operator <<(QDebug&).
4) This also overrides dfaure's patch for KFileItem::metaInfo(). There is a

return d->metaInfo()

at the end of the function, so this can crash as well.

All the classes' objects that are default-constructed and then returned in the case of d == 0 can be checked for validity, looked it up in the code.

This addresses bug 299726.
<a href="http://bugs.kde.org/show_bug.cgi?id=299726" title="http://bugs.kde.org/show_bug.cgi?id=299726">http://bugs.kde.org/show_bug.cgi?id=299726</a>

Diffs
kio/kio/kfileitem.cpp 8ab5a1d

Diff: <a href="http://git.reviewboard.kde.org/r/106902/diff/" title="http://git.reviewboard.kde.org/r/106902/diff/">http://git.reviewboard.kde.org/r/106902/diff/</a>

Testing
Builds fine.

Thanks,

Ignat Semenov

Re: Review Request: Check KFileItem d pointer for validity in KF

By Commit Hook at 10/21/2012 - 14:15

This review has been submitted with commit 0b8942128aafe59d7ffef17cef28419c30cbf684 by Ignat Semenov to branch KDE/4.9.

- Commit Hook

On Oct. 16, 2012, 4:22 p.m., Ignat Semenov wrote:

Re: Review Request: Check KFileItem d pointer for validity in KF

By David Faure at 10/21/2012 - 04:49

Almost there ;)
Feel free to commit after fixing these last small issues.

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16255>

if (!d) {
on a single line (kdelibs coding style, even if you can see other ways being used in old files)

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16256>

Not a setter, no warning needed.

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16257>

This is the one where I would expect a warning, rather than no action.

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16258>

One case is missing: item.d being NULL.

This should say
if (!d || !item.d)
return false;

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16259>

brace on the same line, here and in similar if()s before and after.

- David Faure

On Oct. 16, 2012, 4:22 p.m., Ignat Semenov wrote:

Re: Review Request: Check KFileItem d pointer for validity in KF

By Ignat Semenov at 10/16/2012 - 11:15

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16158>

I was actually thinking about url.isEmpty(), but probably got confused by your solution :)

- Ignat Semenov

On Oct. 16, 2012, 4:15 p.m., Ignat Semenov wrote:

Re: Review Request: Check KFileItem d pointer for validity in KF

By David Faure at 10/16/2012 - 10:39

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16145>

kdelibs coding style says this should be on the same line as the if().

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16146>

Don't put the method name in the warning, kWarning does that.

My bad for pointing you to QImage which doens't do that.

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16147>

setter, should warn

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16148>

setter, should warn

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16149>

supposed to trigger an action, should warn

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16155>

(setter, but deprecated, so don't warn, IMHO)

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16151>

coding style: move up

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16152>

Hmm, this is tricky.
We should always match the number of >> that were done in the stream, otherwise everything will go wrong.

If a.d is null, then we can't restore the item. We should warn, then, and probably restore the 3 fields into temporary (thrown away) variables.

If on the other end a null item was streamed in (empty URL), then we should 1) check for that (you missed that step: if url.isEmpty() { nothing else to stream out } ), and 2) well, make "a" null, so delete a.d; a.d = 0;

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16153>

setter, should warn

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16154>

setter, should warn

- David Faure

On Oct. 16, 2012, 2:43 p.m., Ignat Semenov wrote:

Re: Review Request: Check KFileItem d pointer for validity in KF

By Ignat Semenov at 10/16/2012 - 11:16

The whole file has braces on new line, so to be consistent, I do the same.

Was coding in vim, so could not look up the kWarning def, my bad.

- Ignat

On Oct. 16, 2012, 4:15 p.m., Ignat Semenov wrote:

Re: Review Request: Check KFileItem d pointer for validity in KF

By David Faure at 10/16/2012 - 06:38

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16137>

QImage issues a qWarning when calling a setter on a null image, so that developers know that they are doing something wrong. Can you do the same? Well, with kWarning, in kdelibs-4.x.

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16138>

actually, if both items are null, cmp should return true.

kio/kio/kfileitem.cpp
<http://git.reviewboard.kde.org/r/106902/#comment16139>

this seems wrong, it's not symmetrical with operator>>, so this will corrupt the stream...

Better stream out an empty url if d==0, and in >>, read into a temp url, and if empty, skip the other fields.

- David Faure

On Oct. 15, 2012, 10:15 p.m., Ignat Semenov wrote: