@@ -398,6 +398,8 @@ ST_FUNC int asm_int_expr(TCCState *s1)
398398 asm_expr (s1 , & e );
399399 if (e .sym )
400400 expect ("constant" );
401+ if ((int )e .v != e .v )
402+ tcc_error ("integer out of range %lld" , (long long )e .v );
401403 return e .v ;
402404}
403405
@@ -515,7 +517,7 @@ static void asm_parse_directive(TCCState *s1, int global)
515517 tok1 = TOK_ASMDIR_align ;
516518 }
517519 if (tok1 == TOK_ASMDIR_align || tok1 == TOK_ASMDIR_balign ) {
518- if (n < 0 || (n & (n - 1 )) != 0 )
520+ if (n <= 0 || (n & (n - 1 )) != 0 )
519521 tcc_error ("alignment must be a positive power of two" );
520522 offset = (ind + n - 1 ) & - n ;
521523 size = offset - ind ;
@@ -677,17 +679,21 @@ static void asm_parse_directive(TCCState *s1, int global)
677679 }
678680 case TOK_ASMDIR_org :
679681 {
680- unsigned long n ;
681682 ExprValue e ;
682683 ElfSym * esym ;
683684 next ();
684685 asm_expr (s1 , & e );
685686 n = e .v ;
687+ if (n != e .v || n < 0 )
688+ range :
689+ tcc_error (".org out of range" );
686690 esym = elfsym (e .sym );
687691 if (esym ) {
688692 if (esym -> st_shndx != cur_text_section -> sh_num )
689693 expect ("constant or same-section symbol" );
690694 n += esym -> st_value ;
695+ if (n < esym -> st_value )
696+ goto range ;
691697 }
692698 if (n < ind )
693699 tcc_error ("attempt to .org backwards" );
@@ -713,6 +719,8 @@ static void asm_parse_directive(TCCState *s1, int global)
713719 do {
714720 Sym * sym ;
715721 next ();
722+ if (tok < TOK_IDENT || tok >= SYM_FIRST_ANOM )
723+ tcc_error ("Illegal symbol %s" , get_tok_str (tok1 , NULL ));
716724 sym = get_asm_sym (tok , NULL );
717725 if (tok1 != TOK_ASMDIR_hidden )
718726 sym -> type .t &= ~VT_STATIC ;
@@ -799,7 +807,7 @@ static void asm_parse_directive(TCCState *s1, int global)
799807 if (tok == TOK_STR )
800808 pstrcat (ident , sizeof (ident ), tokc .str .data );
801809 else
802- pstrcat (ident , sizeof (ident ), get_tok_str (tok , NULL ));
810+ pstrcat (ident , sizeof (ident ), get_tok_str (tok , & tokc ));
803811 tcc_warning_c (warn_unsupported )("ignoring .ident %s" , ident );
804812 next ();
805813 }
@@ -808,10 +816,11 @@ static void asm_parse_directive(TCCState *s1, int global)
808816 {
809817 Sym * sym ;
810818
819+ tok1 = tok ;
811820 next ();
812821 sym = asm_label_find (tok );
813822 if (!sym ) {
814- tcc_error ("label not found: %s" , get_tok_str (tok , NULL ));
823+ tcc_error ("label not found: %s" , get_tok_str (tok1 , NULL ));
815824 }
816825 /* XXX .size name,label2-label1 */
817826 tcc_warning_c (warn_unsupported )("ignoring .size %s,*" , get_tok_str (tok , NULL ));
@@ -828,7 +837,10 @@ static void asm_parse_directive(TCCState *s1, int global)
828837 const char * newtype ;
829838 int st_type ;
830839
840+ tok1 = tok ;
831841 next ();
842+ if (tok < TOK_IDENT || tok >= SYM_FIRST_ANOM )
843+ tcc_error ("Illegal symbol %s" , get_tok_str (tok1 , NULL ));
832844 sym = get_asm_sym (tok , NULL );
833845 next ();
834846 skip (',' );
@@ -1177,11 +1189,14 @@ static void subst_asm_operands(ASMOperand *operands, int nb_operands,
11771189 modifier = * str ++ ;
11781190 index = find_constraint (operands , nb_operands , str , & str );
11791191 if (index < 0 )
1192+ error :
11801193 tcc_error ("invalid operand reference after %%" );
11811194 op = & operands [index ];
11821195 if (modifier == 'l' ) {
11831196 cstr_cat (out_str , get_tok_str (op -> is_label , NULL ), -1 );
11841197 } else {
1198+ if (op -> vt == NULL )
1199+ goto error ;
11851200 sv = * op -> vt ;
11861201 if (op -> reg >= 0 ) {
11871202 sv .r = op -> reg ;
@@ -1318,6 +1333,8 @@ ST_FUNC void asm_instr(void)
13181333 tcc_error ("too many asm operands" );
13191334 if (tok < TOK_UIDENT )
13201335 expect ("label identifier" );
1336+ memset (operands + nb_operands + nb_labels , 0 ,
1337+ sizeof (operands [0 ]));
13211338 operands [nb_operands + nb_labels ++ ].id = tok ;
13221339
13231340 csym = label_find (tok );
0 commit comments