After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 580873 - Documentation of register type functions incomplete
Documentation of register type functions incomplete
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: docs
unspecified
Other All
: Normal minor
: ---
Assigned To: gtkdev
gtkdev
Depends on:
Blocks:
 
 
Reported: 2009-04-30 11:53 UTC by ralf-engels
Modified: 2012-02-27 03:21 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Proposed patch (1.47 KB, patch)
2009-04-30 11:54 UTC, ralf-engels
none Details | Review
improve g_type_register_fundamental() description (1.46 KB, patch)
2012-02-21 10:07 UTC, David King
accepted-commit_now Details | Review
docs: Clarify g_type_register_fundamental() behaviour (1.46 KB, patch)
2012-02-27 03:21 UTC, Matthias Clasen
committed Details | Review

Description ralf-engels 2009-04-30 11:53:33 UTC
Documentation 
Section: gobject gtype  g_type_register_fundamental
Does in most cases not mention that the function can return 0 in case of failure.

Correct version:
Should mention error cases

Other information:
I hope that I can add the patch later.
Comment 1 ralf-engels 2009-04-30 11:54:24 UTC
Created attachment 133651 [details] [review]
Proposed patch
Comment 2 Simon McVittie 2011-10-04 10:13:09 UTC
I think the intention is that if you hit any of the code paths that cause a warning and G_TYPE_INVALID, you're in "programmer error, undefined behaviour" territory (see Bug #660809); the solution isn't to cope with a 0 return, it's to fix your program so 0 can't be returned.

Where there are non-obvious preconditions, the preconditions themselves should be documented (to assist in writing non-broken programs), rather than documenting what will happen if your program is broken.
Comment 3 ralf-engels 2011-10-06 20:28:47 UTC
Hi again (after two years...)

I think we agree that the documentation is incomplete as 
  * Returns: The predefined type identifier.
is only half of the truth.

On the other hand it seems to be common to describe the possible return values in complete e.g. in the malloc documentation:

For  calloc()  and  malloc(),  return  a pointer to the allocated memory....  On error, these functions return NULL.
       NULL may also be returned by a successful call to malloc() with a size of zero, ...

It doesn't say here: Better call malloc only if enough memory is available and don't try to use this function to allocate null bytes.
Comment 4 Simon McVittie 2011-10-07 10:27:12 UTC
(In reply to comment #3)
> On the other hand it seems to be common to describe the possible return values
> in complete e.g. in the malloc documentation:

malloc isn't in a library whose policy is to terminate on memory allocation failures; GObject is.

> It doesn't say here: Better call malloc only if enough memory is available and
> don't try to use this function to allocate null bytes.

Neither does it say "if you have previously overrun a heap buffer, malloc might crash", which is equally true. The C standard formalizes this sort of thing as "undefined behaviour", e.g. for realloc:

    If ptr does not match a pointer returned earlier by calloc(),
    malloc() or realloc() or if the space has previously been deallocated
    by a call to free() or realloc(), the behaviour is undefined.

Some libraries word a similar situation as "it is an error if...".

If g_type_register_fundamental documented its error conditions, it should look more like this:

    If @type_id is already registered, or a type named @type_name is
    already registered, the behaviour is undefined

or

    It is an error to call this function for a @type_id or @type_name
    that already exists.
Comment 5 ralf-engels 2011-10-07 18:04:14 UTC
Only partly agree but your last two proposals are fine.


An ASSERT would also help.
Comment 6 David King 2012-02-21 10:07:41 UTC
Created attachment 208101 [details] [review]
improve g_type_register_fundamental() description
Comment 7 Simon McVittie 2012-02-21 11:03:39 UTC
(In reply to comment #6)
> Created an attachment (id=208101)
> improve g_type_register_fundamental() description

I'm not a GLib reviewer, but this looks fine to me.
Comment 8 Matthias Clasen 2012-02-22 07:27:35 UTC
Review of attachment 208101 [details] [review]:

sure, looks fine
Comment 9 Matthias Clasen 2012-02-22 07:27:45 UTC
Review of attachment 208101 [details] [review]:

sure, looks fine
Comment 10 Matthias Clasen 2012-02-27 03:21:19 UTC
The following fix has been pushed:
500aafd docs: Clarify g_type_register_fundamental() behaviour
Comment 11 Matthias Clasen 2012-02-27 03:21:24 UTC
Created attachment 208465 [details] [review]
docs: Clarify g_type_register_fundamental() behaviour