Code repair method, device, equipment and medium
Through automated code repair methods, the analysis and repair model of source code and description files is used to solve the problem of low efficiency in traditional code repair, and fast and accurate code repair is achieved, improving repair efficiency and accuracy.
Patent Information
- Application Number
- CN202411328832.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-24
- Publication Date
- 2024-12-27
- Estimated Expiration
- 2044-09-24
AI Technical Summary
In the process of software system development, traditional code repair methods require a lot of manpower, resulting in low code repair efficiency, especially when the source code is large, it is difficult to quickly and accurately determine the target code repair results.
By obtaining source code files and code description files, vulnerability analysis and information extraction, building initial prompt word information, and inputting it into a pre-built code repair model, similarity analysis is performed to determine the target code repair results, and reducing manual intervention. Improve repair efficiency and accuracy.
It realizes automation and high efficiency of code repair, reduces labor costs, and can quickly and accurately obtain target code repair results, improving the accuracy and efficiency of code repair.
Smart Images

Figure CN118839350B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer software technology, and in particular to a code repair method, device, equipment and medium. Background Art
[0002] With the continuous development of information technology, computer software, as an important carrier for the advancement of various fields of society, has been increasingly applied to different fields of social life, such as transportation, communication, finance, etc. In the process of software system development, dynamic or static detection of the code can be used to discover potential problems in the code. In the process of increasing the scale and complexity of software systems, in order to ensure the reliability and security of software systems, the research on how to repair the source code is particularly important.
[0003] At present, traditional code repair usually requires developers to determine source code repair suggestions based on manual experience in order to repair code defects. However, when the amount of source code is large, adopting this solution requires a lot of manpower, resulting in low code repair efficiency. Summary of the invention
[0004] The purpose of this application is to provide a code repair method, device, equipment and medium, which can quickly and accurately determine the target code repair result and improve the code repair efficiency.
[0005] To achieve the above objectives, this application provides the following solutions:
[0006] In a first aspect, the present application provides a code repair method, comprising:
[0007] Get the source code file and the corresponding code description file;
[0008] Performing vulnerability analysis on the source code file to obtain code defect results of the source code file, and extracting information from the code description file to obtain summary information of the source code file;
[0009] Constructing initial prompt word information according to the summary information and the code defect result;
[0010] Inputting the initial prompt word information into a plurality of pre-built code repair models to obtain a code repair result corresponding to each code repair model; the code repair model is trained based on historical code defect results and corresponding annotated repair results;
[0011] A similarity analysis is performed on each of the code repair results to determine a target code repair result, so as to perform code repair using the code repair result.
[0012] Optionally, constructing initial prompt word information according to the summary information and the code defect result specifically includes:
[0013] Obtaining context information of the code defect result in the source code file;
[0014] Initial prompt word information is constructed based on the code defect result, the context information and the summary information; the context information, the summary information and the code defect result are marked with priority tags; the priority tags are used to characterize the degree of influence of different information on the code repair result, the priority of the code defect result is greater than the priority of the context information, and the priority of the context information is greater than the priority of the summary information.
[0015] Optionally, the initial prompt word information is input into a plurality of pre-built code repair models to obtain a code repair result corresponding to each code repair model, including:
[0016] For each code repair model, the initial prompt word information is input into the code repair model to obtain a feedback value; the feedback value is used to indicate whether the initial prompt word information exceeds the parameter input window of the code repair model;
[0017] If the feedback value indicates that the initial prompt word information exceeds the parameter input window of the code repair model, the initial prompt word information is adjusted based on the priority label to obtain target prompt word information; the target prompt word information does not exceed the parameter input window of the code repair model;
[0018] The target prompt word information is input into the code repair model to obtain a code repair result.
[0019] Optionally, based on the priority tag, the initial prompt word information is adjusted to obtain target prompt word information, including:
[0020] Based on the priority tag, the summary information in the initial prompt word information is deleted to obtain the first prompt word information, and the first prompt word information is used as the target prompt word information; or
[0021] Based on the priority label, the summary information in the prompt word information is deleted to obtain the first prompt word information; when the first prompt word information is represented by the output result of the code repair model that the first prompt word information exceeds the parameter input window of the code repair model, the context information in the first prompt word information is deleted to obtain the second prompt word information; and the second prompt word information is used as the target prompt word information.
[0022] Optionally, after obtaining the feedback value, the method further includes:
[0023] If the feedback value indicates that the initial prompt word information does not exceed the parameter input window of the code repair model, obtaining the output result of the code repair model;
[0024] The output result is used as the code repair result corresponding to the code repair model.
[0025] Optionally, performing similarity analysis on each of the code repair results to determine a target code repair result includes:
[0026] Performing grammatical analysis on each of the code repair results to obtain grammatical elements corresponding to each code repair result;
[0027] According to the grammatical elements, construct a similarity matrix corresponding to each of the code repair results; the similarity matrix includes a plurality of similarity element values, and the similarity element values are used to represent the similarity between every two code repair results;
[0028] The similarity matrix is analyzed to determine the target code repair result.
[0029] Optionally, analyzing the similarity matrix to determine the target code repair result specifically includes:
[0030] Selecting the maximum similarity element value from the similarity matrix as the target similarity element value;
[0031] The two code repair results corresponding to the target similar element values are used as the code repair results to be selected;
[0032] Calculating respectively the average similarity between each of the candidate code repair results and the remaining code repair results; the remaining code repair results are the code repair results other than the candidate code repair result among all the code repair results;
[0033] The candidate code repair result corresponding to the largest average similarity value is used as the target code repair result.
[0034] In a second aspect, the present application provides a code repair device, comprising:
[0035] An acquisition module is used to acquire source code files and corresponding code description files;
[0036] A processing module, configured to perform vulnerability analysis on the source code file to obtain code defect results of the source code file, and to extract information from the code description file to obtain summary information of the source code file;
[0037] A prompt word construction module, used to construct initial prompt word information according to the summary information and the code defect result;
[0038] A result determination module is used to input the initial prompt word information into a plurality of pre-built code repair models to obtain a code repair result corresponding to each code repair model; the code repair model is trained based on historical code defect results and corresponding annotated repair results;
[0039] The repair module is used to perform similarity analysis on each of the code repair results to determine a target code repair result so as to perform code repair based on the code repair result.
[0040] In a third aspect, the present application provides a computer device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the code repair method described in any one of the above.
[0041] In a fourth aspect, the present application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the code repair method described in any one of the above.
[0042] According to the specific embodiments provided in this application, this application discloses the following technical effects:
[0043] The present application provides a code repair method, device, equipment and medium, which obtains a source code file and a corresponding code description file, performs vulnerability analysis on the source code file, obtains the code defect result of the source code file, extracts information from the code description file, obtains summary information of the source code file, and then constructs initial prompt word information according to the summary information and the code defect result, inputs the initial prompt word information into multiple pre-built code repair models, obtains the code repair result corresponding to each code repair model, and performs similarity analysis on each code repair result to determine the target code repair result, so as to perform code repair through the code repair result. Compared with the prior art, the technical solution of the present application does not need to consume a lot of manpower, and automatically extracts information from the code description file to obtain summary information while obtaining the code defect result of the source code, which can provide necessary information and auxiliary information for subsequent code repair in a more comprehensive manner, and comprehensively considers the summary information and the code defect result, so as to accurately construct the initial prompt word information, input the initial prompt word information into multiple preset code repair models, obtain multiple code repair results, improve the accuracy of code repair, and then by performing similarity analysis on each code repair result, the target code repair result can be obtained quickly and accurately, further improving the efficiency of code repair. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0045] Figure 1 This is an application environment diagram of a code repair method in an embodiment of the present application;
[0046] Figure 2 A flowchart of a code repair method provided in one embodiment of the present application;
[0047] Figure 3 A schematic diagram of the structure of a code repair method provided in one embodiment of the present application;
[0048] Figure 4 A flowchart of a method for inputting initial prompt word information into a code repair model to obtain a code repair result provided by an embodiment of the present application;
[0049] Figure 5 A flowchart of a method for determining a target code repair result provided in one embodiment of the present application;
[0050] Figure 6 A schematic diagram of functional modules of a code repair device provided in one embodiment of the present application;
[0051] Figure 7 A schematic diagram of the structure of a computer device provided in one embodiment of the present application. DETAILED DESCRIPTION
[0052] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0053] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, the present application is further described in detail below with reference to the accompanying drawings and specific implementation methods.
[0054] The code repair method provided in the embodiment of the present application can be applied to Figure 1In the application environment shown. Among them, the terminal 102 communicates with the server 104 through the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be set up separately, integrated on the server 104, or placed on the cloud or other servers. The terminal 102 can send the source code file and the code description file to the server 104. After receiving the source code file and the code description file, the server 104 obtains the code defect result and summary information, and constructs the initial prompt word information, inputs the initial word prompt information into the code repair model, obtains multiple code repair results, performs similarity analysis on each code repair result, and obtains the target code repair result. The server 104 can feedback the obtained code repair result to the terminal 102. In addition, in some embodiments, the code repair method can also be implemented by the server 104 or the terminal 102 alone, such as the terminal 102 can directly perform code repair processing on the source code file and the code description file, or the server 104 can obtain the source code file and the code description file from the data storage system, and perform code repair on the source code file and the code description file. The above code repair model may be pre-stored in the server 104 or the terminal 102 .
[0055] The terminal 102 may be, but is not limited to, various desktop computers, laptop computers, smart phones, tablet computers, IoT devices, and portable wearable devices. The IoT devices may be smart speakers, smart TVs, smart air conditioners, smart vehicle-mounted devices, etc. The portable wearable devices may be smart watches, smart bracelets, head-mounted devices, etc. The server 104 may be implemented as an independent server or a server cluster consisting of multiple servers, or may be a cloud server.
[0056] In an exemplary embodiment, Figure 2 As shown, a code repair method is provided, which is executed by a computer device, and can be executed by a computer device such as a terminal or a server alone, or by a terminal and a server together. In the embodiment of the present application, the method is applied to Figure 1 The server 104 in the example is used as an example to illustrate, including the following steps 201 to 205. Among them:
[0057] Step 201, obtaining a source code file and a corresponding code description file.
[0058] It should be noted that the above-mentioned source code file refers to the code file that needs to be analyzed for vulnerabilities. The source code file can be the initial code file written by the developer, which may include code defects. The code description file refers to the configuration file of the project to which the source code belongs. The configuration file may include the number of lines of code and the readme of the project. The readme of the project is mainly to reflect the overall overview or project description of the project, which may include but is not limited to: project introduction, demonstration address, technology selection, construction method, domain model, test strategy, technical architecture, deployment topology, UML diagram, external dependencies, operating environment, coding practice, FAQ, etc.
[0059] Among them, the project introduction can be a brief description of the project's business, business scenarios, purposes, etc.; technology selection includes the project's technology stack, including language, framework, middleware, other information, etc.; construction method can refer to the tools, commands, other content, etc. required in the development process; domain models can include e-commerce systems, including Order, Goods / Products, etc.; testing strategies can include test types, such as automated testing; technical architecture can include technical architecture diagrams, topology diagrams, etc.; deployment topology can include unfamiliar topology diagrams; external dependencies can refer to external third-party components that the project depends on when it is running, such as: the membership authentication system that the order system depends on, etc.; the operating environment refers to the environment configuration information, which can include access to various environments, database connection methods, environment configuration and construction, etc.; coding practices can include exception handling rules, comment formats, etc., and FAQs can include scenario problems and solutions encountered during development or environment construction.
[0060] Optionally, the above-mentioned source code files and corresponding code description files can be imported from an external device, or the developer can obtain the source code and code description files written in real time according to actual needs, or they can be obtained from a database or blockchain. This application does not impose any restrictions on the method of obtaining the source code files and code description files.
[0061] Step 202, performing vulnerability analysis on the source code file to obtain code defect results of the source code file, and extracting information from the code description file to obtain summary information of the source code file.
[0062] Specifically, after obtaining the source code file, the source code file can be statically detected or dynamically detected to obtain the code defect results of the source code file. When the source code file is statically detected, the source code file can be scanned by a static code analysis tool to obtain the code defect results of the source code file. The code defect results are used to characterize the defects or problems existing in the source code file, which may include the defect type, defect location and corresponding defect description information. Optionally, the defect type may be, for example, a security vulnerability, memory leak, null pointer reference, uninitialized variable, array out of bounds, buffer overflow, code unreachable, etc.
[0063] It is understood that static code analysis is an automated code analysis method that detects potential programming errors, security vulnerabilities, and other problems by checking the syntax, structure, and data flow of the code. It does not require running the program, but performs analysis at compile time or code editing, so it is also called static analysis. The above-mentioned static code analysis tool may include Klocwork, for example.
[0064] As an implementable method, in the process of performing vulnerability analysis on source code files to obtain code defect results for source code files, the source code files can be firstly subjected to lexical, grammatical and semantic analysis to obtain a data flow graph, and the data flow graph can be input into a preset detection model for defect detection to obtain code defect results for the source code files.
[0065] It is understandable that the above data flow graph may include a value-flow graph (VFG) and a control-flow graph (CFG). A value-flow graph refers to a directed value-flow graph from variable definition to variable use, where the value refers to the value of the variable (pointer). A control-flow graph is a graph that represents the execution flow of a program within a method. The nodes in the control-flow graph are statements (instructions), and the edges represent the execution flow. For example, if the next statement after statement A is executed is B, there should be a directed edge from A to B in the CFG.
[0066] The above-mentioned preset detection model may include multiple detectors, which may be represented by a pre-built detection program. Each detector is used to detect at least one defect type corresponding to the source code file, and the defect type detection can be performed by running the corresponding detection program. The detection model may include, for example, three detectors, namely a first detector, a second detector, and a third detector. The first detector is used to detect whether an array cross-bounds occurs in the source code file, the second detector is used to detect whether a buffer overflow occurs in the source code file, and the third detector is used to detect whether a code is unreachable in the source code file.
[0067] As another feasible method, information is extracted from the code description file through a static code analysis tool to obtain summary information of the source code file. The summary information includes, for example, the technology stack used in the source code and the project application type. The technology stack may include qt, javafx, spring, and the project application type may include web / client.
[0068] In this embodiment, information is extracted from the code description file to obtain summary information of the source code file, which can provide auxiliary information for the subsequent code repair model to predict code repair. For example, when the technology stack in the summary information is javafx, the corresponding code repair result can be output for the javafx technology stack based on the code defect result, so that the accuracy of the code repair result is higher.
[0069] Step 203: construct initial prompt word information according to the summary information and the code defect result.
[0070] The above-mentioned initial prompt word information refers to the parameters that need to be input into the code repair model, which can be expressed in json format. The initial prompt word information can include necessary information and auxiliary information of the source code file, wherein the necessary information can include the defect results of the source code file, and the auxiliary information can include summary information of the source code file.
[0071] As an implementable method, in the process of constructing the initial prompt word information, the code defect results can be normalized, and the code defect results and summary information can be organized into a json string, and the code defect results can be normalized to obtain the initial prompt word information. Among them, according to the degree of influence of different information on the code repair results, corresponding priority tags can be added to the code defect results and summary information. The priority tags are used to characterize the degree of influence of different information on the code repair results. Since the code defect results have a greater influence on the code repair results, the priority corresponding to the code defect results is greater than the priority corresponding to the summary information.
[0072] As another possible implementation, in the process of constructing the initial prompt word information according to the summary information and the code defect result, the context information of the code defect result in the source code file can be first obtained, and the initial prompt word information can be constructed based on the code defect result, the context information and the summary information. Among them, the context information, the summary information and the code defect result are marked with priority tags, the priority of the code defect result is greater than the priority of the context information, and the priority of the context information is greater than the priority of the summary information.
[0073] It is understandable that the context information of the code defect result in the source code file may include the connection between the code defect result and the context and the code content of the context. Among them, the code defect result, context information and summary information can be organized into the form of a json string, and the code defect result is normalized to obtain the initial prompt word information. Since the code defect result is normalized, the size of the code defect result can be compressed, so that the parameters of the input code repair model are more consistent with its input window size. The above initial prompt word information can be expressed in the following form:
[0074] {
[0075] "request": {
[0076] "essential": {
[0077] "error_type": "double free",
[0078] "error_location": " / path / to / file:123"
[0079] },
[0080] "contextual": {
[0081] "code_snippet": "malloc(...); free(...); free(...);"
[0082] },
[0083] "additional": {
[0084] "project_summary": {
[0085] "project_name": "demo",
[0086] "description": "Kernel part of the project",
[0087] }
[0088] }
[0089] },
[0090] "demand": "Provide a fix for the double free issue in less than 150characters."
[0091] }
[0092] It should be noted that in the structure of the above initial prompt word information, the essential part contains the most critical information for reference when determining the code repair results. This information is necessary for understanding the code defect information and generating code repair results. It may include the code defect type and the location of the defect, and its location may correspond to the corresponding line in the file; the contextual part contains the contextual information of the code defect result in the source file. This information can be used as auxiliary information to help provide more accurate repair suggestions for determining the code repair results, but it can be omitted in an emergency. It may include the context content and location corresponding to the defect result. The additional part contains non-essential information, including summary information, which may include project summary, project name, project description, etc.
[0093] In this embodiment, the initial prompt word information constructed based on the summary information and the code defect result can be made more comprehensive, which facilitates comprehensive consideration of the summary information when determining the code repair result, thereby improving the accuracy of the code repair result.
[0094] Step 204, input the initial prompt word information into a plurality of pre-built code repair models to obtain a code repair result corresponding to each code repair model; the code repair model is trained based on historical code defect results and corresponding annotated repair results.
[0095] It should be noted that the above-mentioned code repair model is a network structure model that is trained on historical code defect results and has the ability to determine repair results. The code repair model is a neural network model that takes initial prompt word information as input and outputs code repair results and has the ability to predict code repair results. The code repair model may include a multi-layer network structure, and different layers of network structures process the input data differently, and transmit their output results to the next network layer until they are processed by the last network layer to obtain the code repair result of the source code file. The above-mentioned code repair results may include at least three, and the parameter input window sizes corresponding to different code repair models are also different.
[0096] After obtaining the initial prompt word information, the initial prompt word information can be input into multiple pre-built code repair models respectively to obtain corresponding code repair results. For example, when there are five code repair models, five code repair results are output accordingly. The code repair models can include, for example, the qwen model, the GLM3 model, the Lamda model, etc.
[0097] Among them, different parameter input windows correspond to different code repair models, and the corresponding input window sizes are also different. The initial prompt word information input to the code repair model can be adjusted accordingly according to the input window size. For example, since the additional part includes some non-essential information, when the initial prompt word information exceeds the input window size of the code repair model, the additional part can be deleted first to obtain updated prompt word information. If the updated prompt word information still exceeds the input window size of the code repair model, the contextual part can be deleted. Specifically, the code_snippet part can be deleted, so that only the essential part of the prompt word information remains. The defect type and defect location information included in the essential part are repaired through the code repair model according to the content in the essential part to obtain the corresponding code repair result.
[0098] In this embodiment, by inputting the initial prompt word information into multiple code repair models, multiple code repair results are obtained, different code repair results can be determined, and each code repair result is comprehensively compared, so that the final target code repair result is more accurate.
[0099] Step 205 , performing similarity analysis on each code repair result to determine a target code repair result, so as to perform code repair based on the code repair result.
[0100] The above target code repair result is a code repair result finally determined from multiple code repair results, and the accuracy of the target code repair result is higher.
[0101] After obtaining multiple code repair results, a similarity matrix can be calculated for the multiple code repair results. The similarity matrix includes multiple similarity element values, each similarity element value is used to characterize the similarity between every two code repair results, and then the target code repair result is determined based on the similarity matrix.
[0102] For example, see Figure 3As shown, after the source code file 3-1 and the code description file 3-2 are obtained, a defect analysis can be performed on the source code file 3-1 to obtain a code defect result 3-3, and information can be extracted from the code description file 3-2 to obtain summary information 3-4, and then the code defect result 3-3 and the summary information 3-4 are constructed into initial prompt word information 3-5, and the initial prompt word information 3-5 is input into multiple code repair models. For example, the code repair model includes n, namely, a first code repair model, a second code repair model, ..., an nth code repair model, and corresponding code repair results are obtained, namely, a first code repair result, a second code repair result, ..., an nth code repair result, and similarity analysis is performed on each code repair result to determine a target code repair result 3-6, so as to perform code repair through the code repair result.
[0103] The present application provides a code repair method, by obtaining a source code file and a corresponding code description file, performing vulnerability analysis on the source code file, obtaining a code defect result of the source code file, and extracting information from the code description file to obtain summary information of the source code file, and then constructing initial prompt word information based on the summary information and the code defect result, inputting the initial prompt word information into multiple pre-built code repair models, obtaining a code repair result corresponding to each code repair model, and performing similarity analysis on each code repair result to determine a target code repair result, so as to perform code repair through the code repair result. Compared with the prior art, the technical solution of the present application does not require a lot of manpower, and by automatically extracting information from the code description file to obtain summary information while obtaining the code defect result of the source code, it can provide necessary information and auxiliary information for subsequent code repair in a more comprehensive manner, and comprehensively consider the summary information and the code defect result, so as to accurately construct the initial prompt word information, input the initial prompt word information into multiple preset code repair models, obtain multiple code repair results, improve the accuracy of code repair, and then by performing similarity analysis on each code repair result, it can quickly and accurately obtain the target code repair result, further improving the efficiency of code repair.
[0104] In another exemplary embodiment of the present application, in order to accurately determine the target code repair result, the initial prompt word can be input into multiple pre-built code repair models to obtain the code repair result corresponding to each code repair model, such as Figure 4 As shown, the above step 204 is replaced by the following steps 301 to 303:
[0105] Step 301: for each code repair model, input the initial prompt word information into the code repair model to obtain a feedback value; the feedback value is used to indicate whether the initial prompt word information exceeds the parameter input window of the code repair model.
[0106] Specifically, the initial prompt word information is input into the code repair model to obtain a feedback value, which is used to characterize whether the initial prompt word information exceeds the parameter input window of the code repair model. For example, the feedback value can be represented by 0 or 1, wherein 0 indicates that the initial prompt word information exceeds the parameter input window of the code repair model, and 1 indicates that the initial prompt word information does not exceed the parameter input window of the code repair model. The feedback value is also customized according to actual needs.
[0107] After obtaining the initial prompt word information, multiple code repair models can be obtained by calling the interface, and then the initial prompt word information can be input into the code repair model to obtain a feedback value. According to the feedback value, it is determined whether the initial prompt word information exceeds the parameter input window of the code repair model. When the feedback value indicates that it does not exceed the parameter input window of the code repair model, the output result of the code repair model is obtained and the output result is used as the code repair result. For each code repair model, the initial prompt word information can be input into the code repair model to obtain the corresponding code repair result, which is represented by the following string form:
[0108] int result = 0; / / Initialize result. Make sure to initialize it before using it.
[0109] if (condition) {
[0110] result = calculateSomething();
[0111] }
[0112] System.out.println("The result is: " + result);
[0113] Among them, some of the above strings represent repair suggestions given by one of the models.
[0114] Step 302: if the feedback value indicates that the initial prompt word information exceeds the parameter input window of the code repair model, the initial prompt word information is adjusted based on the priority label to obtain the target prompt word information; the target prompt word information does not exceed the parameter input window of the code repair model.
[0115] Step 303: input the target prompt word information into the code repair model to obtain the code repair result.
[0116] Among them, when the feedback value represents that the initial prompt word information exceeds the parameter input window of the code repair model, it is necessary to adjust the initial prompt word information based on the priority label to obtain the target prompt word information. The target prompt word information is the length size and the parameters required by the parameter input window of the code repair model.
[0117] As an implementable manner, the summary information in the initial prompt word information may be deleted based on the priority tag to obtain the first prompt word information, and the first prompt word information may be used as the target prompt word information.
[0118] As another feasible method, the summary information in the prompt word information can be deleted based on the priority label to obtain the first prompt word information. When the first prompt word information is represented by the output result of the code repair model and the first prompt word information exceeds the parameter input window of the code repair model, the context information in the first prompt word information is deleted to obtain the second prompt word information, and the second prompt word information is used as the target prompt word information.
[0119] It should be noted that the first prompt word information is the prompt word information after deleting the summary information in the initial prompt word information, and the second prompt word information is the prompt word information after deleting the summary information and context information in the initial prompt word information. The priority of the code defect result is higher than the priority of the context information, and the priority of the context information is higher than the priority of the summary information.
[0120] Exemplarily, when there are five code repair models obtained, namely the first model, the second model, the third model, the fourth model and the fifth model, the initial prompt word information can be input into the five models respectively. For example, the initial prompt word information is first input into the first model to obtain a feedback value. For example, if the feedback value is 0, it means that the initial prompt word information exceeds the parameter input window of the code repair model, that is, the initial prompt word information needs to be adjusted according to the priority label. Since the priority order of the degree of influence on the code repair result is code defect result, context information, and summary information, the summary information in the initial prompt word information is first deleted to form the first prompt word information in json format. The first prompt word information is input into the first model to obtain the corresponding feedback value. For example, if the feedback value is 1, it means that the first prompt word information does not exceed the parameter input window of the code repair model. At this time, the first prompt word information is the target prompt word information, and the output result of the code repair model can be obtained. The output result is the code repair result A of the first model.
[0121] When the first prompt word is input into the first model and a corresponding feedback value is obtained, for example, the feedback value is 0, which indicates that the first prompt word information exceeds the parameter input window of the code repair model, the context information in the first prompt word information can be deleted to form the second prompt word information in json format, and the second prompt word information is input into the first model and a corresponding feedback value is obtained. For example, the feedback value is 1, which indicates that the second prompt word information does not exceed the parameter input window of the code repair model. At this time, the second prompt word information is the target prompt word information, and the output result of the code repair model can be obtained, which is the code repair result A of the first model.
[0122] Similarly, using a method similar to the first model mentioned above, the initial prompt word information is input into the second model in turn to obtain the corresponding code repair result B, and the initial prompt word information is input into the third model to obtain the corresponding code repair result C, the initial prompt word information is input into the fourth model to obtain the corresponding code repair result D, and the initial prompt word information is input into the fifth model to obtain the corresponding code repair result E.
[0123] Optionally, after the initial prompt word information is input into the code repair model, a feedback value is obtained. When the feedback value indicates that the initial prompt word information does not exceed the parameter input window of the code repair model, the output result of the code repair model is obtained, and the output result is used as the code repair result corresponding to the code repair model.
[0124] The above code repair result may be represented in the form of a string of codes, for example, it may be a result after correcting a code defect result.
[0125] In this embodiment, the initial prompt word information is input into multiple pre-built code repair models to obtain corresponding code repair results. The length of the code repair model input information can be automatically adjusted according to the feedback value. That is, when the input limit of the code repair model is exceeded, the non-essential information in the initial prompt word information is deleted layer by layer according to the priority label to reduce the length of the prompt word input of the code repair model until it meets the code repair model parameter input window, thereby improving the input flexibility.
[0126] In another exemplary embodiment of the present application, after determining each code repair result, in order to make the target code repair result more accurate, similarity analysis can be performed on each code repair result, such as Figure 5 As shown, the above step 205 may include the following steps 401 to 403:
[0127] Step 401 , perform syntax analysis on each code repair result to obtain syntax elements corresponding to each code repair result.
[0128] Step 402: construct a similarity matrix corresponding to each code repair result according to the syntax elements; the similarity matrix includes a plurality of similarity element values, and the similarity element values are used to represent the similarity between every two code repair results.
[0129] Step 403: Analyze the similarity matrix to determine the target code repair result.
[0130] Specifically, after obtaining each code repair result, you can first perform a grammatical analysis on it to obtain the grammatical element corresponding to each code repair result, which can be a token, for example. Among them, token refers to the smallest grammatical unit in a program, which represents a grammatical element in the code. The programming language will decompose the code repair results into tokens, and then determine the target code repair results based on these tokens. Optionally, tokens can be divided into multiple types, including identifiers, keywords, operators, delimiters, and literals.
[0131] A token is the smallest unit recognized by a compiler or interpreter. Generating tokens is one of the basic functions of a compiler or interpreter. Specifically, it can be done by first reading the code repair result, converting the code repair result character stream into a token sequence, and then identifying different types of tokens according to the grammar rules.
[0132] After obtaining the syntax elements, the similarity between each two code repair results can be calculated according to the syntax elements, and the similarity is used as the similarity element value between the two code repair results. The similarity element values are combined to construct a similarity matrix.
[0133] For example, there are five code repair models, namely the first model, the second model, the third model, the fourth model and the fifth model, and the obtained code repair results are A, B, C, D, and E respectively. For the code repair results A and B, the similarity J(A, B) can be calculated; wherein the similarity between the above code repair results A and B can be expressed by the following formula:
[0134]
[0135] For code repair results A and C, the similarity J(A, C) can be calculated, and so on, the similarity between each two code repair results can be obtained, thereby constructing a similarity matrix, which can be expressed in the following form:
[0136] ABCDE
[0137] A 1 J(A,B) J(A,C) J(A,D) J(A,E)
[0138] BJ(B,A) 1 J(B,C) J(B,D) J(B,E)
[0139] CJ(C,A) J(C,B) 1 J(C,D) J(C,E)
[0140] DJ(D,A) J(D,B) J(D,C) 1 J(D,E)
[0141] EJ(E,A) J(E,B) J(E,C) J(E,D) 1
[0142] It should be noted that the value range of the above similarity can be 0 to 1, wherein 1 indicates that the code repair results of the two are exactly the same, and 0 indicates that the code repair results are completely different.
[0143] Exemplarily, for code repair results A and B, in the process of determining the similarity J(A, B), it can be first determined that the grammatical elements of the code repair result A include 10, and the grammatical elements of the code repair result B include 20. For example, if the intersection of the grammatical elements in A and B is 5, and the intersection of the grammatical elements is 15, the corresponding similarity is 1 / 3.
[0144] As an implementable method, after constructing the similarity matrix, each similarity element value can be compared with a preset threshold, and the code repair results corresponding to the similarity element values greater than the preset threshold can be used as the candidate code repair results. The candidate code repair results are then analyzed to obtain the target code repair results.
[0145] As another feasible method, in the process of analyzing the similarity matrix and determining the target code repair result, the largest similarity element value can be first selected from the similarity matrix as the target similarity element value, and the two code repair results corresponding to the target similarity element value can be used as the candidate code repair results, and the average similarity between each candidate code repair result and the remaining code repair results can be calculated respectively; the remaining code repair results are the code repair results other than the candidate code repair results among all the code repair results, and the candidate code repair result corresponding to the largest similarity average value is used as the target code repair result.
[0146] Exemplarily, when the code repair results are A, B, C, D, and E respectively, and the maximum similarity element value in the similarity matrix is J(A, B), the code repair result A and the code repair result B can be used as candidate code repair results, and the similarity element values J(A, C), J(A, D), and J(A, E) of the code repair result A and the remaining code repair results C, D, and E are calculated, and then the similarity average value a of J(A, C), J(A, D), and J(A, E) is calculated, and then the similarity element values J(B, C), J(B, D), and J(B, E) of the code repair result B and the remaining code repair results C, D, and E are calculated, and then the similarity average value b of J(B, C), J(B, D), and J(B, E) is calculated, and then the size of the similarity average value a and the similarity average value b is determined, and the candidate code repair result with the largest similarity average value is used as the target code repair result, and the largest similarity average value indicates that the probability of the candidate code repair result overlapping with other code repair results is relatively large. For example, if the maximum similarity average value is similarity average value a, the code repair result A is used as the target code repair result.
[0147] In this embodiment, a grammatical analysis is performed on each code repair result to obtain the grammatical elements corresponding to each code repair result. A similarity matrix corresponding to each code repair result is constructed in more detail based on the grammatical elements, and the similarity matrix is analyzed. By integrating the output results of each code repair model, the target code repair result can be accurately determined, further improving the code repair efficiency.
[0148] The present application also provides an application scenario, which applies the above-mentioned code repair method. Specifically: the code repair method provided in this embodiment can be applied in a content development scenario. The source code file enters the defect repair stage from the code defect inspection stage, and after constructing the initial prompt word information, it is input into multiple pre-constructed code repair models to obtain a code repair result. The code repair method provided in this embodiment belongs to the stage of repairing code defects. Specifically, in the repair process for the source code file, the initial prompt word information can be constructed based on the source code file and the code description file, and the target code repair result can be obtained based on the code repair model, so as to perform code repair through the target code repair result.
[0149] Based on the same inventive concept, the embodiment of the present application also provides a code repair device for implementing the code repair method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in the one or more code repair device embodiments provided below can refer to the limitations of the code repair method above, and will not be repeated here.
[0150] In an exemplary embodiment, Figure 6 As shown, a code repair device is provided, comprising:
[0151] An acquisition module 610 is used to acquire a source code file and a corresponding code description file;
[0152] The processing module 620 is used to perform vulnerability analysis on the source code file to obtain code defect results of the source code file, and extract information from the code description file to obtain summary information of the source code file;
[0153] A prompt word construction module 630 is used to construct initial prompt word information according to the summary information and the code defect result;
[0154] The result determination module 640 is used to input the initial prompt word information into a plurality of pre-built code repair models to obtain the code repair result corresponding to each code repair model; the code repair model is trained based on the historical code defect results and the corresponding annotated repair results;
[0155] The repair module 650 is used to perform similarity analysis on each code repair result, determine a target code repair result, and perform code repair based on the target code repair result.
[0156] As an optional implementation, the prompt word construction module 630 is specifically used to:
[0157] Get the context information of code defect results in source code files;
[0158] Based on the code defect results, context information and summary information, the initial prompt word information is constructed; the context information, summary information and code defect results are annotated with priority labels; the priority labels are used to characterize the degree of influence of different information on the code repair results, the priority of the code defect results is greater than the priority of the context information, and the priority of the context information is greater than the priority of the summary information.
[0159] As an optional implementation, the result determination module 640 is specifically configured to:
[0160] For each code repair model, the initial prompt word information is input into the code repair model to obtain a feedback value; the feedback value is used to indicate whether the initial prompt word information exceeds the parameter input window of the code repair model;
[0161] If the feedback value indicates that the initial prompt word information exceeds the parameter input window of the code repair model, the initial prompt word information is adjusted based on the priority label to obtain the target prompt word information; the target prompt word information does not exceed the parameter input window of the code repair model;
[0162] The target prompt word information is input into the code repair model to obtain the code repair result.
[0163] As an optional implementation, the result determination module 640 is further configured to:
[0164] Based on the priority tag, the summary information in the initial prompt word information is deleted to obtain the first prompt word information, and the first prompt word information is used as the target prompt word information; or
[0165] Based on the priority label, the summary information in the prompt word information is deleted to obtain the first prompt word information. When the first prompt word information is represented by the output result of the code repair model that the first prompt word information exceeds the parameter input window of the code repair model, the context information in the first prompt word information is deleted to obtain the second prompt word information, and the second prompt word information is used as the target prompt word information.
[0166] As an optional implementation, the above device is also used for:
[0167] If the feedback value indicates that the initial prompt word information does not exceed the parameter input window of the code repair model, the output result of the code repair model is obtained;
[0168] The output result is used as the code repair result corresponding to the code repair model.
[0169] As an optional implementation, the repair module 650 is specifically configured to:
[0170] Performing grammatical analysis on each code repair result to obtain a grammatical element corresponding to each code repair result;
[0171] According to the grammatical elements, a similarity matrix corresponding to each code repair result is constructed; the similarity matrix includes a plurality of similar element values, and the similar element values are used to represent the similarity between each two code repair results;
[0172] Analyze the similarity matrix to determine the target code repair results.
[0173] As an optional implementation, the repair module 650 is further configured to:
[0174] Select the largest similar element value from the similarity matrix as the target similar element value;
[0175] The two code repair results corresponding to the target similar element values are used as candidate code repair results;
[0176] Calculate the average similarity between each candidate code repair result and the remaining code repair results respectively; the remaining code repair results are the code repair results other than the candidate code repair result among all the code repair results;
[0177] The candidate code repair result corresponding to the largest average similarity value is used as the target code repair result.
[0178] As an optional implementation, the acquisition module 610 is specifically configured to:
[0179] Perform lexical, grammatical and semantic analysis on the source code file to obtain a data flow graph;
[0180] The data flow graph is input into the preset detection model for defect detection to obtain the code defect results of the source code file.
[0181] Among them, the code repair device provided by this embodiment does not require a lot of manpower. By automatically extracting information from the code description file to obtain summary information while obtaining the code defect results of the source code, it can provide necessary information and auxiliary information for subsequent code repair in a more comprehensive manner, and comprehensively consider the summary information and the code defect results to accurately construct initial prompt word information, and input the initial prompt word information into multiple preset code repair models to obtain multiple code repair results, thereby improving the accuracy of code repair, and then by performing similarity analysis on each code repair result, the target code repair result can be obtained quickly and accurately, further improving the code repair efficiency.
[0182] In an exemplary embodiment, a computer device is provided. The computer device may be a server or a terminal. The internal structure diagram thereof may be as follows: Figure 7 As shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. Among them, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store code repair data. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a code repair method is implemented.
[0183] Those skilled in the art will understand that Figure 7The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0184] In an exemplary embodiment, a computer device is further provided, including a memory and a processor, wherein a computer program is stored in the memory, and the processor implements the steps in the above-mentioned method embodiments when executing the computer program.
[0185] In an exemplary embodiment, a computer-readable storage medium is provided, storing a computer program, and when the computer program is executed by a processor, the steps in the above method embodiments are implemented.
[0186] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant regulations.
[0187] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM may be in various forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM).
[0188] The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. The non-relational database may include a distributed database based on blockchain, etc., but is not limited thereto. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but is not limited thereto.
[0189] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0190] This article uses specific examples to illustrate the principles and implementation methods of this application. The description of the above embodiments is only used to help understand the method and core ideas of this application. At the same time, for those skilled in the art, according to the ideas of this application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as limiting this application.
Claims
1. A code repair method, characterized in that: The code repair method includes: Get the source code file and the corresponding code description file; Performing vulnerability analysis on the source code file to obtain code defect results of the source code file, and extracting information from the code description file to obtain summary information of the source code file; Constructing initial prompt word information according to the summary information and the code defect result; Inputting the initial prompt word information into a plurality of pre-built code repair models to obtain a code repair result corresponding to each code repair model; the code repair model is trained based on historical code defect results and corresponding annotated repair results; Performing similarity analysis on each of the code repair results to determine a target code repair result, so as to perform code repair using the target code repair result; The step of constructing the initial prompt word information according to the summary information and the code defect result specifically includes: Obtaining context information of the code defect result in the source code file; Initial prompt word information is constructed based on the code defect result, the context information and the summary information; the context information, the summary information and the code defect result are marked with priority tags; the priority tags are used to characterize the degree of influence of different information on the code repair result, the priority of the code defect result is greater than the priority of the context information, and the priority of the context information is greater than the priority of the summary information.
2. The code repair method according to claim 1, characterized in that: Inputting the initial prompt word information into a plurality of pre-built code repair models to obtain a code repair result corresponding to each code repair model, including: For each code repair model, the initial prompt word information is input into the code repair model to obtain a feedback value; the feedback value is used to indicate whether the initial prompt word information exceeds the parameter input window of the code repair model; If the feedback value indicates that the initial prompt word information exceeds the parameter input window of the code repair model, the initial prompt word information is adjusted based on the priority label to obtain target prompt word information; the target prompt word information does not exceed the parameter input window of the code repair model; The target prompt word information is input into the code repair model to obtain a code repair result.
3. The code repair method according to claim 2, characterized in that: Based on the priority tag, the initial prompt word information is adjusted to obtain target prompt word information, including: Based on the priority tag, the summary information in the initial prompt word information is deleted to obtain the first prompt word information, and the first prompt word information is used as the target prompt word information; or Based on the priority label, the summary information in the prompt word information is deleted to obtain the first prompt word information; when the first prompt word information is represented by the output result of the code repair model that the first prompt word information exceeds the parameter input window of the code repair model, the context information in the first prompt word information is deleted to obtain the second prompt word information; and the second prompt word information is used as the target prompt word information.
4. The code repair method according to claim 2, characterized in that: After obtaining the feedback value, the method further includes: If the feedback value indicates that the initial prompt word information does not exceed the parameter input window of the code repair model, obtaining the output result of the code repair model; The output result is used as the code repair result corresponding to the code repair model.
5. The code repair method according to claim 1, characterized in that: Performing similarity analysis on each of the code repair results to determine a target code repair result includes: Performing grammatical analysis on each of the code repair results to obtain grammatical elements corresponding to each code repair result; According to the grammatical elements, construct a similarity matrix corresponding to each of the code repair results; the similarity matrix includes a plurality of similarity element values, and the similarity element values are used to represent the similarity between every two code repair results; The similarity matrix is analyzed to determine the target code repair result.
6. The code repair method according to claim 5, characterized in that: Analyzing the similarity matrix to determine the target code repair result specifically includes: Selecting the maximum similarity element value from the similarity matrix as the target similarity element value; The two code repair results corresponding to the target similar element values are used as the code repair results to be selected; Calculating respectively the average similarity between each of the candidate code repair results and the remaining code repair results; the remaining code repair results are the code repair results other than the candidate code repair result among all the code repair results; The candidate code repair result corresponding to the largest average similarity value is used as the target code repair result.
7. A code repair device, characterized in that: The code repair device comprises: An acquisition module is used to acquire source code files and corresponding code description files; A processing module, configured to perform vulnerability analysis on the source code file to obtain code defect results of the source code file, and to extract information from the code description file to obtain summary information of the source code file; A prompt word construction module, used to construct initial prompt word information according to the summary information and the code defect result; A result determination module is used to input the initial prompt word information into a plurality of pre-built code repair models to obtain a code repair result corresponding to each code repair model; the code repair model is trained based on historical code defect results and corresponding annotated repair results; A repair module, used to perform similarity analysis on each of the code repair results, determine a target code repair result, and perform code repair based on the target code repair result; Among them, the prompt word construction module is specifically used for: Obtaining context information of the code defect result in the source code file; Initial prompt word information is constructed based on the code defect result, the context information and the summary information; the context information, the summary information and the code defect result are marked with priority tags; the priority tags are used to characterize the degree of influence of different information on the code repair result, the priority of the code defect result is greater than the priority of the context information, and the priority of the context information is greater than the priority of the summary information.
8. A computer device comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement the steps of the code repair method described in any one of claims 1 to 6.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the code repair method described in any one of claims 1 to 6 are implemented.
