Re: RFC: warnings on ignoring return value on some list operations

On Thu, 24 Nov 2005, Alexander Larsson wrote:

We recently had a bug in Nautilus where the return value from
g_list_sort() was ignored. These sorts of bugs are not all that uncommon
given the GList api, since its easy to forget the return value and from
an OO point of view g_list_sort (list) looks very right.

The attached patch adds a define for the gcc warn_unused_result function
attribute, and uses it in a few list operations.

I tried to be conservative in adding them, doing so only if it seems
unreasonable to assume the programmer knew it was safe to ignore the
return value. I.e. with g_list_prepend its never safe to ignore it, but
with g_list_append its safe if you know the list isn't empty.

Another example is g_list_remove(), where I think its uncommon to know
that the removed item isn't first in the list, whereas I didn't add one
for g_list_remove_link() since in that case its more likely that you
know the position of the link.

What do people think about this?

i think that is a really good idea. however i'm more of the opinion that all
list functions should have the warn_unused_result tag. code and conditions
are easily changed, moved around or pasted so that missing the return value
assignment for lists will become a problem. i.e. even if you know you're
appending to a non-empty list, you should still write
list = g_list_append (list, data);

so i'd apprchiate if you extended the patch to cover all list functions
that return possibly modified lists ;)

Alexander Larsson                                            Red Hat, Inc


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]