@@ -370,51 +370,70 @@ void html_tag_arg_add(tag_arguments_t *tags,
370
370
const char * tag , char * value )
371
371
{
372
372
int len , i ;
373
- tags -> count ++ ;
374
- tags -> tag = (unsigned char * * )cli_max_realloc_or_free (tags -> tag ,
375
- tags -> count * sizeof (char * ));
376
- if (!tags -> tag ) {
373
+ int tagCnt = tags -> count ;
374
+ int valueCnt = tags -> count ;
375
+ int contentCnt = 0 ;
376
+ unsigned char * * tmp = NULL ;
377
+
378
+ tmp = (unsigned char * * )cli_max_realloc (tags -> tag , (tagCnt + 1 ) * sizeof (char * ));
379
+ if (!tmp ) {
377
380
goto done ;
378
381
}
379
- tags -> value = (unsigned char * * )cli_max_realloc_or_free (tags -> value ,
380
- tags -> count * sizeof (char * ));
381
- if (!tags -> value ) {
382
+ tags -> tag = tmp ;
383
+ tagCnt ++ ;
384
+
385
+ tmp = (unsigned char * * )cli_max_realloc (tags -> value , (valueCnt + 1 ) * sizeof (char * ));
386
+ if (!tmp ) {
382
387
goto done ;
383
388
}
389
+ tags -> value = tmp ;
390
+ valueCnt ++ ;
391
+
384
392
if (tags -> scanContents ) {
385
- tags -> contents = ( unsigned char * * ) cli_max_realloc_or_free ( tags -> contents ,
386
- tags -> count * sizeof (* tags -> contents ));
387
- if (!tags -> contents ) {
393
+ contentCnt = tags -> count ;
394
+ tmp = ( unsigned char * * ) cli_max_realloc ( tags -> contents , ( contentCnt + 1 ) * sizeof (* tags -> contents ));
395
+ if (!tmp ) {
388
396
goto done ;
389
397
}
390
- tags -> contents [tags -> count - 1 ] = NULL ;
398
+ tags -> contents = tmp ;
399
+ tags -> contents [contentCnt ] = NULL ;
400
+ contentCnt ++ ;
391
401
}
392
- tags -> tag [tags -> count - 1 ] = (unsigned char * )cli_safer_strdup (tag );
402
+
403
+ tags -> tag [tags -> count ] = (unsigned char * )cli_safer_strdup (tag );
393
404
if (value ) {
394
405
if (* value == '"' ) {
395
- tags -> value [tags -> count - 1 ] = (unsigned char * )cli_safer_strdup (value + 1 );
396
- len = strlen ((const char * )value + 1 );
406
+ tags -> value [tags -> count ] = (unsigned char * )cli_safer_strdup (value + 1 );
407
+ if (NULL == tags -> value [tags -> count ]) {
408
+ goto done ;
409
+ }
410
+ len = strlen ((const char * )value + 1 );
397
411
if (len > 0 ) {
398
- tags -> value [tags -> count - 1 ][len - 1 ] = '\0' ;
412
+ tags -> value [tags -> count ][len - 1 ] = '\0' ;
399
413
}
400
414
} else {
401
- tags -> value [tags -> count - 1 ] = (unsigned char * )cli_safer_strdup (value );
415
+ tags -> value [tags -> count ] = (unsigned char * )cli_safer_strdup (value );
402
416
}
403
417
} else {
404
- tags -> value [tags -> count - 1 ] = NULL ;
418
+ tags -> value [tags -> count ] = NULL ;
405
419
}
420
+
421
+ tags -> count ++ ;
406
422
return ;
407
423
408
424
done :
409
425
/* Bad error - can't do 100% recovery */
410
- tags -> count -- ;
411
- for (i = 0 ; i < tags -> count ; i ++ ) {
426
+ for (i = 0 ; i < tagCnt ; i ++ ) {
412
427
if (tags -> tag ) {
413
428
free (tags -> tag [i ]);
414
429
}
430
+ }
431
+ for (i = 0 ; i < valueCnt ; i ++ ) {
415
432
if (tags -> value ) {
416
433
free (tags -> value [i ]);
417
434
}
435
+ }
436
+ for (i = 0 ; i < contentCnt ; i ++ ) {
418
437
if (tags -> contents ) {
419
438
if (tags -> contents [i ])
420
439
free (tags -> contents [i ]);
@@ -649,7 +668,46 @@ static void js_process(struct parser_state *js_state, const unsigned char *js_be
649
668
}
650
669
}
651
670
652
- static bool cli_html_normalise (cli_ctx * ctx , int fd , m_area_t * m_area , const char * dirname , tag_arguments_t * hrefs , const struct cli_dconf * dconf )
671
+ bool html_insert_form_data (const char * const value , form_data_t * tags )
672
+ {
673
+ bool bRet = false;
674
+ size_t cnt = tags -> count + 1 ;
675
+ char * * tmp = NULL ;
676
+
677
+ /*
678
+ * Do NOT use cli_max_realloc_or_free because all the previously malloc'd tag
679
+ * values will be leaked when tag is free'd in the case where realloc fails.
680
+ */
681
+ tmp = cli_max_realloc (tags -> urls , cnt * sizeof (unsigned char * ));
682
+ if (!tmp ) {
683
+ goto done ;
684
+ }
685
+ tags -> urls = tmp ;
686
+
687
+ tags -> urls [tags -> count ] = cli_safer_strdup (value );
688
+ if (tags -> urls [tags -> count ]) {
689
+ tags -> count = cnt ;
690
+ }
691
+
692
+ bRet = true;
693
+ done :
694
+ if (!bRet ) {
695
+ memset (tags , 0 , sizeof (* tags ));
696
+ }
697
+
698
+ return bRet ;
699
+ }
700
+
701
+ void html_form_data_tag_free (form_data_t * tags )
702
+ {
703
+ size_t i ;
704
+ for (i = 0 ; i < tags -> count ; i ++ ) {
705
+ CLI_FREE_AND_SET_NULL (tags -> urls [i ]);
706
+ }
707
+ CLI_FREE_AND_SET_NULL (tags -> urls );
708
+ }
709
+
710
+ static bool cli_html_normalise (cli_ctx * ctx , int fd , m_area_t * m_area , const char * dirname , tag_arguments_t * hrefs , const struct cli_dconf * dconf , form_data_t * form_data )
653
711
{
654
712
int fd_tmp , tag_length = 0 , tag_arg_length = 0 ;
655
713
bool binary , retval = false, escape = false, hex = false;
@@ -659,7 +717,7 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
659
717
FILE * stream_in = NULL ;
660
718
html_state state = HTML_NORM , next_state = HTML_BAD_STATE , saved_next_state = HTML_BAD_STATE ;
661
719
char filename [1024 ], tag [HTML_STR_LENGTH + 1 ], tag_arg [HTML_STR_LENGTH + 1 ];
662
- char tag_val [HTML_STR_LENGTH + 1 ], * tmp_file , * arg_value ;
720
+ char tag_val [HTML_STR_LENGTH + 1 ], * tmp_file = NULL , * arg_value = NULL ;
663
721
unsigned char * line = NULL , * ptr , * ptr_screnc = NULL ;
664
722
tag_arguments_t tag_args ;
665
723
quoted_state quoted = NOT_QUOTED ;
@@ -1224,8 +1282,9 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
1224
1282
href_contents_begin = ptr ;
1225
1283
}
1226
1284
if (strcmp (tag , "/form" ) == 0 ) {
1227
- if (in_form_action )
1285
+ if (in_form_action ) {
1228
1286
free (in_form_action );
1287
+ }
1229
1288
in_form_action = NULL ;
1230
1289
}
1231
1290
} else if (strcmp (tag , "script" ) == 0 ) {
@@ -1310,9 +1369,13 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
1310
1369
} else if (strcmp (tag , "form" ) == 0 && hrefs -> scanContents ) {
1311
1370
const char * arg_action_value = html_tag_arg_value (& tag_args , "action" );
1312
1371
if (arg_action_value ) {
1313
- if (in_form_action )
1372
+ if (in_form_action ) {
1314
1373
free (in_form_action );
1374
+ }
1315
1375
in_form_action = (unsigned char * )cli_safer_strdup (arg_action_value );
1376
+ if (form_data ) {
1377
+ html_insert_form_data ((const char * const )in_form_action , form_data );
1378
+ }
1316
1379
}
1317
1380
} else if (strcmp (tag , "img" ) == 0 ) {
1318
1381
arg_value = html_tag_arg_value (& tag_args , "src" );
@@ -1917,8 +1980,9 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
1917
1980
done :
1918
1981
if (line ) /* only needed for done case */
1919
1982
free (line );
1920
- if (in_form_action )
1983
+ if (in_form_action ) {
1921
1984
free (in_form_action );
1985
+ }
1922
1986
if (in_ahref ) /* tag not closed, force closing */
1923
1987
html_tag_contents_done (hrefs , in_ahref , & contents );
1924
1988
@@ -1960,6 +2024,11 @@ static bool cli_html_normalise(cli_ctx *ctx, int fd, m_area_t *m_area, const cha
1960
2024
}
1961
2025
1962
2026
bool html_normalise_mem (cli_ctx * ctx , unsigned char * in_buff , off_t in_size , const char * dirname , tag_arguments_t * hrefs , const struct cli_dconf * dconf )
2027
+ {
2028
+ return html_normalise_mem_form_data (ctx , in_buff , in_size , dirname , hrefs , dconf , NULL );
2029
+ }
2030
+
2031
+ bool html_normalise_mem_form_data (cli_ctx * ctx , unsigned char * in_buff , off_t in_size , const char * dirname , tag_arguments_t * hrefs , const struct cli_dconf * dconf , form_data_t * form_data )
1963
2032
{
1964
2033
m_area_t m_area ;
1965
2034
@@ -1968,18 +2037,23 @@ bool html_normalise_mem(cli_ctx *ctx, unsigned char *in_buff, off_t in_size, con
1968
2037
m_area .offset = 0 ;
1969
2038
m_area .map = NULL ;
1970
2039
1971
- return cli_html_normalise (ctx , -1 , & m_area , dirname , hrefs , dconf );
2040
+ return cli_html_normalise (ctx , -1 , & m_area , dirname , hrefs , dconf , form_data );
1972
2041
}
1973
2042
1974
2043
bool html_normalise_map (cli_ctx * ctx , fmap_t * map , const char * dirname , tag_arguments_t * hrefs , const struct cli_dconf * dconf )
2044
+ {
2045
+ return html_normalise_map_form_data (ctx , map , dirname , hrefs , dconf , NULL );
2046
+ }
2047
+
2048
+ bool html_normalise_map_form_data (cli_ctx * ctx , fmap_t * map , const char * dirname , tag_arguments_t * hrefs , const struct cli_dconf * dconf , form_data_t * form_data )
1975
2049
{
1976
2050
bool retval = false;
1977
2051
m_area_t m_area ;
1978
2052
1979
2053
m_area .length = map -> len ;
1980
2054
m_area .offset = 0 ;
1981
2055
m_area .map = map ;
1982
- retval = cli_html_normalise (ctx , -1 , & m_area , dirname , hrefs , dconf );
2056
+ retval = cli_html_normalise (ctx , -1 , & m_area , dirname , hrefs , dconf , form_data );
1983
2057
return retval ;
1984
2058
}
1985
2059
0 commit comments