3939 dynamic table capacity that QPACK encoder is willing to use. */
4040#define NGHTTP3_QPACK_ENCODER_MAX_DTABLE_CAPACITY 4096
4141
42- nghttp3_objalloc_def (chunk , nghttp3_chunk , oplent );
42+ nghttp3_objalloc_def (chunk , nghttp3_chunk , oplent )
4343
4444/*
4545 * conn_remote_stream_uni returns nonzero if |stream_id| is remote
@@ -233,12 +233,16 @@ static int conn_new(nghttp3_conn **pconn, int server, int callbacks_version,
233233 const nghttp3_callbacks * callbacks , int settings_version ,
234234 const nghttp3_settings * settings , const nghttp3_mem * mem ,
235235 void * user_data ) {
236- int rv ;
237236 nghttp3_conn * conn ;
238237 size_t i ;
239238 (void )callbacks_version ;
240239 (void )settings_version ;
241240
241+ assert (settings -> max_field_section_size <= NGHTTP3_VARINT_MAX );
242+ assert (settings -> qpack_max_dtable_capacity <= NGHTTP3_VARINT_MAX );
243+ assert (settings -> qpack_encoder_max_dtable_capacity <= NGHTTP3_VARINT_MAX );
244+ assert (settings -> qpack_blocked_streams <= NGHTTP3_VARINT_MAX );
245+
242246 if (mem == NULL ) {
243247 mem = nghttp3_mem_default ();
244248 }
@@ -254,18 +258,11 @@ static int conn_new(nghttp3_conn **pconn, int server, int callbacks_version,
254258
255259 nghttp3_map_init (& conn -> streams , mem );
256260
257- rv =
258- nghttp3_qpack_decoder_init (& conn -> qdec , settings -> qpack_max_dtable_capacity ,
259- settings -> qpack_blocked_streams , mem );
260- if (rv != 0 ) {
261- goto qdec_init_fail ;
262- }
261+ nghttp3_qpack_decoder_init (& conn -> qdec , settings -> qpack_max_dtable_capacity ,
262+ settings -> qpack_blocked_streams , mem );
263263
264- rv = nghttp3_qpack_encoder_init (
265- & conn -> qenc , settings -> qpack_encoder_max_dtable_capacity , mem );
266- if (rv != 0 ) {
267- goto qenc_init_fail ;
268- }
264+ nghttp3_qpack_encoder_init (& conn -> qenc ,
265+ settings -> qpack_encoder_max_dtable_capacity , mem );
269266
270267 nghttp3_pq_init (& conn -> qpack_blocked_streams , ricnt_less , mem );
271268
@@ -291,16 +288,6 @@ static int conn_new(nghttp3_conn **pconn, int server, int callbacks_version,
291288 * pconn = conn ;
292289
293290 return 0 ;
294-
295- qenc_init_fail :
296- nghttp3_qpack_decoder_free (& conn -> qdec );
297- qdec_init_fail :
298- nghttp3_map_free (& conn -> streams );
299- nghttp3_objalloc_free (& conn -> stream_objalloc );
300- nghttp3_objalloc_free (& conn -> out_chunk_objalloc );
301- nghttp3_mem_free (mem , conn );
302-
303- return rv ;
304291}
305292
306293int nghttp3_conn_client_new_versioned (nghttp3_conn * * pconn ,
@@ -399,6 +386,9 @@ nghttp3_ssize nghttp3_conn_read_stream(nghttp3_conn *conn, int64_t stream_id,
399386 size_t bidi_nproc ;
400387 int rv ;
401388
389+ assert (stream_id >= 0 );
390+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
391+
402392 stream = nghttp3_conn_find_stream (conn , stream_id );
403393 if (stream == NULL ) {
404394 /* TODO Assert idtr */
@@ -434,6 +424,10 @@ nghttp3_ssize nghttp3_conn_read_stream(nghttp3_conn *conn, int64_t stream_id,
434424 return rv ;
435425 }
436426 }
427+ } else if (!nghttp3_client_stream_uni (stream_id )) {
428+ /* server does not expect to receive new server initiated
429+ bidirectional or unidirectional stream from client. */
430+ return NGHTTP3_ERR_H3_STREAM_CREATION_ERROR ;
437431 } else {
438432 /* unidirectional stream */
439433 if (srclen == 0 && fin ) {
@@ -448,7 +442,7 @@ nghttp3_ssize nghttp3_conn_read_stream(nghttp3_conn *conn, int64_t stream_id,
448442
449443 stream -> rx .hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL ;
450444 stream -> tx .hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL ;
451- } else if (nghttp3_stream_uni (stream_id )) {
445+ } else if (nghttp3_server_stream_uni (stream_id )) {
452446 if (srclen == 0 && fin ) {
453447 return 0 ;
454448 }
@@ -461,17 +455,16 @@ nghttp3_ssize nghttp3_conn_read_stream(nghttp3_conn *conn, int64_t stream_id,
461455 stream -> rx .hstate = NGHTTP3_HTTP_STATE_RESP_INITIAL ;
462456 stream -> tx .hstate = NGHTTP3_HTTP_STATE_RESP_INITIAL ;
463457 } else {
464- /* client doesn't expect to receive new bidirectional stream
465- from server. */
458+ /* client doesn't expect to receive new bidirectional stream or
459+ client initiated unidirectional stream from server. */
466460 return NGHTTP3_ERR_H3_STREAM_CREATION_ERROR ;
467461 }
468462 } else if (conn -> server ) {
469- if (nghttp3_client_stream_bidi (stream_id )) {
470- if (stream -> rx .hstate == NGHTTP3_HTTP_STATE_NONE ) {
471- stream -> rx .hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL ;
472- stream -> tx .hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL ;
473- }
474- }
463+ assert (nghttp3_client_stream_bidi (stream_id ) ||
464+ nghttp3_client_stream_uni (stream_id ));
465+ } else {
466+ assert (nghttp3_client_stream_bidi (stream_id ) ||
467+ nghttp3_server_stream_uni (stream_id ));
475468 }
476469
477470 if (srclen == 0 && !fin ) {
@@ -608,6 +601,9 @@ nghttp3_ssize nghttp3_conn_read_uni(nghttp3_conn *conn, nghttp3_stream *stream,
608601 break ;
609602 case NGHTTP3_STREAM_TYPE_UNKNOWN :
610603 nconsumed = (nghttp3_ssize )srclen ;
604+ if (fin ) {
605+ break ;
606+ }
611607
612608 rv = conn_call_stop_sending (conn , stream , NGHTTP3_H3_STREAM_CREATION_ERROR );
613609 if (rv != 0 ) {
@@ -665,7 +661,7 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
665661 nghttp3_varint_read_state_reset (rvint );
666662 rstate -> state = NGHTTP3_CTRL_STREAM_STATE_FRAME_LENGTH ;
667663 if (p == end ) {
668- break ;
664+ return ( nghttp3_ssize ) nconsumed ;
669665 }
670666 /* Fall through */
671667 case NGHTTP3_CTRL_STREAM_STATE_FRAME_LENGTH :
@@ -973,6 +969,10 @@ nghttp3_ssize nghttp3_conn_read_control(nghttp3_conn *conn,
973969
974970 rstate -> state = NGHTTP3_CTRL_STREAM_STATE_PRIORITY_UPDATE ;
975971
972+ if (p == end ) {
973+ return (nghttp3_ssize )nconsumed ;
974+ }
975+
976976 /* Fall through */
977977 case NGHTTP3_CTRL_STREAM_STATE_PRIORITY_UPDATE :
978978 /* We need to buffer Priority Field Value because it might be
@@ -1792,6 +1792,8 @@ conn_on_priority_update_stream(nghttp3_conn *conn,
17921792
17931793 stream -> node .pri = fr -> pri ;
17941794 stream -> flags |= NGHTTP3_STREAM_FLAG_PRIORITY_UPDATE_RECVED ;
1795+ stream -> rx .hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL ;
1796+ stream -> tx .hstate = NGHTTP3_HTTP_STATE_REQ_INITIAL ;
17951797
17961798 return 0 ;
17971799 }
@@ -1836,7 +1838,7 @@ int nghttp3_conn_create_stream(nghttp3_conn *conn, nghttp3_stream **pstream,
18361838 nghttp3_stream * stream ;
18371839 int rv ;
18381840 nghttp3_stream_callbacks callbacks = {
1839- conn_stream_acked_data ,
1841+ . acked_data = conn_stream_acked_data ,
18401842 };
18411843
18421844 rv = nghttp3_stream_new (& stream , stream_id , & callbacks ,
@@ -1874,6 +1876,8 @@ int nghttp3_conn_bind_control_stream(nghttp3_conn *conn, int64_t stream_id) {
18741876 nghttp3_frame_entry frent ;
18751877 int rv ;
18761878
1879+ assert (stream_id >= 0 );
1880+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
18771881 assert (!conn -> server || nghttp3_server_stream_uni (stream_id ));
18781882 assert (conn -> server || nghttp3_client_stream_uni (stream_id ));
18791883
@@ -1906,6 +1910,10 @@ int nghttp3_conn_bind_qpack_streams(nghttp3_conn *conn, int64_t qenc_stream_id,
19061910 nghttp3_stream * stream ;
19071911 int rv ;
19081912
1913+ assert (qenc_stream_id >= 0 );
1914+ assert (qenc_stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
1915+ assert (qdec_stream_id >= 0 );
1916+ assert (qdec_stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
19091917 assert (!conn -> server || nghttp3_server_stream_uni (qenc_stream_id ));
19101918 assert (!conn -> server || nghttp3_server_stream_uni (qdec_stream_id ));
19111919 assert (conn -> server || nghttp3_client_stream_uni (qenc_stream_id ));
@@ -2194,13 +2202,11 @@ int nghttp3_conn_submit_request(nghttp3_conn *conn, int64_t stream_id,
21942202 assert (!conn -> server );
21952203 assert (conn -> tx .qenc );
21962204
2205+ assert (stream_id >= 0 );
2206+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
21972207 assert (nghttp3_client_stream_bidi (stream_id ));
21982208
2199- /* TODO Should we check that stream_id is client stream_id? */
22002209 /* TODO Check GOAWAY last stream ID */
2201- if (nghttp3_stream_uni (stream_id )) {
2202- return NGHTTP3_ERR_INVALID_ARGUMENT ;
2203- }
22042210
22052211 if (conn -> flags & NGHTTP3_CONN_FLAG_GOAWAY_RECVED ) {
22062212 return NGHTTP3_ERR_CONN_CLOSING ;
@@ -2454,6 +2460,9 @@ int nghttp3_conn_close_stream(nghttp3_conn *conn, int64_t stream_id,
24542460int nghttp3_conn_shutdown_stream_read (nghttp3_conn * conn , int64_t stream_id ) {
24552461 nghttp3_stream * stream ;
24562462
2463+ assert (stream_id >= 0 );
2464+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
2465+
24572466 if (!nghttp3_client_stream_bidi (stream_id )) {
24582467 return 0 ;
24592468 }
@@ -2515,6 +2524,9 @@ uint64_t nghttp3_conn_get_frame_payload_left(nghttp3_conn *conn,
25152524 nghttp3_stream * stream ;
25162525 int uni = 0 ;
25172526
2527+ assert (stream_id >= 0 );
2528+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
2529+
25182530 if (!nghttp3_client_stream_bidi (stream_id )) {
25192531 uni = conn_remote_stream_uni (conn , stream_id );
25202532 if (!uni ) {
@@ -2542,6 +2554,8 @@ int nghttp3_conn_get_stream_priority_versioned(nghttp3_conn *conn,
25422554 (void )pri_version ;
25432555
25442556 assert (conn -> server );
2557+ assert (stream_id >= 0 );
2558+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
25452559
25462560 if (!nghttp3_client_stream_bidi (stream_id )) {
25472561 return NGHTTP3_ERR_INVALID_ARGUMENT ;
@@ -2566,6 +2580,8 @@ int nghttp3_conn_set_client_stream_priority(nghttp3_conn *conn,
25662580 uint8_t * buf = NULL ;
25672581
25682582 assert (!conn -> server );
2583+ assert (stream_id >= 0 );
2584+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
25692585
25702586 if (!nghttp3_client_stream_bidi (stream_id )) {
25712587 return NGHTTP3_ERR_INVALID_ARGUMENT ;
@@ -2603,6 +2619,8 @@ int nghttp3_conn_set_server_stream_priority_versioned(nghttp3_conn *conn,
26032619 assert (conn -> server );
26042620 assert (pri -> urgency < NGHTTP3_URGENCY_LEVELS );
26052621 assert (pri -> inc == 0 || pri -> inc == 1 );
2622+ assert (stream_id >= 0 );
2623+ assert (stream_id <= (int64_t )NGHTTP3_MAX_VARINT );
26062624
26072625 if (!nghttp3_client_stream_bidi (stream_id )) {
26082626 return NGHTTP3_ERR_INVALID_ARGUMENT ;
0 commit comments