Code repairing method and device
By scanning and repairing the code, the use of enhanced tools to generate and update repair information, the problem of overly fixed description of code repair suggestions in the prior art is solved, and the efficiency and accuracy of code repair is improved.
Patent Information
- Application Number
- CN202510198961.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-21
- Publication Date
- 2025-06-03
AI Technical Summary
The existing code scanning tools provide a description of repair suggestions that are too fixed, resulting in inefficient code repair.
By scanning the to-be-processed code, initial repair information is generated, reference repair information is generated using the repair enhancement tool, and reference repair information is updated according to the location to be repaired, code variables and repair types in the initial repair method to obtain target repair information, and finally the target repair information is used to repair the code.
Improves the efficiency and accuracy of code repair, making repair information more suitable for specific code descriptions, and is easy for developers to understand and implement.
Smart Images

Figure CN120085900A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and more particularly to a code repair method and device. Background Art
[0002] In the process of software development, code quality is very important. Through code scanning tools, developers can timely discover potential problems during the coding process and repair the code in a timely manner.
[0003] However, the repair suggestions given after code scanning are fixed descriptions, resulting in the same basic description regardless of the code problem, making the code repair efficiency low. Summary of the Invention
[0004] In view of the above problems, this application provides a code repair method and device.
[0005] According to a first aspect of this application, a code repair method is provided, including: scanning the code to be processed to obtain initial repair information of the code to be processed, where the initial repair information includes the position to be repaired and the initial repair method of the code to be processed; using a repair enhancement tool to generate reference repair information for the code to be processed according to the initial repair information, where the reference repair method in the reference repair information has the same repair type as the initial repair method; updating the reference repair information according to the position to be repaired, the code variables in the initial repair method, and the repair type to obtain target repair information; using the target repair information to repair the code to be processed.
[0006] According to an embodiment of this application, the repair enhancement tool includes at least one of the following: a database and a preset model, and the reference repair information includes at least one of the following: recommended repair information and example repair information; using the repair enhancement tool to generate reference repair information for the code according to the code to be processed and the initial repair information, including: matching the code with example codes in the database based on the repair type to obtain a target example code corresponding to the code to be processed, and determining the example repair information of the target example code from the database, where the target example code and the code to be processed belong to the same project; or inputting the code to be processed and the initial repair information into the preset model to obtain the recommended repair information of the code, and the preset model is trained based on repair type labels, historical codes, and historical repair information.
[0007] According to an embodiment of this application, matching the code to be processed with example codes in the database based on the repair type to obtain a target example code corresponding to the code to be processed includes: determining type example codes corresponding to the repair type from the database; and determining the type example code as the target example code when the similarity between the code to be processed and the type example code meets a preset condition.
[0008] According to an embodiment of the present application, the reference repair information is updated according to the position to be repaired, the code variables in the initial repair method, and the repair type to obtain target repair information, including: updating the reference repair information according to the position to be repaired and the code variables of the initial repair method to obtain the updated reference repair information; evaluating the updated reference repair information based on the repair type to obtain an evaluation result, and the target repair information includes: the evaluation result and the updated reference repair information.
[0009] According to an embodiment of the present application, the reference repair information is updated according to the position to be repaired and the code variables of the initial repair method to obtain the updated reference repair information, including: using the position to be repaired to update the reference position to be repaired in the reference repair information; using the code variables of the initial repair method to update the reference repair method to obtain the updated reference repair information.
[0010] According to an embodiment of the present application, the updated reference repair information is evaluated based on the repair type to obtain an evaluation result, including: determining a target repair method from the database according to the repair type; obtaining the code repair result corresponding to the target repair method; evaluating the updated reference repair information according to the code repair result to obtain an evaluation result.
[0011] According to an embodiment of the present application, the target repair information is used to repair the code to be processed, including: when the evaluation result meets the preset evaluation conditions and the reference repair information is example repair information, determining the example repair code of the target example code from the database; using the example repair code to repair the code to be processed.
[0012] According to an embodiment of the present application, the above method further includes: when the evaluation result meets the preset evaluation conditions and the reference repair information is recommended repair information, using the recommended repair code in the target repair information to repair the code to be processed.
[0013] According to an embodiment of the present application, the above method further includes: in response to an operation of changing the target repair information, generating custom repair information according to the change operation; storing the custom repair information in the database as example repair information.
[0014] The second aspect of the present application provides a code repair device, including: a scanning module for scanning the code to be processed to obtain initial repair information of the code to be processed, where the initial repair information includes the position to be repaired and the initial repair method of the code to be processed; a generation module for using a repair enhancement tool to generate reference repair information for the code to be processed according to the initial repair information, and the reference repair method in the reference repair information has the same repair type as the initial repair method; an update module for updating the reference repair information according to the position to be repaired, the code variables and the repair type in the initial repair method to obtain target repair information; and a repair module for using the target repair information to repair the code to be processed.
[0015] The third aspect of the present application provides an electronic device, including: one or more processors; a memory for storing one or more computer programs, wherein the above one or more processors execute the above one or more computer programs to implement the steps of the above method.
[0016] The fourth aspect of the present application further provides a computer-readable storage medium, on which computer programs or instructions are stored, and when the above computer programs or instructions are executed by a processor, the steps of the above method are implemented.
[0017] The fifth aspect of the present application further provides a computer program product, including computer programs or instructions, and when the above computer programs or instructions are executed by a processor, the steps of the above method are implemented.
[0018] According to the embodiments of the present application, by scanning the code to be processed, initial repair information of the code to be processed is obtained, and the initial repair information includes the position to be repaired and the repair method of the code; by using a repair enhancement tool, reference repair information for the code to be processed is generated according to the initial repair information, which can enrich the description of the code repair information. The reference repair information is updated according to the position to be repaired, the code variables and the repair type in the repair method to obtain target repair information, so that the target repair information is adapted to the repair description of the code to be processed, which is convenient for developers to understand. Then, the target repair information is used to repair the code to be processed, thereby improving the repair efficiency of the code. Description of the Drawings
[0019] Through the following description of the embodiments of the present application with reference to the drawings, the above content and other objects, features and advantages of the present application will be clearer. In the drawings:
[0020] Figure 1 Schematically shows an application scenario diagram of the code repair method according to an embodiment of the present application;
[0021] Figure 2 Schematically shows a flowchart of the code repair method according to an embodiment of the present application;
[0022] Figure 3 Schematically shows a flowchart of updating reference repair information according to the position to be repaired, code variables in the initial repair method, and repair types according to an embodiment of the present application to obtain target repair information;
[0023] Figure 4 Schematically shows a flowchart of a code repair method according to another embodiment of the present application;
[0024] Figure 5 Schematically shows a structural block diagram of a code repair device according to an embodiment of the present application; and
[0025] Figure 6 Schematically shows a block diagram of an electronic device suitable for implementing the code repair method according to an embodiment of the present application. Detailed implementation manners
[0026] Hereinafter, embodiments of the present application will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are merely exemplary and are not intended to limit the scope of the present application. In the following detailed description, for the sake of explanation, many specific details are set forth in order to provide a comprehensive understanding of the embodiments of the present application. However, it is obvious that one or more embodiments can be implemented without these specific details. In addition, in the following description, descriptions of well-known structures and technologies are omitted to avoid unnecessarily confusing the concepts of the present application.
[0027] The terms used herein are merely for describing specific embodiments and are not intended to limit the present application. The terms "including", "comprising", etc. used herein indicate the presence of the described features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0028] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein should be interpreted as having a meaning consistent with the context of this specification and should not be interpreted in an idealized or overly rigid manner.
[0029] In the case of using expressions such as "at least one of A, B, and C", generally, it should be interpreted according to the meaning commonly understood by those skilled in the art (for example, "a system having at least one of A, B, and C" should include, but is not limited to, a system having only A, only B, only C, having A and B, having A and C, having B and C, and / or having A, B, and C, etc.).
[0030] In the technical solution of this application, the user information involved (including but not limited to user personal information, user image information, user device information, such as location information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) are all information and data that have been authorized by the user or fully authorized by all parties. Moreover, the processing of relevant data, such as collection, storage, use, processing, transmission, provision, application, and request, all comply with relevant laws, regulations, and standards, adopt necessary confidentiality measures, do not violate public order and good customs, and provide corresponding operation entrances for users to choose to authorize or reject.
[0031] In the scenario of making automated decisions using personal information, the methods, devices, and systems provided in the embodiments of this application all provide corresponding operation entrances for users to choose to agree or reject the results of automated decisions; if the user chooses to reject, the expert decision-making process will be entered. The expression "automated decision" here refers to the activity of automatically analyzing and evaluating an individual's behavior habits, hobbies, or economic, health, credit status, etc. through a computer program and making a decision. The expression "expert decision" here refers to the activity of making a decision by a person who specializes in a certain field, has specialized experience, knowledge, and skills, and has reached a certain professional level.
[0032] The embodiments of this application provide a code repair method, including: scanning the code to be processed to obtain the initial repair information of the code to be processed, where the initial repair information includes the position to be repaired and the initial repair method of the code to be processed; using a repair enhancement tool to generate reference repair information for the code to be processed according to the initial repair information, where the reference repair method in the reference repair information has the same repair type as the initial repair method; updating the reference repair information according to the position to be repaired, the code variables in the initial repair method, and the repair type to obtain the target repair information; using the target repair information to repair the code to be processed.
[0033] Figure 1 An application scenario diagram of the code repair method according to the embodiments of this application is schematically shown.
[0034] As Figure 1 shown, the application scenario 100 according to this embodiment may include a first terminal device 101, a second terminal device 102, a third terminal device 103, a network 104, and a server 105. The network 104 is used to provide a medium for communication links between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. The network 104 may include various connection types, such as wired, wireless communication links, or fiber optic cables, etc.
[0035] Users can use the first terminal device 101, the second terminal device 102, and the third terminal device 103 to interact with the server 105 via the network 104 to receive or send messages, etc. Various communication client applications can be installed on the first terminal device 101, the second terminal device 102, and the third terminal device 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social platform software, etc. (for example only).
[0036] The first terminal device 101, the second terminal device 102, and the third terminal device 103 can be various electronic devices with a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop portable computers, desktop computers, and so on.
[0037] The server 105 can be a server providing various services, such as a background management server that supports the websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103 (for example only). The background management server can analyze and process data such as received user requests, and feedback the processing results (such as web pages, information, or data obtained or generated according to user requests) to the terminal device.
[0038] It should be noted that the code repair method provided by the embodiments of the present application can generally be executed by the server 105. Correspondingly, the code repair device provided by the embodiments of the present application can generally be set in the server 105. The code repair method provided by the embodiments of the present application can also be executed by a server or a server cluster different from the server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or the server 105. Correspondingly, the code repair device provided by the embodiments of the present application can also be set in a server or a server cluster different from the server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or the server 105.
[0039] It should be understood that Figure 1 the numbers of the terminal devices, the network, and the server in
[0040] Figure 2 are merely illustrative. According to the implementation requirements, there can be any number of terminal devices, networks, and servers.
[0041] As Figure 2 shown, the code repair method of this embodiment includes operations S210 to S240.
[0042] In operation S210, the code to be processed is scanned to obtain the initial repair information of the code to be processed.
[0043] According to an embodiment of the present application, the scanning method for scanning the code to be processed can be dynamic scanning and static scanning.
[0044] The scanning process generally includes two parts: compilation and scanning. The compilation part refers to the process of compiling the code to form an intermediate file. For some languages, scanning is required after compilation. After scanning is completed, various forms of scanning reports are generated. The scanning report shows the problem description of the code to be processed, the code location where the problem occurs, and the description of the repair suggestion in a fixed format.
[0045] For example, the scanning report shows the method name of the vulnerability, the location of the vulnerability, and the repair suggestion. The repair suggestion is described briefly and generally, so that developers need to rely on experience or consult relevant vulnerability content to further find a solution, resulting in low efficiency of vulnerability repair.
[0046] According to an embodiment of the present application, the initial repair information includes the location to be repaired and the initial repair method of the code to be processed.
[0047] According to an embodiment of the present application, the location to be repaired can locate the location of the vulnerability. For example, the file where the code to be processed is located, the class, method, line number where the vulnerability is located, the call relationship where the vulnerability exists, etc.
[0048] According to an embodiment of the present application, the initial repair method can be the repair suggestion shown in the scanning report.
[0049] In operation S220, a repair enhancement tool is used to generate reference repair information for the code to be processed according to the initial repair information.
[0050] According to an embodiment of the present application, the reference repair method in the reference repair information has the same repair type as the initial repair method.
[0051] According to an embodiment of the present application, the repair enhancement tool can call code repair knowledge outside the scanning report to generate a reference repair method with the same repair type as the initial repair method.
[0052] For example, the types of the reference repair method are more and the feasibility is stronger than that of the initial repair method.
[0053] For example, the vulnerability description in the reference repair information is more accurate, providing the specification for vulnerability determination, the harm caused by the vulnerability and its examples.
[0054] For example, the reference repair information can include repair examples corresponding to each reference repair method.
[0055] According to an embodiment of the present application, since there may be various types of vulnerabilities in the code to be processed, the repair type can correspond to the vulnerability type. Each repair type can have multiple repair methods.
[0056] Therefore, the reference repair information not only details the multiple repair methods for each repair type in the initial repair information, but also has more types of reference repair methods in the reference repair information, enabling developers to obtain more comprehensive repair suggestions and improving the efficiency of vulnerability repair.
[0057] In operation S230, the reference repair information is updated according to the position to be repaired, the code variables in the initial repair method, and the repair type to obtain the target repair information.
[0058] According to an embodiment of the present application, the code variables characterize the code segment and / or code parameter at the position to be repaired.
[0059] For example, according to the code segment at the position to be repaired, the code segment to be updated with the same repair type as the code segment is determined from the reference repair information; the code segment to be updated is updated to the code segment at the position to be repaired.
[0060] For example, according to the code segment at the position to be repaired, the code parameter related to the vulnerability in the code to be processed is obtained; according to the repair type and the code parameter related to the vulnerability, the code parameter to be updated is located in the reference repair information; the code parameter to be updated is updated to the code parameter of the code to be processed.
[0061] In operation S240, the code to be processed is repaired using the target repair information.
[0062] For example, it is possible to select to repair the code to be processed using the target repair method in the target repair information. The target repair method can be one that meets the preset repair conditions among multiple reference repair methods under the same repair type.
[0063] According to an embodiment of the present application, by scanning the code to be processed, the initial repair information of the code to be processed is obtained. The initial repair information includes the position to be repaired and the repair method of the code. Using a repair enhancement tool, reference repair information for the code to be processed is generated according to the initial repair information, which can enrich the description of the code repair information. The reference repair information is updated according to the position to be repaired, the code variables in the repair method, and the repair type to obtain the target repair information, so that the target repair information adapts to the repair description of the code to be processed, facilitating understanding by developers. Then, the code to be processed is repaired using the target repair information, thereby improving the repair efficiency of the code.
[0064] According to an embodiment of the present application, the repair enhancement tool includes at least one of the following: a database and a preset model, and the reference repair information includes at least one of the following: recommended repair information and example repair information.
[0065] According to an embodiment of the present application, the database includes example codes, example repair information, repair time, etc. The example code can be a vulnerability code obtained by dynamic scanning or static scanning. The example repair information can be the repair information after code scanning or the repair suggestions improved by developers in the scanning report.
[0066] According to an embodiment of the present application, the preset model can be a machine learning algorithm such as a neural network model, a large language model, a random forest model, a support vector machine, etc.
[0067] According to an embodiment of the present application, using the repair enhancement tool, the reference repair information of the code is generated according to the code to be processed and the initial repair information, including: matching the code with the example codes in the database based on the repair type to obtain the target example code corresponding to the code to be processed, and determining the example repair information of the target example code from the database, where the target example code and the code to be processed belong to the same project; or inputting the code to be processed and the initial repair information into the preset model to obtain the recommended repair information of the code to be processed, and the preset model is trained based on repair type labels, historical codes, and historical repair information.
[0068] According to an embodiment of the present application, the target example code has the same repair type as the code to be processed and belongs to the same project.
[0069] For example, the database may have a mapping relationship between example codes, example repair information, example repair time, example repair type, example repair code, example repair method, etc. The example repair information of the target example code can be determined according to the mapping relationship.
[0070] For example, both the example code and the example repair information are set with identifiers, and the identifier represents the unique encoding of the vulnerability. The example repair information of the target example code can be determined from the database according to the identifier of the target example code.
[0071] According to an embodiment of the present application, the historical codes and historical repair information can come from the repair methods of vulnerabilities on the web, the external application database or data source of the repair methods of vulnerabilities, the historically scanned vulnerability codes, and the repair information generated by scanning.
[0072] For example, the preset model can be a combined model of a neural network model and a large language model. First, use the large language model to perform semantic analysis on the initial repair information to obtain at least one repair type and the corresponding repair method for the repair type; input the code to be processed and the repair method corresponding to the repair type into the neural network model to obtain the recommended repair information for the code to be processed.
[0073] For example, the preset model can be a random forest model. The random forest model can include multiple decision trees, and each decision tree corresponds to a repair type. There are multiple leaf nodes under each decision tree, and each leaf node corresponds to a repair method. Input the code to be processed and the initial repair information into the random forest model, and multiple repair methods for the code to be processed can be accurately obtained. It is also possible to detect the missing filling repair methods in the initial repair information. The missing filling repair methods can be obtained from historical code and historical repair information. Combine the multiple repair methods generated by the random forest model with the filling repair methods to obtain the recommended repair information for the code to be processed.
[0074] According to an embodiment of the present application, by matching the code with the example code in the database based on the repair type, the target example code corresponding to the code to be processed is obtained, and the example repair information of the target example code is determined from the database. Since the target example code has the same repair type as the code to be processed and belongs to the same project, the repair accuracy of the example repair information of the target example code is improved; since the historical code and historical repair information can come from the repair methods for vulnerabilities on the web, publicly available databases or data sources for the repair methods for vulnerabilities, historically scanned vulnerable codes, and the repair information generated by the scan, the vulnerability description of the recommended repair information generated by the preset model is richer and the repair methods are more diverse.
[0075] According to an embodiment of the present application, matching the code to be processed with the example code in the database based on the repair type to obtain the target example code corresponding to the code to be processed includes: determining the type example code corresponding to the repair type from the database; and determining the type example code as the target example code when the similarity between the code to be processed and the type example code meets the preset condition.
[0076] According to an embodiment of the present application, since the database can have mapping relationships between example code, example repair information, example repair time, example repair type, example repair code, example repair method, etc. Therefore, the example code in the database can be filtered according to the repair type to obtain at least one type example code.
[0077] According to an embodiment of the present application, the formula for the similarity between the code to be processed and the type example code is as follows:
[0078] (1)
[0079] where \(e\) is the base of the natural logarithm, \(X1\) and \(X2\) respectively represent the quantization vectors of the code to be processed and the quantization vector of the type example code, \(n\) is the amount of code, and \(x\) i1 represents the code to be processed, and \(x\) i2 represents the type example code. Similar represents the similarity.
[0080] According to an embodiment of the present application, the preset condition may be less than the similarity threshold. The smaller the value of the similarity, the more similar it is, and the code is determined to be of the same project.
[0081] According to an embodiment of the present application, the reference repair information is updated according to the position to be repaired, the code variables in the initial repair method, and the repair type to obtain the target repair information, including: updating the reference repair information according to the position to be repaired and the code variables of the initial repair method to obtain the updated reference repair information; evaluating the updated reference repair information based on the repair type to obtain an evaluation result, and the target repair information includes: the evaluation result and the updated reference repair information.
[0082] Exemplarily, semantic analysis is performed on the initial repair information according to the position to be repaired and the code variables to obtain multiple keywords of the initial repair method. The multiple keywords respectively correspond to the position to be repaired and the code variables. By matching the keywords with the text in the reference repair information, the positions of the multiple keywords in the reference repair information are determined. The reference repair information is updated according to the positions of the multiple keywords in the reference repair information.
[0083] Exemplarily, the updated reference repair information can be supplemented to the initial repair information.
[0084] Exemplarily, the updated reference repair information is evaluated according to the preset repair conditions of the repair type. For example, the repair type may be code repair, and the preset repair conditions of the repair type may be removing unnecessary code functions, fixing input security, etc.
[0085] For example, the preset repair conditions can be quantified to obtain an evaluation result. If the quantization result is closer to 100%, it indicates that the repair effect of the updated reference repair information better meets the preset repair conditions.
[0086] According to an embodiment of the present application, the evaluation result characterizes the repair effect of the updated reference repair information. The repair effect may be related to the preset evaluation conditions. For example, the more sufficient the verification and the safer the input, the better the evaluation result of the updated reference repair information.
[0087] For example, by sequentially evaluating various repair methods of the updated reference repair information according to the preset repair conditions of the repair type, the evaluation results of each repair method can be obtained, which is beneficial for developers to quickly obtain high-quality repair methods.
[0088] According to an embodiment of the present application, by updating the reference repair information according to the code variables of the position to be repaired and the initial repair method, the updated reference repair information is obtained, so that the reference repair information adapts to the repair description of the code to be processed; then, based on the repair type, the updated reference repair information is evaluated to obtain an evaluation result. Therefore, developers can quickly obtain the vulnerability description of the code to be processed and better understand the reference of the target repair information.
[0089] According to an embodiment of the present application, updating the reference repair information according to the position to be repaired and the variables of the repair method to obtain the updated reference repair information includes: updating the reference position to be repaired in the reference repair information by using the position to be repaired; updating the reference repair method by using the code variables of the initial repair method to obtain the updated reference repair information.
[0090] According to an embodiment of the present application, semantic analysis is performed on the initial repair information according to the position to be repaired and the code variables to obtain the position keyword and variable keyword of the initial repair method. The position keyword and variable keyword correspond to the position to be repaired and the code variables respectively. By separately matching the position keyword and variable keyword with the text in the reference repair information, the position of the position keyword in the reference repair information and the position of the variable keyword in the reference repair information are determined.
[0091] According to the position of the position keyword in the reference repair information, update the reference position to be repaired in the reference repair method to the position to be repaired.
[0092] According to the position of the variable keyword in the reference repair information, update the reference code variable in the reference repair method to the code variable of the initial repair method.
[0093] According to an embodiment of the present application, using the position to be repaired to update the reference position to be repaired in the reference repair information makes the code position description more accurate; using the code variables of the initial repair method to update the reference repair method to obtain the updated reference repair information makes the repair method adapt to the code to be processed, enhances the readability of the target repair information, is more convenient for developers to understand, and improves the repair efficiency.
[0094] Exemplarily, there is a json (JavaScript Object Notation) injection vulnerability in the code to be processed. The repair type can be a json injection vulnerability. The following is part of the content of the code to be processed.
[0095] func (********) error {
[0096] ************************;
[0097] ************************}
[0098] body, err := json.Marshal(args)
[0099] if err != nil {
[0100] log.Errorf("marshal json args error: %v", err)
[0101] return err
[0102] }。
[0103] Replace the code variables in the reference repair information with the code variables in the initial repair information. For example, the code variable in the initial repair information can be the code with a JSON injection vulnerability: "body, err := json.Marshal(args)".
[0104] For different types of vulnerabilities, the code snippets are usually different. Replace the vulnerability description and repair information in the reference repair information with those of the code to be processed, which is convenient for developers to understand.
[0105] For example, the location to be repaired can include the file name, class name, and the line number of the vulnerable code.
[0106] According to an embodiment of the present application, the above method further includes: in response to a change operation on the target repair information, generating custom repair information according to the change operation; storing the custom repair information in a database as example repair information.
[0107] For example, for the custom repair information provided by a developer, supplement the custom repair information to each example repair information of the same type. It should be noted that only the code determined to be in the same project will have the updated reference repair information supplemented to the initial repair information.
[0108] According to an embodiment of the present application, adding a change operation for the target repair information facilitates developers to customize repair suggestions, and can add actual effective repair suggestions to the database, which can generate effective repair information in subsequent code security scans.
[0109] When a preset model interface is configured for code security scanning, the code to be processed and the initial repair information are input into the preset model to obtain the recommended repair information for the code. The reference to-be-repaired location in the reference repair information is updated using the to-be-repaired location; the reference repair method is updated using the code variables of the initial repair method to obtain the updated reference repair information.
[0110] For example, based on the repair type, the code to be processed is matched with the sample code in the database, and no type sample code belonging to the same project as the code to be processed is obtained. The preset model can be called to process the code to be processed and the initial repair information input into the preset model to obtain the recommended repair information for the code. The to-be-repaired location and the code variables of the initial repair method are updated to the preset repair information template, thereby obtaining the target repair information for the code to be processed.
[0111] By generating the target repair information for the code to be processed, the readability of the code security scanning vulnerabilities is increased, and the database is updated. In subsequent scanning processes, the sample repair information or the preset model in the database is used to reduce the repair difficulty of the vulnerabilities, thereby improving the accuracy of the scanning results and reducing the code security scanning repair workload.
[0112] According to an embodiment of the present application, the updated reference repair information is evaluated based on the repair type to obtain an evaluation result, including: determining a target repair method from the database according to the repair type; obtaining a code repair result corresponding to the target repair method; evaluating the updated reference repair information according to the code repair result to obtain an evaluation result.
[0113] Determine the target repair method of the same repair type from the database. Obtain the code repair result of the target repair method. The repair method of the updated reference repair information is included in the target repair method. Refer to using the code repair result of the target repair method to evaluate the updated reference repair information, which is more referenceable.
[0114] According to an embodiment of the present application, by synthesizing the code repair results of multiple target repair methods of the same repair type in the database, the evaluation result of the updated reference repair information is made more accurate.
[0115] Figure 3 Schematically shows a flowchart of updating the reference repair information according to the to-be-repaired location, the code variables in the initial repair method, and the repair type according to an embodiment of the present application to obtain the target repair information.
[0116] As Figure 3 shown, updating the reference repair information according to the to-be-repaired location, the code variables in the initial repair method, and the repair type in this embodiment to obtain the target repair information includes operations S310 to S370.
[0117] In operation S310, the code to be processed is scanned to obtain the initial repair information of the code to be processed, and the initial repair information includes the position to be repaired and the initial repair method of the code to be processed.
[0118] In operation S320, a repair enhancement tool is used to generate reference repair information for the code to be processed according to the initial repair information.
[0119] In operation S330, the reference position to be repaired in the reference repair information is updated using the position to be repaired.
[0120] In operation S340, the reference repair method is updated using the code variables of the initial repair method to obtain the updated reference repair information.
[0121] In operation S350, the target repair method is determined from the database according to the repair type.
[0122] In operation S360, the code repair result corresponding to the target repair method is obtained.
[0123] In operation S370, the updated reference repair information is evaluated according to the code repair result to obtain an evaluation result, and the target repair information includes the evaluation result and the updated reference repair information.
[0124] According to an embodiment of the present application, repairing the code to be processed using the target repair information includes: when the evaluation result meets the preset evaluation conditions and the reference repair information is the example repair information, determining the example repair code of the target example code from the database; using the example repair code to repair the code to be processed.
[0125] According to an embodiment of the present application, the evaluation result has a quantitative value, and the preset evaluation condition may be greater than the evaluation threshold. For example, the evaluation threshold may be 80%.
[0126] For example, the target repair information includes the target repair method. When the evaluation result meets the preset evaluation conditions and the reference repair information is the example repair information, the target example code for the target repair method is determined from the database. According to the mapping relationship, the example repair code of the target example code is determined. The code to be processed is repaired using the example repair code.
[0127] According to an embodiment of the present application, the target example code and the code to be processed belong to the same project and have the same repair type. Therefore, using the example repair code in the database is more accurate and more conducive to the project progress.
[0128] Figure 4 Schematically shows a flowchart of a code repair method according to another embodiment of the present application.
[0129] As shown Figure 4 in the figure, the code repair method of this embodiment includes operations S410 to S470.
[0130] In operation S410, the code to be processed is scanned to obtain the initial repair information of the code to be processed. The initial repair information includes the position to be repaired and the initial repair method of the code to be processed.
[0131] In operation S420, the type example code corresponding to the repair type is determined from the database.
[0132] In operation S430, when the similarity between the code to be processed and the type example code meets the preset conditions, the type example code is determined as the target example code.
[0133] In operation S440, the example repair information of the target example code is determined from the database.
[0134] In operation S450, the example repair information is updated according to the code variables and repair type in the position to be repaired and the initial repair method to obtain the target repair information. The target repair information includes the evaluation result.
[0135] In operation S460, when the evaluation result meets the preset evaluation conditions, the example repair code of the target example code is determined from the database.
[0136] In operation S470, the code to be processed is repaired using the example repair code.
[0137] According to the embodiment of the present application, the above method further includes: when the evaluation result meets the preset evaluation conditions and the reference repair information is the recommended repair information, the code is repaired using the recommended repair code in the target repair information.
[0138] According to the embodiment of the present application, the preset model can be trained based on the repair type label, the historical repair information with repair codes, and the historical codes. Therefore, using the recommended repair information generated by the preset model, the recommended repair code can be automatically generated.
[0139] For example, when the repair enhancement tool includes a database and a preset model, the evaluation results corresponding to the example repair information and the evaluation results corresponding to the recommended repair information can be compared, and the repair code with a good evaluation result is used to repair the code to be processed to achieve accurate code repair.
[0140] Figure 5 Schematically shows the structural block diagram of the code repair device according to the embodiment of the present application.
[0141] As Figure 5As shown, the code repair device 500 of this embodiment includes a scanning module 510, a first generation module 520, an update module 530, and a first repair module 540.
[0142] The scanning module 510 is configured to scan the code to be processed to obtain initial repair information of the code to be processed. The initial repair information includes the position to be repaired and the initial repair method of the code to be processed. In one embodiment, the scanning module 510 can be used to perform the operation S210 described above, which will not be elaborated here.
[0143] The first generation module 520 is configured to use a repair enhancement tool to generate reference repair information for the code to be processed according to the initial repair information. The reference repair method in the reference repair information has the same repair type as the initial repair method. In one embodiment, the generation module 520 can be used to perform the operation S220 described above, which will not be elaborated here.
[0144] The update module 530 is configured to update the reference repair information according to the position to be repaired, the code variables in the initial repair method, and the repair type to obtain target repair information. In one embodiment, the update module 530 can be used to perform the operation S230 described above, which will not be elaborated here.
[0145] The first repair module 540 is configured to repair the code to be processed by using the target repair information. In one embodiment, the repair module 540 can be used to perform the operation S240 described above, which will not be elaborated here.
[0146] According to the embodiment of the present application, the repair enhancement tool includes at least one of the following: a database and a preset model. The reference repair information includes at least one of the following: recommended repair information and example repair information. The generation module 520 includes a matching sub-module or an input sub-module. The matching sub-module is configured to match the code with the example code in the database based on the repair type to obtain a target example code corresponding to the code to be processed, and determine the example repair information of the target example code from the database. The target example code and the code to be processed belong to the same project; or the input sub-module is configured to input the code to be processed and the initial repair information into the preset model to obtain the recommended repair information of the code. The preset model is trained based on repair type labels, historical code, and historical repair information.
[0147] According to the embodiment of the present application, the matching sub-module includes a first determination unit and a second determination unit. The first determination unit is configured to determine the type example code corresponding to the repair type from the database; the second determination unit is configured to determine the type example code as the target example code when the similarity between the code to be processed and the type example code meets a preset condition.
[0148] According to an embodiment of the present application, the update module 530 includes an update sub-module and an evaluation sub-module. The update sub-module is used to update the reference repair information according to the code variables of the position to be repaired and the initial repair method, so as to obtain the updated reference repair information; the evaluation sub-module is used to evaluate the updated reference repair information based on the repair type to obtain an evaluation result, and the target repair information includes: the evaluation result and the updated reference repair information.
[0149] According to an embodiment of the present application, the update sub-module includes a first update unit and a second update unit. The first update unit is used to update the reference position to be repaired in the reference repair information by using the position to be repaired; the second update unit is used to update the reference repair method by using the code variables of the repair method to obtain the updated reference repair information.
[0150] According to an embodiment of the present application, the evaluation sub-module includes a third determination unit, an acquisition unit, and an evaluation unit. The third determination unit is used to determine the target repair method from the database according to the repair type; the acquisition unit is used to acquire the code repair result corresponding to the target repair method; the evaluation unit is used to evaluate the updated reference repair information according to the code repair result to obtain an evaluation result.
[0151] According to an embodiment of the present application, the first repair module 540 includes a determination sub-module and a repair sub-module. The determination sub-module is used to determine the example repair code of the target example code from the database when the evaluation result meets the preset evaluation conditions and the reference repair information is the example repair information; the repair sub-module is used to repair the code to be processed by using the example repair code.
[0152] According to an embodiment of the present application, the above device further includes a second repair module. The second repair module is used to repair the code to be processed by using the recommended repair code in the target repair information when the evaluation result meets the preset evaluation conditions and the reference repair information is the recommended repair information.
[0153] According to an embodiment of the present application, the above device further includes a second generation module and a storage module. The second generation module is used to generate custom repair information according to the change operation in response to the change operation on the target repair information; the storage module is used to store the custom repair information in the database as the example repair information.
[0154] According to an embodiment of the present application, any multiple of the scanning module 510, the first generation module 520, the updating module 530, and the first repair module 540 may be combined and implemented in one module, or any one of them may be split into multiple modules. Alternatively, at least part of the functions of one or more of these modules may be combined with at least part of the functions of other modules and implemented in one module. According to an embodiment of the present application, at least one of the scanning module 510, the first generation module 520, the updating module 530, and the first repair module 540 may be at least partially implemented as a hardware circuit, such as a field programmable gate array (FPGA), a programmable logic array (PLA), a system on chip, a system on substrate, a system on package, an application specific integrated circuit (ASIC), or any other reasonable manner of integrating or packaging circuits, etc., implemented by hardware or firmware, or implemented in any one of the three implementation manners of software, hardware, and firmware, or in any suitable combination of several of them. Alternatively, at least one of the scanning module 510, the first generation module 520, the updating module 530, and the first repair module 540 may be at least partially implemented as a computer program module, which can perform corresponding functions when the computer program module is run.
[0155] Figure 6 A block diagram of an electronic device suitable for implementing the code repair method according to an embodiment of the present application is schematically shown.
[0156] As Figure 6 shown, the electronic device 600 according to an embodiment of the present application includes a processor 601, which can perform various appropriate actions and processes according to a program stored in a read only memory (ROM) 602 or a program loaded from a storage section 608 into a random access memory (RAM) 603. The processor 601 may include, for example, a general microprocessor (such as a CPU), an instruction set processor, and / or a related chipset, and / or a dedicated microprocessor (such as an application specific integrated circuit (ASIC)), etc. The processor 601 may also include on-board memory for caching purposes. The processor 601 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of the present application.
[0157] In the RAM 603, various programs and data required for the operation of the electronic device 600 are stored. The processor 601, the ROM 602, and the RAM 603 are connected to each other via the bus 604. The processor 601 performs various operations of the method flow according to the embodiments of the present application by executing the programs in the ROM 602 and / or the RAM 603. It should be noted that the programs may also be stored in one or more memories other than the ROM 602 and the RAM 603. The processor 601 may also perform various operations of the method flow according to the embodiments of the present application by executing the programs stored in the one or more memories.
[0158] According to an embodiment of the present application, the electronic device 600 may further include an input / output (I / O) interface 605, and the input / output (I / O) interface 605 is also connected to the bus 604. The electronic device 600 may further include one or more of the following components connected to the input / output (I / O) interface 605: an input part 606 including a keyboard, a mouse, etc.; an output part 607 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc. and a speaker, etc.; a storage part 608 including a hard disk, etc.; and a communication part 609 including a network interface card such as a LAN card, a modem, etc. The communication part 609 performs communication processing via a network such as the Internet. The drive 610 is also connected to the input / output (I / O) interface 605 as needed. A removable medium 611, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 610 as needed so that a computer program read from it can be installed into the storage part 608 as needed.
[0159] The present application also provides a computer-readable storage medium, which may be included in the device / device / system described in the above embodiments; or may exist separately without being assembled into the device / device / system. The above computer-readable storage medium carries one or more programs, and when the one or more programs are executed, the method according to the embodiments of the present application is implemented.
[0160] According to an embodiment of the present application, the computer-readable storage medium may be a non-volatile computer-readable storage medium, for example, it may include but is not limited to: portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the above. In the present application, the computer-readable storage medium may be any tangible medium that contains or stores a program, and this program can be used by or in combination with an instruction execution system, device, or apparatus. For example, according to an embodiment of the present application, the computer-readable storage medium may include the above-described ROM 602 and / or RAM 603 and / or one or more memories other than ROM 602 and RAM 603.
[0161] An embodiment of the present application also includes a computer program product, which includes a computer program that contains program code for executing the method shown in the flowchart. When the computer program product runs in a computer system, the program code is used to enable the computer system to implement the code repair method provided by the embodiment of the present application.
[0162] When the computer program is executed by the processor 601, it executes the above functions defined in the system / apparatus of the embodiment of the present application. According to an embodiment of the present application, the above-described systems, apparatuses, modules, units, etc. can be implemented by computer program modules.
[0163] In one embodiment, the computer program can rely on tangible storage media such as optical storage devices and magnetic storage devices. In another embodiment, the computer program can also be transmitted and distributed in the form of a signal on a network medium, and is downloaded and installed through the communication part 609, and / or installed from the removable medium 611. The program code contained in the computer program can be transmitted by any suitable network medium, including but not limited to: wireless, wired, etc., or any suitable combination of the above.
[0164] In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 609, and / or installed from the removable medium 611. When the computer program is executed by the processor 601, it executes the above functions defined in the system of the embodiment of the present application. According to an embodiment of the present application, the above-described systems, devices, apparatuses, modules, units, etc. can be implemented by computer program modules.
[0165] In accordance with embodiments of the present application, program code for executing the computer programs provided by the embodiments of the present application can be written in any combination of one or more programming languages. Specifically, these computing programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. The programming languages include, but are not limited to, programming languages such as Java, C++, Python, the "C" language, or similar programming languages. The program code can be executed entirely on the user's computing device, partially on the user's device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving a remote computing device, the remote computing device can be connected to the user's computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., by connecting through the Internet using an Internet service provider).
[0166] The flowcharts and block diagrams in the accompanying drawings illustrate the possible architectures, functions, and operations of systems, methods, and computer program products according to various embodiments of the present application. In this regard, each block in the flowchart or block diagram can represent a module, a program segment, or a part of code that contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the blocks can occur in a different order than that marked in the accompanying drawings. For example, two consecutive blocks shown can actually be executed substantially in parallel, and they can sometimes be executed in the reverse order, depending on the functions involved. It should also be noted that each block in the block diagram or flowchart, and combinations of blocks in the block diagram or flowchart, can be implemented by a dedicated hardware-based system for performing the specified functions or operations, or can be implemented by a combination of dedicated hardware and computer instructions.
[0167] Those skilled in the art can understand that the features described in the various embodiments of the present application can be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in the present application. In particular, without departing from the spirit and teachings of the present application, the features described in the various embodiments of the present application can be combined and / or combined in various ways. All such combinations and / or combinations fall within the scope of the present application.
[0168] The above describes the embodiments of the present application. However, these embodiments are for illustrative purposes only and are not intended to limit the scope of the present application. Although the embodiments are described separately above, this does not mean that the measures in each embodiment cannot be used advantageously in combination. Without departing from the scope of the present application, those skilled in the art can make various substitutions and modifications, and all such substitutions and modifications should fall within the scope of the present application.
Claims
1. A code repair method, characterized in that: The method comprises: Scan the code to be processed to obtain initial repair information of the code to be processed, wherein the initial repair information includes a position to be repaired and an initial repair method of the code to be processed; Using a repair enhancement tool, generating reference repair information for the code to be processed according to the initial repair information, wherein a reference repair method in the reference repair information has the same repair type as the initial repair method; The reference repair information is updated according to the position to be repaired, the code variable in the initial repair mode, and the repair type to obtain target repair information; The target repair information is used to repair the code to be processed.
2. The method according to claim 1, characterized in that The repair enhancement tool includes at least one of the following: a database and a preset model, and the reference repair information includes at least one of the following: recommended repair information and example repair information; The using the repair enhancement tool to generate reference repair information of the code according to the code to be processed and the initial repair information includes: Matching the code with sample codes in a database based on the repair type to obtain a target sample code corresponding to the code to be processed, and determining sample repair information of the target sample code from the database, wherein the target sample code and the code to be processed belong to the same project; or The code to be processed and the initial repair information are input into a preset model to obtain recommended repair information for the code, wherein the preset model is trained based on a repair type label, historical codes, and historical repair information.
3. The method according to claim 2, characterized in that The step of matching the code to be processed with a sample code in a database based on the repair type to obtain a target sample code corresponding to the code to be processed includes: determining from the database a type example code corresponding to the repair type; When the similarity between the code to be processed and the type example code meets a preset condition, the type example code is determined to be the target example code.
4. The method according to claim 2, characterized in that: The updating of the reference repair information according to the position to be repaired, the code variable in the initial repair mode and the repair type to obtain the target repair information includes: The reference repair information is updated according to the code variables of the position to be repaired and the initial repair method to obtain updated reference repair information; The updated reference repair information is evaluated based on the repair type to obtain an evaluation result, and the target repair information includes: the evaluation result and the updated reference repair information.
5. The method according to claim 4, characterized in that The updating of the reference repair information according to the code variable of the position to be repaired and the initial repair method to obtain updated reference repair information includes: Using the position to be repaired, the reference position to be repaired in the reference repair information is updated; The reference repair method is updated using the code variable of the initial repair method to obtain the updated reference repair information.
6. The method according to claim 4, characterized in that The step of evaluating the updated reference repair information based on the repair type to obtain an evaluation result includes: According to the repair type, determining a target repair method from a database; Obtaining a code repair result corresponding to the target repair method; The updated reference repair information is evaluated according to the code repair result to obtain the evaluation result.
7. The method according to claim 4, characterized in that The repairing the code to be processed by using the target repair information includes: In a case where the evaluation result satisfies a preset evaluation condition and the reference repair information is sample repair information, determining a sample repair code for the target sample code from a database; The code to be processed is repaired using the example repair code.
8. The method according to claim 7, characterized in that The method further comprises: In a case where the evaluation result meets a preset evaluation condition and the reference repair information is recommended repair information, the code to be processed is repaired using the recommended repair code in the target repair information.
9. The method according to claim 2, characterized in that: The method further comprises: In response to a modification operation on the target repair information, generating customized repair information according to the modification operation; The customized repair information is stored in a database as the example repair information.
10. A code repair device, characterized in that: The device comprises: A scanning module, used for scanning the code to be processed to obtain initial repair information of the code to be processed, wherein the initial repair information includes a position to be repaired and an initial repair method of the code to be processed; A generation module, configured to generate reference repair information for the code to be processed according to the initial repair information by using a repair enhancement tool, wherein a reference repair method in the reference repair information has the same repair type as the initial repair method; An updating module, configured to update the reference repair information according to the position to be repaired, the code variable in the initial repair mode and the repair type, to obtain target repair information; A repair module is used to repair the code to be processed by using the target repair information.
Citation Information
Cited By
Code error detection method and device and storage medium
CN120353713A