Simplified error handling in spdylay_session_new

This commit is contained in:
Tatsuhiro Tsujikawa 2012-02-16 22:47:49 +09:00
parent ac1629e61b
commit 61cfa3b9f8
1 changed files with 25 additions and 36 deletions

View File

@ -79,7 +79,7 @@ static int spdylay_session_new(spdylay_session **session_ptr,
int r; int r;
*session_ptr = malloc(sizeof(spdylay_session)); *session_ptr = malloc(sizeof(spdylay_session));
if(*session_ptr == NULL) { if(*session_ptr == NULL) {
return SPDYLAY_ERR_NOMEM; goto fail_session;
} }
memset(*session_ptr, 0, sizeof(spdylay_session)); memset(*session_ptr, 0, sizeof(spdylay_session));
@ -96,62 +96,34 @@ static int spdylay_session_new(spdylay_session **session_ptr,
r = spdylay_zlib_deflate_hd_init(&(*session_ptr)->hd_deflater); r = spdylay_zlib_deflate_hd_init(&(*session_ptr)->hd_deflater);
if(r != 0) { if(r != 0) {
free(*session_ptr); goto fail_hd_deflater;
return r;
} }
r = spdylay_zlib_inflate_hd_init(&(*session_ptr)->hd_inflater); r = spdylay_zlib_inflate_hd_init(&(*session_ptr)->hd_inflater);
if(r != 0) { if(r != 0) {
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater); goto fail_hd_inflater;
free(*session_ptr);
return r;
} }
r = spdylay_map_init(&(*session_ptr)->streams); r = spdylay_map_init(&(*session_ptr)->streams);
if(r != 0) { if(r != 0) {
spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater); goto fail_streams;
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
free(*session_ptr);
return r;
} }
r = spdylay_pq_init(&(*session_ptr)->ob_pq, spdylay_outbound_item_compar); r = spdylay_pq_init(&(*session_ptr)->ob_pq, spdylay_outbound_item_compar);
if(r != 0) { if(r != 0) {
spdylay_map_free(&(*session_ptr)->streams); goto fail_ob_pq;
spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater);
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
free(*session_ptr);
return r;
} }
r = spdylay_pq_init(&(*session_ptr)->ob_ss_pq, spdylay_outbound_item_compar); r = spdylay_pq_init(&(*session_ptr)->ob_ss_pq, spdylay_outbound_item_compar);
if(r != 0) { if(r != 0) {
spdylay_pq_free(&(*session_ptr)->ob_pq); goto fail_ob_ss_pq;
spdylay_map_free(&(*session_ptr)->streams);
spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater);
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
free(*session_ptr);
return r;
} }
(*session_ptr)->aob.framebuf = malloc(SPDYLAY_INITIAL_OUTBOUND_BUFFER_LENGTH); (*session_ptr)->aob.framebuf = malloc(SPDYLAY_INITIAL_OUTBOUND_BUFFER_LENGTH);
if((*session_ptr)->aob.framebuf == NULL) { if((*session_ptr)->aob.framebuf == NULL) {
spdylay_pq_free(&(*session_ptr)->ob_ss_pq); goto fail_aob_framebuf;
spdylay_pq_free(&(*session_ptr)->ob_pq);
spdylay_map_free(&(*session_ptr)->streams);
spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater);
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
free(*session_ptr);
return r;
} }
(*session_ptr)->aob.framebufmax = SPDYLAY_INITIAL_OUTBOUND_BUFFER_LENGTH; (*session_ptr)->aob.framebufmax = SPDYLAY_INITIAL_OUTBOUND_BUFFER_LENGTH;
(*session_ptr)->nvbuf = malloc(SPDYLAY_INITIAL_NV_BUFFER_LENGTH); (*session_ptr)->nvbuf = malloc(SPDYLAY_INITIAL_NV_BUFFER_LENGTH);
if((*session_ptr)->nvbuf == NULL) { if((*session_ptr)->nvbuf == NULL) {
free((*session_ptr)->aob.framebuf); goto fail_nvbuf;
spdylay_pq_free(&(*session_ptr)->ob_ss_pq);
spdylay_pq_free(&(*session_ptr)->ob_pq);
spdylay_map_free(&(*session_ptr)->streams);
spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater);
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
free(*session_ptr);
return r;
} }
(*session_ptr)->nvbuflen = SPDYLAY_INITIAL_NV_BUFFER_LENGTH; (*session_ptr)->nvbuflen = SPDYLAY_INITIAL_NV_BUFFER_LENGTH;
@ -169,6 +141,23 @@ static int spdylay_session_new(spdylay_session **session_ptr,
(*session_ptr)->iframe.state = SPDYLAY_RECV_HEAD; (*session_ptr)->iframe.state = SPDYLAY_RECV_HEAD;
return 0; return 0;
fail_nvbuf:
free((*session_ptr)->aob.framebuf);
fail_aob_framebuf:
spdylay_pq_free(&(*session_ptr)->ob_ss_pq);
fail_ob_ss_pq:
spdylay_pq_free(&(*session_ptr)->ob_pq);
fail_ob_pq:
spdylay_map_free(&(*session_ptr)->streams);
fail_streams:
spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater);
fail_hd_inflater:
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
fail_hd_deflater:
free(*session_ptr);
fail_session:
return SPDYLAY_ERR_NOMEM;
} }
int spdylay_session_client_new(spdylay_session **session_ptr, int spdylay_session_client_new(spdylay_session **session_ptr,