@@ -518,29 +518,52 @@ def check(cfunc, ctree_item):
518
518
if expression :
519
519
expression = expression .to_specific_type
520
520
if expression .op == idaapi .cot_asg and \
521
- expression .x .op in (idaapi .cot_var , idaapi .cot_obj , idaapi .cot_memptr , idaapi .cot_memref ) \
522
- and expression .y .op == idaapi .cot_cast :
521
+ expression .x .op in (idaapi .cot_var , idaapi .cot_obj , idaapi .cot_memptr , idaapi .cot_memref ):
522
+
523
+ right_expr = expression .y
524
+ right_tinfo = right_expr .x .type if right_expr .op == idaapi .cot_cast else right_expr .type
525
+
526
+ # Check if both left and right parts of expression are of the same types.
527
+ # If no then we can recast then.
528
+ if right_tinfo .dstr () == expression .x .type .dstr ():
529
+ return
530
+
523
531
if expression .x .op == idaapi .cot_var :
524
532
variable = cfunc .get_lvars ()[expression .x .v .idx ]
525
533
idaapi .update_action_label (RecastItemLeft .name , 'Recast Variable "{0}"' .format (variable .name ))
526
- return RECAST_LOCAL_VARIABLE , expression . y . x . type , variable
534
+ return RECAST_LOCAL_VARIABLE , right_tinfo , variable
527
535
elif expression .x .op == idaapi .cot_obj :
528
536
idaapi .update_action_label (RecastItemLeft .name , 'Recast Global' )
529
- return RECAST_GLOBAL_VARIABLE , expression . y . x . type , expression .x .obj_ea
537
+ return RECAST_GLOBAL_VARIABLE , right_tinfo , expression .x .obj_ea
530
538
elif expression .x .op == idaapi .cot_memptr :
531
539
idaapi .update_action_label (RecastItemLeft .name , 'Recast Field' )
532
- return RECAST_STRUCTURE , expression .x .x .type .get_pointed_object ().dstr (), expression .x .m , expression . y . x . type
540
+ return RECAST_STRUCTURE , expression .x .x .type .get_pointed_object ().dstr (), expression .x .m , right_tinfo
533
541
elif expression .x .op == idaapi .cot_memref :
534
542
idaapi .update_action_label (RecastItemLeft .name , 'Recast Field' )
535
- return RECAST_STRUCTURE , expression .x .x .type .dstr (), expression .x .m , expression . y . x . type
543
+ return RECAST_STRUCTURE , expression .x .x .type .dstr (), expression .x .m , right_tinfo
536
544
537
545
elif expression .op == idaapi .cit_return :
546
+
547
+ idaapi .update_action_label (RecastItemLeft .name , "Recast Return" )
538
548
child = child or expression .creturn .expr
549
+
539
550
if child .op == idaapi .cot_cast :
540
- idaapi .update_action_label (RecastItemLeft .name , "Recast Return" )
541
551
return RECAST_RETURN , child .x .type , None
542
552
553
+ func_tinfo = idaapi .tinfo_t ()
554
+ cfunc .get_func_type (func_tinfo )
555
+ rettype = func_tinfo .get_rettype ()
556
+
557
+ print func_tinfo .get_rettype ().dstr (), child .type .dstr ()
558
+ if func_tinfo .get_rettype ().dstr () != child .type .dstr ():
559
+ return RECAST_RETURN , child .type , None
560
+
543
561
elif expression .op == idaapi .cot_call :
562
+
563
+ if expression .x .op == idaapi .cot_memptr :
564
+ # TODO: Recast arguments of virtual functions
565
+ return
566
+
544
567
if child and child .op == idaapi .cot_cast :
545
568
if child .cexpr .x .op == idaapi .cot_memptr :
546
569
idaapi .update_action_label (RecastItemLeft .name , 'Recast Virtual Function' )
@@ -642,23 +665,32 @@ def check(cfunc, ctree_item):
642
665
if ctree_item .citype == idaapi .VDI_EXPR :
643
666
644
667
expression = ctree_item .it
668
+
645
669
while expression and expression .op != idaapi .cot_cast :
646
670
expression = expression .to_specific_type
647
671
expression = cfunc .body .find_parent_of (expression )
648
672
if expression :
649
673
expression = expression .to_specific_type
674
+
675
+ if expression .x .op == idaapi .cot_ref :
676
+ new_type = expression .type .get_pointed_object ()
677
+ expression = expression .x
678
+ else :
679
+ new_type = expression .type
680
+
650
681
if expression .x .op == idaapi .cot_var :
682
+
651
683
variable = cfunc .get_lvars ()[expression .x .v .idx ]
652
684
idaapi .update_action_label (RecastItemRight .name , 'Recast Variable "{0}"' .format (variable .name ))
653
- return RECAST_LOCAL_VARIABLE , expression . type , variable
685
+ return RECAST_LOCAL_VARIABLE , new_type , variable
654
686
655
687
elif expression .x .op == idaapi .cot_obj :
656
688
idaapi .update_action_label (RecastItemRight .name , 'Recast Global' )
657
- return RECAST_GLOBAL_VARIABLE , expression . type , expression .x .obj_ea
689
+ return RECAST_GLOBAL_VARIABLE , new_type , expression .x .obj_ea
658
690
659
691
elif expression .x .op == idaapi .cot_call :
660
692
idaapi .update_action_label (RecastItemRight .name , "Recast Return" )
661
- return RECAST_RETURN , expression . type , expression .x .x .obj_ea
693
+ return RECAST_RETURN , new_type , expression .x .x .obj_ea
662
694
663
695
664
696
class RenameInside (idaapi .action_handler_t ):
0 commit comments