soarsmu / attack-pretrain-models-of-code

Replication Package for "Natural Attack for Pre-trained Models of Code", ICSE 2022
MIT License
38 stars 8 forks source link

Variable Substitution Algorithm Bug #76

Open 126pikaqiu opened 1 year ago

126pikaqiu commented 1 year ago

In the get_example_batch function of run_parser.py, there is an error in the substitution algorithm for variable names. In your code, the problem of variable name length inconsistency before and after substitution is handled by accumulating differences, but only if the row is replaced sequentially from left to right. Failure to do so results in substitution misplaced code that produces syntax errors.

Your code in get_example_batch:

    for index, pos in enumerate(replace_pos[line]):

        code[line] = code[line][:pos[3]+diff] + pos[1] + code[line][pos[4]+diff:]

        diff += pos[2]

Bug fixed code in get_example_batch:

    for index, pos in enumerate(list(sorted(replace_pos[line], key=lambda x:x[3]))):

        code[line] = code[line][:pos[3]+diff] + pos[1] + code[line][pos[4]+diff:]

        diff += pos[2]