Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the advantage of using goto in this code?

static gboolean
gst_fd_src_start (GstBaseSrc * bsrc)
{
  GstFdSrc *src = GST_FD_SRC (bsrc);

  src->curoffset = 0;

  if ((src->fdset = gst_poll_new (TRUE)) == NULL)
    goto socket_pair;

  gst_fd_src_update_fd (src, -1);

  return TRUE;

  /* ERRORS */
socket_pair:
  {
    GST_ELEMENT_ERROR (src, RESOURCE, OPEN_READ_WRITE, (NULL),
        GST_ERROR_SYSTEM);
    return FALSE;
  }
}

why here someone has used goto socket_pair;i am not getting why this mechanism is used ? why dont we just write error message there and return ?

note: this is gstreamer plugin code

like image 469
Jeegar Patel Avatar asked Nov 28 '22 07:11

Jeegar Patel


1 Answers

In larger routines there might be multiple points in the function which need to goto the error handling code. This routine should be seen in the context of having been written according to code conventions that mandate a common form for error handling.

It's true that goto should be considered dangerous but for a language like C which has no exception handling it can be the least bad option for error handling.

like image 137
David Heffernan Avatar answered Dec 05 '22 13:12

David Heffernan