Skip to content

Commit 0bdf692

Browse files
committed
[dev] added checks for memory allocations & some important functions.
1 parent 1ba6a5d commit 0bdf692

10 files changed

+146
-19
lines changed

dash/ngx_rtmp_dash_module.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,6 +891,10 @@ ngx_rtmp_dash_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
891891
}
892892

893893
ctx->playlist.data = ngx_palloc(s->connection->pool, len);
894+
if (ctx->playlist.data == NULL) {
895+
return NGX_ERROR;
896+
}
897+
894898
p = ngx_cpymem(ctx->playlist.data, dacf->path.data, dacf->path.len);
895899

896900
if (p[-1] != '/') {
@@ -909,6 +913,9 @@ ngx_rtmp_dash_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
909913
ctx->stream.data = ngx_palloc(s->connection->pool,
910914
ctx->stream.len + NGX_INT32_LEN +
911915
sizeof(".m4x"));
916+
if (ctx->stream.data == NULL) {
917+
return NGX_ERROR;
918+
}
912919

913920
ngx_memcpy(ctx->stream.data, ctx->playlist.data, ctx->stream.len - 1);
914921
ctx->stream.data[ctx->stream.len - 1] = (dacf->nested ? '/' : '-');
@@ -927,6 +934,10 @@ ngx_rtmp_dash_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
927934

928935
ctx->playlist_bak.data = ngx_palloc(s->connection->pool,
929936
ctx->playlist.len + sizeof(".bak"));
937+
if (ctx->playlist_bak.data == NULL) {
938+
return NGX_ERROR;
939+
}
940+
930941
p = ngx_cpymem(ctx->playlist_bak.data, ctx->playlist.data,
931942
ctx->playlist.len);
932943
p = ngx_cpymem(p, ".bak", sizeof(".bak") - 1);

hls/ngx_rtmp_hls_module.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1323,10 +1323,14 @@ ngx_rtmp_hls_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
13231323
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_hls_module);
13241324

13251325
if (ctx == NULL) {
1326-
13271326
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_hls_ctx_t));
1328-
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_hls_module);
1327+
if (ctx == NULL) {
1328+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1329+
"hls: failed to allocate for publish ctx");
1330+
return NGX_ERROR;
1331+
}
13291332

1333+
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_hls_module);
13301334
} else {
13311335

13321336
f = ctx->frags;
@@ -1372,6 +1376,10 @@ ngx_rtmp_hls_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
13721376
}
13731377

13741378
ctx->playlist.data = ngx_palloc(s->connection->pool, len);
1379+
if (ctx->playlist.data == NULL) {
1380+
return NGX_ERROR;
1381+
}
1382+
13751383
p = ngx_cpymem(ctx->playlist.data, hacf->path.data, hacf->path.len);
13761384

13771385
if (p[-1] != '/') {
@@ -1390,6 +1398,9 @@ ngx_rtmp_hls_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
13901398
ctx->stream.data = ngx_palloc(s->connection->pool,
13911399
ctx->stream.len + NGX_INT64_LEN +
13921400
sizeof(".ts"));
1401+
if (ctx->stream.data == NULL) {
1402+
return NGX_ERROR;
1403+
}
13931404

13941405
ngx_memcpy(ctx->stream.data, ctx->playlist.data, ctx->stream.len - 1);
13951406
ctx->stream.data[ctx->stream.len - 1] = (hacf->nested ? '/' : '-');
@@ -1413,6 +1424,9 @@ ngx_rtmp_hls_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
14131424
- 1;
14141425
ctx->var_playlist.data = ngx_palloc(s->connection->pool,
14151426
ctx->var_playlist.len + 1);
1427+
if (ctx->var_playlist.data == NULL) {
1428+
return NGX_ERROR;
1429+
}
14161430

14171431
pp = ngx_cpymem(ctx->var_playlist.data, ctx->playlist.data,
14181432
len - var->suffix.len);
@@ -1423,6 +1437,9 @@ ngx_rtmp_hls_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
14231437
sizeof(".bak") - 1;
14241438
ctx->var_playlist_bak.data = ngx_palloc(s->connection->pool,
14251439
ctx->var_playlist_bak.len + 1);
1440+
if (ctx->var_playlist_bak.data == NULL) {
1441+
return NGX_ERROR;
1442+
}
14261443

14271444
pp = ngx_cpymem(ctx->var_playlist_bak.data,
14281445
ctx->var_playlist.data,
@@ -1454,6 +1471,10 @@ ngx_rtmp_hls_publish(ngx_rtmp_session_t *s, ngx_rtmp_publish_t *v)
14541471

14551472
ctx->playlist_bak.data = ngx_palloc(s->connection->pool,
14561473
ctx->playlist.len + sizeof(".bak"));
1474+
if (ctx->playlist_bak.data == NULL) {
1475+
return NGX_ERROR;
1476+
}
1477+
14571478
p = ngx_cpymem(ctx->playlist_bak.data, ctx->playlist.data,
14581479
ctx->playlist.len);
14591480
p = ngx_cpymem(p, ".bak", sizeof(".bak") - 1);

ngx_http_flv_live_module.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,13 @@ ngx_http_flv_live_join(ngx_rtmp_session_t *s, u_char *name,
10921092

10931093
if (ctx == NULL) {
10941094
ctx = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_live_ctx_t));
1095+
if (ctx == NULL) {
1096+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1097+
"flv live: failed to allocate for ctx");
1098+
1099+
return NGX_ERROR;
1100+
}
1101+
10951102
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_live_module);
10961103
}
10971104

@@ -2073,6 +2080,9 @@ ngx_http_flv_live_connect_init(ngx_rtmp_session_t *s, ngx_str_t *app,
20732080
#define NGX_RTMP_SET_STRPAR(name) \
20742081
s->name.len = ngx_strlen(v.name); \
20752082
s->name.data = ngx_palloc(c->pool, s->name.len); \
2083+
if (s->name.data == NULL) { \
2084+
return NGX_ERROR; \
2085+
} \
20762086
ngx_memcpy(s->name.data, v.name, s->name.len)
20772087

20782088
NGX_RTMP_SET_STRPAR(app);

ngx_rtmp.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,11 @@ ngx_rtmp_init_event_handlers(ngx_conf_t *cf, ngx_rtmp_core_main_conf_t *cmcf)
456456
*eh = ngx_rtmp_aggregate_message_handler;
457457

458458
/* init amf callbacks */
459-
ngx_array_init(&cmcf->amf_arrays, cf->pool, 1, sizeof(ngx_hash_key_t));
459+
if (ngx_array_init(&cmcf->amf_arrays, cf->pool,
460+
1, sizeof(ngx_hash_key_t)) != NGX_OK)
461+
{
462+
return NGX_ERROR;
463+
}
460464

461465
h = cmcf->amf.elts;
462466
for(n = 0; n < cmcf->amf.nelts; ++n, ++h) {
@@ -1342,6 +1346,10 @@ ngx_rtmp_set_virtual_server(ngx_rtmp_session_t *s, ngx_str_t *host)
13421346
* ((ngx_rtmp_core_srv_conf_t *)
13431347
cscf->ctx->srv_conf[ngx_rtmp_core_module
13441348
.ctx_index])->out_queue);
1349+
if (s->out == NULL) {
1350+
ngx_rtmp_finalize_session(s);
1351+
return NGX_ERROR;
1352+
}
13451353

13461354
s->out_queue = cscf->out_queue;
13471355
}

ngx_rtmp_cmd_module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ ngx_rtmp_cmd_connect_init(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
189189
#define NGX_RTMP_SET_STRPAR(name) \
190190
s->name.len = ngx_strlen(v.name); \
191191
s->name.data = ngx_palloc(s->connection->pool, s->name.len); \
192+
if (s->name.data == NULL) { \
193+
return NGX_ERROR; \
194+
} \
192195
ngx_memcpy(s->name.data, v.name, s->name.len)
193196

194197
NGX_RTMP_SET_STRPAR(app);
@@ -322,6 +325,9 @@ ngx_rtmp_cmd_connect(ngx_rtmp_session_t *s, ngx_rtmp_connect_t *v)
322325
{ \
323326
s->name.len = ngx_strlen(v->name); \
324327
s->name.data = ngx_palloc(s->connection->pool, s->name.len); \
328+
if (s->name.data == NULL) { \
329+
return NGX_ERROR; \
330+
} \
325331
ngx_memcpy(s->name.data, v->name, s->name.len); \
326332
} \
327333
} while (0)

ngx_rtmp_codec_module.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ ngx_rtmp_codec_av(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
209209
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
210210
if (ctx == NULL) {
211211
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_codec_ctx_t));
212+
if (ctx == NULL) {
213+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
214+
"codec: failed to allocate for ctx");
215+
return NGX_ERROR;
216+
}
217+
212218
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_codec_module);
213219
}
214220

@@ -881,6 +887,12 @@ ngx_rtmp_codec_meta_data(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
881887
ctx = ngx_rtmp_get_module_ctx(s, ngx_rtmp_codec_module);
882888
if (ctx == NULL) {
883889
ctx = ngx_pcalloc(s->connection->pool, sizeof(ngx_rtmp_codec_ctx_t));
890+
if (ctx == NULL) {
891+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
892+
"codec: failed to allocate for ctx (meta)");
893+
return NGX_ERROR;
894+
}
895+
884896
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_codec_module);
885897
}
886898

ngx_rtmp_handshake.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,12 @@ ngx_rtmp_handshake_parse_challenge(ngx_rtmp_session_t *s,
321321
b->pos += offs;
322322
b->last = b->pos + NGX_RTMP_HANDSHAKE_KEYLEN;
323323
s->hs_digest = ngx_palloc(s->connection->pool, NGX_RTMP_HANDSHAKE_KEYLEN);
324+
if (s->hs_digest == NULL) {
325+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
326+
"handshake: failed to allocate for digest");
327+
return NGX_ERROR;
328+
}
329+
324330
if (ngx_rtmp_make_digest(key, b, NULL, s->hs_digest, s->connection->log)
325331
!= NGX_OK)
326332
{

ngx_rtmp_live_module.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,9 @@ ngx_rtmp_live_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
300300

301301
conf->streams = ngx_pcalloc(cf->pool,
302302
sizeof(ngx_rtmp_live_stream_t *) * conf->nbuckets);
303+
if (conf->streams == NULL) {
304+
return NGX_CONF_ERROR;
305+
}
303306

304307
return NGX_CONF_OK;
305308
}
@@ -360,6 +363,11 @@ ngx_rtmp_live_get_stream(ngx_rtmp_session_t *s, u_char *name, int create)
360363
lacf->free_streams = lacf->free_streams->next;
361364
} else {
362365
*stream = ngx_palloc(lacf->pool, sizeof(ngx_rtmp_live_stream_t));
366+
if (*stream == NULL) {
367+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
368+
"live: failed to allocate for stream");
369+
return NULL;
370+
}
363371
}
364372
ngx_memzero(*stream, sizeof(ngx_rtmp_live_stream_t));
365373
ngx_memcpy((*stream)->name, name,
@@ -624,8 +632,22 @@ ngx_rtmp_live_join(ngx_rtmp_session_t *s, u_char *name, unsigned publisher)
624632
if (ctx == NULL) {
625633
ctx = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_live_ctx_t));
626634
if (ctx == NULL) {
627-
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
628-
"live: failed to allocate for ctx");
635+
if (publisher) {
636+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
637+
"live: failed to allocate for publish ctx");
638+
639+
ngx_rtmp_send_status(s, "NetStream.Publish.Failed", "error",
640+
"Failed to allocate memory");
641+
} else {
642+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
643+
"live: failed to allocate for play ctx");
644+
645+
ngx_rtmp_send_status(s, "NetStream.Play.Failed", "error",
646+
"Failed to allocate memory");
647+
}
648+
649+
ngx_rtmp_finalize_session(s);
650+
629651
return;
630652
}
631653

@@ -1429,22 +1451,27 @@ ngx_rtmp_live_on_fcpublish(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
14291451

14301452
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
14311453
if (lacf == NULL) {
1432-
ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0,
1454+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
14331455
"live: FCPublish - no live config!");
14341456
return NGX_ERROR;
14351457
}
14361458

14371459
if (!lacf->live || in == NULL || in->buf == NULL) {
1438-
ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0,
1460+
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
14391461
"live: FCPublish - no live or no buffer!");
14401462
return NGX_OK;
14411463
}
14421464

14431465
ngx_memzero(&v, sizeof(v));
1444-
ngx_rtmp_receive_amf(s, in, in_elts,
1445-
sizeof(in_elts) / sizeof(in_elts[0]));
1466+
if (ngx_rtmp_receive_amf(s, in, in_elts,
1467+
sizeof(in_elts) / sizeof(in_elts[0])))
1468+
{
1469+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1470+
"live: FCPublish - error receiving amf data");
1471+
return NGX_ERROR;
1472+
}
14461473

1447-
ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0,
1474+
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
14481475
"live: onFCPublish: stream='%s'", v.stream);
14491476

14501477
return ngx_rtmp_send_fcpublish(s, v.stream);
@@ -1480,22 +1507,27 @@ ngx_rtmp_live_on_fcunpublish(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
14801507

14811508
lacf = ngx_rtmp_get_module_app_conf(s, ngx_rtmp_live_module);
14821509
if (lacf == NULL) {
1483-
ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0,
1510+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
14841511
"live: FCUnpublish - no live config!");
14851512
return NGX_ERROR;
14861513
}
14871514

14881515
if (!lacf->live || in == NULL || in->buf == NULL) {
1489-
ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0,
1516+
ngx_log_debug0(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
14901517
"live: FCUnpublish - no live or no buffer!");
14911518
return NGX_OK;
14921519
}
14931520

14941521
ngx_memzero(&v, sizeof(v));
1495-
ngx_rtmp_receive_amf(s, in, in_elts,
1496-
sizeof(in_elts) / sizeof(in_elts[0]));
1522+
if (ngx_rtmp_receive_amf(s, in, in_elts,
1523+
sizeof(in_elts) / sizeof(in_elts[0])))
1524+
{
1525+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1526+
"live: FCUnpublish - error receiving amf data");
1527+
return NGX_ERROR;
1528+
}
14971529

1498-
ngx_log_error(NGX_LOG_DEBUG, s->connection->log, 0,
1530+
ngx_log_debug1(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,
14991531
"live: onFCUnpublish: stream='%s'", v.stream);
15001532

15011533
return ngx_rtmp_send_fcunpublish(s, v.stream);

ngx_rtmp_play_module.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,12 @@ ngx_rtmp_play_play(ngx_rtmp_session_t *s, ngx_rtmp_play_t *v)
733733

734734
if (ctx == NULL) {
735735
ctx = ngx_palloc(s->connection->pool, sizeof(ngx_rtmp_play_ctx_t));
736+
if (ctx == NULL) {
737+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
738+
"play: failed to allocate for ctx");
739+
return NGX_ERROR;
740+
}
741+
736742
ngx_rtmp_set_ctx(s, ctx, ngx_rtmp_play_module);
737743
}
738744

ngx_rtmp_relay_module.c

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ ngx_rtmp_relay_merge_app_conf(ngx_conf_t *cf, void *parent, void *child)
222222

223223
conf->ctx = ngx_pcalloc(cf->pool, sizeof(ngx_rtmp_relay_ctx_t *)
224224
* conf->nbuckets);
225+
if (conf->ctx == NULL) {
226+
return NGX_CONF_ERROR;
227+
}
225228

226229
ngx_conf_merge_value(conf->session_relay, prev->session_relay, 0);
227230
ngx_conf_merge_msec_value(conf->buflen, prev->buflen, 5000);
@@ -1528,11 +1531,23 @@ ngx_rtmp_relay_on_status(ngx_rtmp_session_t *s, ngx_rtmp_header_t *h,
15281531

15291532
ngx_memzero(&v, sizeof(v));
15301533
if (h->type == NGX_RTMP_MSG_AMF_META) {
1531-
ngx_rtmp_receive_amf(s, in, in_elts_meta,
1532-
sizeof(in_elts_meta) / sizeof(in_elts_meta[0]));
1534+
if (ngx_rtmp_receive_amf(s, in, in_elts_meta,
1535+
sizeof(in_elts_meta) / sizeof(in_elts_meta[0])))
1536+
{
1537+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1538+
"relay: error receiving meta");
1539+
1540+
return NGX_ERROR;
1541+
}
15331542
} else {
1534-
ngx_rtmp_receive_amf(s, in, in_elts,
1535-
sizeof(in_elts) / sizeof(in_elts[0]));
1543+
if (ngx_rtmp_receive_amf(s, in, in_elts,
1544+
sizeof(in_elts) / sizeof(in_elts[0])))
1545+
{
1546+
ngx_log_error(NGX_LOG_ERR, s->connection->log, 0,
1547+
"relay: error receiving status");
1548+
1549+
return NGX_ERROR;
1550+
}
15361551
}
15371552

15381553
ngx_log_debug3(NGX_LOG_DEBUG_RTMP, s->connection->log, 0,

0 commit comments

Comments
 (0)