@@ -502,19 +502,22 @@ static int _yr_parser_write_string(
502
502
literal_string -> length + 1 , // +1 to include terminating NULL
503
503
& ref );
504
504
505
+ if (result != ERROR_SUCCESS )
506
+ goto cleanup ;
507
+
505
508
string -> length = (uint32_t ) literal_string -> length ;
506
509
string -> string = (uint8_t * ) yr_arena_ref_to_ptr (compiler -> arena , & ref );
507
510
508
- if ( result == ERROR_SUCCESS )
509
- {
510
- result = yr_atoms_extract_from_string (
511
- & compiler -> atoms_config ,
512
- ( uint8_t * ) literal_string -> c_string ,
513
- ( int32_t ) literal_string -> length ,
514
- modifier ,
515
- & atom_list ,
516
- min_atom_quality );
517
- }
511
+ result = yr_atoms_extract_from_string (
512
+ & compiler -> atoms_config ,
513
+ ( uint8_t * ) literal_string -> c_string ,
514
+ ( int32_t ) literal_string -> length ,
515
+ modifier ,
516
+ & atom_list ,
517
+ min_atom_quality );
518
+
519
+ if ( result != ERROR_SUCCESS )
520
+ goto cleanup ;
518
521
}
519
522
else
520
523
{
@@ -524,37 +527,72 @@ static int _yr_parser_write_string(
524
527
// variable-length portions.
525
528
modifier .flags &= ~STRING_FLAGS_FIXED_OFFSET ;
526
529
530
+ // Save the position where the RE forward code starts for later reference.
531
+ yr_arena_off_t forward_code_start = yr_arena_get_current_offset (
532
+ compiler -> arena , YR_RE_CODE_SECTION );
533
+
527
534
// Emit forwards code
528
535
result = yr_re_ast_emit_code (re_ast , compiler -> arena , false);
529
536
537
+ if (result != ERROR_SUCCESS )
538
+ goto cleanup ;
539
+
530
540
// Emit backwards code
531
- if (result == ERROR_SUCCESS )
532
- result = yr_re_ast_emit_code (re_ast , compiler -> arena , true);
541
+ result = yr_re_ast_emit_code (re_ast , compiler -> arena , true);
533
542
534
- if (result == ERROR_SUCCESS )
535
- result = yr_atoms_extract_from_re (
536
- & compiler -> atoms_config ,
537
- re_ast ,
538
- modifier ,
539
- & atom_list ,
540
- min_atom_quality );
543
+ if (result != ERROR_SUCCESS )
544
+ goto cleanup ;
545
+
546
+ // Extract atoms from the regular expression.
547
+ result = yr_atoms_extract_from_re (
548
+ & compiler -> atoms_config ,
549
+ re_ast ,
550
+ modifier ,
551
+ & atom_list ,
552
+ min_atom_quality );
553
+
554
+ if (result != ERROR_SUCCESS )
555
+ goto cleanup ;
556
+
557
+ // If no atom was extracted let's add a zero-length atom.
558
+ if (atom_list == NULL )
559
+ {
560
+ atom_list = (YR_ATOM_LIST_ITEM * ) yr_malloc (sizeof (YR_ATOM_LIST_ITEM ));
561
+
562
+ if (atom_list == NULL )
563
+ {
564
+ result = ERROR_INSUFFICIENT_MEMORY ;
565
+ goto cleanup ;
566
+ }
567
+
568
+ atom_list -> atom .length = 0 ;
569
+ atom_list -> backtrack = 0 ;
570
+ atom_list -> backward_code_ref = YR_ARENA_NULL_REF ;
571
+ atom_list -> next = NULL ;
572
+
573
+ yr_arena_ptr_to_ref (
574
+ compiler -> arena ,
575
+ yr_arena_get_ptr (
576
+ compiler -> arena , YR_RE_CODE_SECTION , forward_code_start ),
577
+ & (atom_list -> forward_code_ref ));
578
+ }
541
579
}
542
580
543
581
string -> flags = modifier .flags ;
544
582
string -> rule_idx = compiler -> current_rule_idx ;
545
583
string -> idx = compiler -> current_string_idx ;
546
584
string -> fixed_offset = YR_UNDEFINED ;
547
585
548
- if ( result == ERROR_SUCCESS )
549
- {
550
- // Add the string to Aho-Corasick automaton.
551
- result = yr_ac_add_string (
552
- compiler -> automaton ,
553
- string ,
554
- compiler -> current_string_idx ,
555
- atom_list ,
556
- compiler -> arena );
557
- }
586
+ // Add the string to Aho-Corasick automaton.
587
+ result = yr_ac_add_string (
588
+ compiler -> automaton ,
589
+ string ,
590
+ compiler -> current_string_idx ,
591
+ atom_list ,
592
+ compiler -> arena );
593
+
594
+ if ( result != ERROR_SUCCESS )
595
+ goto cleanup ;
558
596
559
597
if (modifier .flags & STRING_FLAGS_LITERAL )
560
598
{
@@ -580,6 +618,7 @@ static int _yr_parser_write_string(
580
618
581
619
compiler -> current_string_idx ++ ;
582
620
621
+ cleanup :
583
622
if (free_literal )
584
623
yr_free (literal_string );
585
624
@@ -761,11 +800,9 @@ int yr_parser_reduce_string_declaration(
761
800
{
762
801
if (result == ERROR_UNKNOWN_ESCAPE_SEQUENCE )
763
802
{
764
- yywarning (
765
- yyscanner ,
766
- "unknown escape sequence" );
803
+ yywarning (yyscanner , "unknown escape sequence" );
767
804
}
768
- else
805
+ else
769
806
{
770
807
snprintf (
771
808
message ,
@@ -1148,7 +1185,7 @@ int yr_parser_reduce_string_identifier(
1148
1185
YR_STRING * string ;
1149
1186
YR_COMPILER * compiler = yyget_extra (yyscanner );
1150
1187
1151
- if (strcmp (identifier , "$" ) == 0 ) // is an anonymous string ?
1188
+ if (strcmp (identifier , "$" ) == 0 ) // is an anonymous string ?
1152
1189
{
1153
1190
if (compiler -> loop_for_of_var_index >= 0 ) // inside a loop ?
1154
1191
{
0 commit comments