Code review method, device, electronic device and storage medium
By traversing the object code generation and running virtual trees and injecting use cases, the problem that static scans cannot detect code runtime errors is solved, and dynamic scans are realized to discover runtime errors, reducing the code bug rate and improving product quality.
Patent Information
- Application Number
- CN202211614209.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-15
- Publication Date
- 2025-08-22
- Estimated Expiration
- 2042-12-15
AI Technical Summary
The existing code scanning technology is mainly static scanning, which cannot detect code errors at runtime, especially dynamic languages such as front-end js language, which makes it difficult to detect runtime errors.
By dynamically scanning the object code, generating a running virtual tree, and injecting target use cases into the tree for traversal, runtime errors are found.
Dynamic code scanning can discover possible problems that may arise when the code is run in a specific scenario, reducing the code bug rate and improving product quality.
Smart Images

Figure CN116010250B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of code detection technology, and in particular to a code review method, device, electronic device and storage medium. Background Art
[0002] Currently, code scanning is all static scanning, detecting whether there are errors in code syntax, detecting whether there are security vulnerabilities in the code, etc. For example, sonar scanning and JSLint all detect coding conventions.
[0003] Front-end JavaScript is a static language (parsed language), unlike Java, which is a dynamic language. Java must be compiled before it can run, and the compilation process can detect potential runtime errors. Whether it's Sonar scanning or JSLint scanning, they all check coding conventions from top to bottom, without running the code. Instead, they perform analysis and testing in the order in which the code is executed, which can lead to runtime errors. For example, if a variable is an empty array under certain circumstances at runtime, but an array method is called, a runtime error will be reported. To avoid such errors, the general approach is to write test cases, or to detect them during code reviews or testing.
[0004] How to perform dynamic scanning at the system level (i.e., detecting whether there are errors after the code is compiled) is a problem that needs to be solved at present. Summary of the Invention
[0005] In view of this, embodiments of the present application provide a code review method, device, electronic device, and storage medium that can achieve dynamic scanning of codes.
[0006] The technical solution of the embodiment of the present application is implemented as follows:
[0007] In a first aspect, an embodiment of the present application provides a code review method, comprising the following steps:
[0008] sequentially scanning at least one subcode in the target code, wherein the target code is written in a dynamic language;
[0009] When the current subcode in the scan result has no syntax errors, a running virtual tree is generated for the current subcode, wherein the root node of the running virtual tree is the entry of the current subcode, and the child nodes of the running virtual tree are the running statements of the current subcode;
[0010] Injecting at least one preset target use case into the running virtual tree, wherein the at least one target use case is used to test the code virtual tree;
[0011] The running virtual tree is traversed, and when a running error occurs in the traversal result, error information for the running error is displayed.
[0012] In a possible implementation, the sequentially scanning at least one subcode in the target code includes:
[0013] Acquiring the target code and performing a reading process on the target code to obtain the at least one subcode in the target code;
[0014] Each subcode in the at least one subcode is scanned and detected in turn according to a preset encoding rule to detect whether there is a grammatical error in each subcode.
[0015] In a possible implementation, generating a running virtual tree for the current subcode includes:
[0016] Obtaining an entry of the current subcode, and using the entry as the root node of the running virtual tree;
[0017] The execution statements are sequentially acquired backward from the entry point, and the acquired execution statements are sequentially used as first child nodes of the execution virtual tree. When a function or interface exists in the execution statement, the function or interface in the execution statement is used as a second child node of the first child node.
[0018] In a possible implementation, injecting at least one preset target use case into the running virtual tree includes:
[0019] Presetting the at least one target use case, wherein the target use case includes at least one of a numerical boundary test case and an array or string call method non-empty judgment case;
[0020] Each target use case in the at least one target use case is injected into a corresponding child node in the running virtual tree.
[0021] In a possible implementation, traversing the running virtual tree includes:
[0022] The running virtual tree is traversed from left to right. When the at least one injected target use case is encountered during the traversal, the child nodes involved in the target use case are repeatedly traversed according to the number of the at least one target use case to determine the traversal result under the at least one target use case.
[0023] In a possible implementation, the displaying of error information regarding the operation error includes:
[0024] Obtain the code line where the runtime error occurs, as well as the error type and prompt information of the runtime error;
[0025] The erroneous code is marked in the target code according to the code line, and the error type and prompt information are displayed through a human-computer interaction interface.
[0026] In one possible implementation, the method further includes:
[0027] When the current subcode in the scanning result has the syntax error, or when the runtime error occurs in the traversal result, returning the target code;
[0028] After the target code is modified, the target code is re-audited in response to a submission operation of the modified target code.
[0029] In a second aspect, an embodiment of the present application further provides a code review device, the device comprising:
[0030] a scanning module, configured to sequentially scan at least one subcode in a target code, wherein the target code is written in a dynamic language;
[0031] a generating module, configured to generate, for the current subcode, a running virtual tree when there is no syntax error in the current subcode in the scanning result, wherein the root node of the running virtual tree is the entry of the current subcode, and the child nodes of the running virtual tree are running statements of the current subcode;
[0032] An injection module, configured to inject at least one preset target use case into the running virtual tree, wherein the at least one target use case is used to test the code virtual tree;
[0033] The traversal module is used to traverse the running virtual tree and display error information for the running error when a running error occurs in the traversal result.
[0034] In a third aspect, an embodiment of the present application further provides an electronic device comprising: a processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor communicates with the storage medium through the bus, and the processor executes the machine-readable instructions to execute the code review method described in any one of the first aspects.
[0035] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is run by a processor, the code review method described in any one of the first aspects is executed.
[0036] The embodiments of the present application have the following beneficial effects:
[0037] By sequentially scanning at least one sub-code in the target code, when the current sub-code in the scan result has no syntax errors, a virtual runtime tree is generated for the current sub-code, at least one preset target use case is injected into the virtual runtime tree, and the virtual runtime tree is traversed. When a runtime error occurs in the traversal result, an error message for the runtime error is displayed. In this way, a virtual runtime tree is generated for each section of code, and traversing each branch of the virtual tree is equivalent to running each branch of the code. The process of traversing each node of the tree is equivalent to the process of running the code. Therefore, dynamic code scanning can discover many problems that may only occur in specific scenarios when the code is running. It is difficult for humans to test all scenarios through code review or testing, but dynamic code scanning can run all branches of the code, which plays an important role in reducing the code bug rate and improving product quality. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other relevant drawings can be obtained based on these drawings without creative work.
[0039] Figure 1 10 is a flow chart of steps S101-S104 provided in an embodiment of the present application;
[0040] Figure 2 It is a flowchart of steps S201-S202 provided in an embodiment of the present application;
[0041] Figure 3 Schematic diagram of the process of steps S301-S302 provided in the embodiment of the present application;
[0042] Figure 4 This is a schematic diagram of the structure of the code review device provided in an embodiment of the present application;
[0043] Figure 5 It is a schematic diagram of the composition structure of the electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0044] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. It should be understood that the drawings in the present application only serve the purpose of illustration and description and are not used to limit the scope of protection of the present application. In addition, it should be understood that the schematic drawings are not drawn to scale. The flowcharts used in this application illustrate the operations implemented according to some embodiments of the present application. It should be understood that the operations of the flowcharts can be implemented out of sequence, and steps without logical context can be reversed or implemented simultaneously. In addition, those skilled in the art, under the guidance of the contents of this application, can add one or more other operations to the flowchart, or remove one or more operations from the flowchart.
[0045] In the following description, reference is made to “some embodiments”, which describes a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0046] In addition, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments. The components of the embodiments of the present application generally described and shown in the drawings here can be arranged and designed in various configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the claimed application, but merely represents selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work are within the scope of protection of the present application.
[0047] In the following description, the terms "first\second\third" involved are merely used to distinguish similar objects and do not represent a specific ordering of the objects. It can be understood that "first\second\third" can be interchanged with a specific order or sequence where permitted, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.
[0048] It should be noted that the term "comprising" will be used in the embodiments of the present application to indicate the existence of the features declared thereafter, but does not exclude the addition of other features.
[0049] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by those skilled in the art to which this application belongs. The terms used herein are for the purpose of describing the embodiments of this application and are not intended to limit this application.
[0050] When implementing the embodiments of this application, the applicant discovered the following problems:
[0051] The front-end js language is a static language (parsed language), unlike java which is a dynamic language. Java can only run after compilation. The java compilation process can detect errors that may occur at runtime. Whether it is sonar scanning or JSLint scanning, etc., they all check the coding conventions from front to back, without running the code. They traverse the analysis and detection according to the running order of the code, so there will be errors at runtime. For example, at runtime, a variable is an empty array in a specific case, but the array method is called, then an error will be reported at runtime. In order to avoid such a situation, this application provides the following method.
[0052] See also Figure 1 , Figure 1 This is a flow chart of steps S101-S104 of the code review method provided in the embodiment of the present application, which will be combined with Figure 1 Steps S101-S104 are shown for explanation.
[0053] Step S101, sequentially scanning at least one sub-code in a target code, wherein the target code is written in a dynamic language;
[0054] Step S102: When the current subcode in the scan result has no syntax errors, a virtual execution tree is generated for the current subcode, wherein the root node of the virtual execution tree is the entry of the current subcode, and the child nodes of the virtual execution tree are execution statements of the current subcode;
[0055] Step S103: injecting at least one preset target use case into the running virtual tree, wherein the at least one target use case is used to test the code virtual tree;
[0056] Step S104: traverse the execution virtual tree, and when an execution error occurs in the traversal result, display error information for the execution error.
[0057] The above-mentioned code review method sequentially scans at least one sub-code in the target code. When the current sub-code in the scan result does not have a syntax error, a running virtual tree is generated for the current sub-code, and at least one preset target use case is injected into the running virtual tree. The running virtual tree is traversed, and when a running error occurs in the traversal result, an error message for the running error is displayed. In this way, a running virtual tree is generated for each section of code, and each branch of the virtual tree is traversed as each branch of the running code. The process of traversing each node of the tree is the process of running the code. Therefore, dynamic code scanning can discover many problems that may only occur in specific scenarios when the code is running. It is difficult for people to test all scenarios through code review or testing, but through dynamic code scanning, all branches of the code can be run, which plays an important role in reducing the bug rate of the code and improving product quality.
[0058] The above exemplary steps of the embodiment of the present application are described below respectively.
[0059] In step S101, at least one sub-code in a target code is scanned in sequence, wherein the target code is written in a dynamic language.
[0060] In step S102, when the current sub-code in the scanning result has no syntax errors, a running virtual tree is generated for the current sub-code, wherein the root node of the running virtual tree is the entry of the current sub-code, and the child nodes of the running virtual tree are the running statements of the current sub-code.
[0061] In step S103, at least one preset target use case is injected into the running virtual tree, wherein the at least one target use case is used to test the code virtual tree.
[0062] In step S104, the execution virtual tree is traversed, and when an execution error occurs in the traversal result, error information for the execution error is displayed.
[0063] In some embodiments, see Figure 2 , Figure 2 This is a flow chart of steps S201-S202 provided in an embodiment of the present application. The sequential scanning and processing of at least one sub-code in the target code can be achieved through steps S201-S202, which will be explained in conjunction with each step.
[0064] In step S201, the target code is acquired and read to obtain the at least one sub-code in the target code.
[0065] In step S202, each subcode in the at least one subcode is scanned and detected in turn according to a preset encoding rule to detect whether there is a grammatical error in each subcode.
[0066] Here, the code is syntax checked from top to bottom. Just like a normal system, the coding conventions are checked to detect syntax errors and security vulnerabilities.
[0067] For example, you can use Sonar to scan the target code. Sonar, whose full name is SonarQube, is an open source platform for code quality management. Sonar provides a platform for code quality management and integrates traditional code static detection tools such as PMD and FindBugs. It can support code quality management and detection in more than 20 programming languages including Java, C#, C / C++, PL / SQL, Cobol, JavaScript, Groovy, etc.
[0068] In some embodiments, see Figure 3 , Figure 3 This is a flow chart of steps S301-S302 provided in an embodiment of the present application. The generation of a running virtual tree for the current sub-code can be achieved through steps S301-S302, which will be explained in conjunction with each step.
[0069] In step S301, the entry of the current subcode is obtained, and the entry is used as the root node of the running virtual tree.
[0070] In step S302, the execution statements are sequentially acquired from the entry point and used as the first child nodes of the execution virtual tree. When a function or interface exists in the execution statement, the function or interface in the execution statement is used as the second child node of the first child node.
[0071] The scanning provided in the above embodiment is only a static scan, which only checks the syntax and does not actually compile the code, so there are limitations.
[0072] Here, dynamic scanning begins. The entry point of the code is the root node. The statements executed by the code generate child nodes. If the statement contains a function or interface, child nodes are generated again, and the virtual tree is generated and executed in sequence.
[0073] In some embodiments, injecting at least one preset target use case into the running virtual tree includes:
[0074] Presetting the at least one target use case, wherein the target use case includes at least one of a numerical boundary test case and an array or string call method non-empty judgment case;
[0075] Each target use case in the at least one target use case is injected into a corresponding child node in the running virtual tree.
[0076] Here, common use cases are injected. Common use cases include upper and lower limits of numbers, non-empty judgment of array and string calling methods, etc.
[0077] In some embodiments, traversing the running virtual tree includes:
[0078] The running virtual tree is traversed from left to right. When the at least one injected target use case is encountered during the traversal, the child nodes involved in the target use case are repeatedly traversed according to the number of the at least one target use case to determine the traversal result under the at least one target use case.
[0079] Here, we traverse the virtual tree. We traverse the tree from the left, and after traversing all branches of the virtual tree, we start traversing all possible cases when we encounter a use case. All use cases are traversed in the virtual tree to check for errors.
[0080] In this approach, each code segment generates a virtual execution tree. Each branch of the virtual execution tree corresponds to each branch of the code. Traversing every node in the tree completes the code execution. Therefore, dynamic code scanning can run all code scenarios, preventing issues discovered after code release. This plays a significant role in reducing bugs and improving product quality.
[0081] In some embodiments, displaying error information for the running error includes:
[0082] Obtain the code line where the runtime error occurs, as well as the error type and prompt information of the runtime error;
[0083] The erroneous code is marked in the target code according to the code line, and the error type and prompt information are displayed through a human-computer interaction interface.
[0084] Here, in order to enable the user to quickly locate the erroneous code, the code line where the runtime error occurs can be obtained, and then the erroneous code can be highlighted in the original target code so that the user can quickly locate the error location.
[0085] In addition, since this application uses dynamic scanning, there are execution-level error messages for the code scanning results. These error messages include error types and prompt information. In order to facilitate users to quickly identify error information, the error type and prompt information can also be displayed through the human-computer interaction interface.
[0086] In some embodiments, the method further comprises:
[0087] When the current subcode in the scanning result has the syntax error, or when the runtime error occurs in the traversal result, returning the target code;
[0088] After the target code is modified, the target code is re-audited in response to a submission operation of the modified target code.
[0089] Here, when the syntax check starts or an error is found during the traversal, the target code can be returned for the user to modify. After the user completes the modification, click Submit, and the code will be reviewed again to execute the method provided in the embodiment of the present application.
[0090] In summary, the embodiments of the present application have the following beneficial effects:
[0091] By sequentially scanning at least one sub-code in the target code, when the current sub-code in the scan result has no syntax errors, a virtual runtime tree is generated for the current sub-code, at least one preset target use case is injected into the virtual runtime tree, and the virtual runtime tree is traversed. When a runtime error occurs in the traversal result, an error message for the runtime error is displayed. In this way, a virtual runtime tree is generated for each section of code, and traversing each branch of the virtual tree is equivalent to running each branch of the code. The process of traversing each node of the tree is equivalent to the process of running the code. Therefore, dynamic code scanning can discover many problems that may only occur in specific scenarios when the code is running. It is difficult for humans to test all scenarios through code review or testing, but dynamic code scanning can run all branches of the code, which plays an important role in reducing the code bug rate and improving product quality.
[0092] Based on the same inventive concept, the embodiment of the present application also provides a code review device corresponding to the code review method in the first embodiment. Since the principle of solving the problem by the device in the embodiment of the present application is similar to the above-mentioned code review method, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0093] like Figure 4 As shown, Figure 4 : is a schematic diagram of the structure of the code audit device 400 provided in an embodiment of the present application. The code audit device 400 includes:
[0094] A scanning module 401 is configured to sequentially scan at least one sub-code in a target code, wherein the target code is written in a dynamic language;
[0095] A generating module 402 is configured to generate a virtual execution tree for the current subcode when there is no syntax error in the current subcode in the scanning result, wherein the root node of the virtual execution tree is the entry of the current subcode and the child nodes of the virtual execution tree are execution statements of the current subcode;
[0096] An injection module 403 is configured to inject at least one preset target use case into the running virtual tree, wherein the at least one target use case is used to test the code virtual tree;
[0097] The traversal module 404 is configured to traverse the execution virtual tree and, when an execution error occurs in the traversal result, display error information for the execution error.
[0098] It should be understood by those skilled in the art that Figure 4 The implementation functions of each unit in the code auditing device 400 shown can be understood by referring to the relevant description of the aforementioned code auditing method. Figure 4 The functions of the various units in the code auditing device 400 shown can be implemented by a program running on a processor, or by a specific logic circuit.
[0099] In a possible implementation, the scanning module 401 sequentially scans at least one subcode in the target code, including:
[0100] Acquiring the target code and performing a reading process on the target code to obtain the at least one subcode in the target code;
[0101] Each subcode in the at least one subcode is scanned and detected in turn according to a preset encoding rule to detect whether there is a grammatical error in each subcode.
[0102] In a possible implementation, the generating module 402 generates a running virtual tree for the current sub-code, including:
[0103] Obtaining an entry of the current subcode, and using the entry as the root node of the running virtual tree;
[0104] The execution statements are sequentially acquired backward from the entry point, and the acquired execution statements are sequentially used as first child nodes of the execution virtual tree. When a function or interface exists in the execution statement, the function or interface in the execution statement is used as a second child node of the first child node.
[0105] In a possible implementation, the injection module 403 injects at least one preset target use case into the running virtual tree, including:
[0106] Presetting the at least one target use case, wherein the target use case includes at least one of a numerical boundary test case and an array or string call method non-empty judgment case;
[0107] Each target use case in the at least one target use case is injected into a corresponding child node in the running virtual tree.
[0108] In a possible implementation, the traversal module 404 traverses the running virtual tree, including:
[0109] The running virtual tree is traversed from left to right. When the at least one injected target use case is encountered during the traversal, the child nodes involved in the target use case are repeatedly traversed according to the number of the at least one target use case to determine the traversal result under the at least one target use case.
[0110] In a possible implementation, the traversal module 404 displays error information for the execution error, including:
[0111] Obtain the code line where the runtime error occurs, as well as the error type and prompt information of the runtime error;
[0112] The erroneous code is marked in the target code according to the code line, and the error type and prompt information are displayed through a human-computer interaction interface.
[0113] In one possible implementation, the method further includes:
[0114] When the current subcode in the scanning result has the syntax error, or when the runtime error occurs in the traversal result, returning the target code;
[0115] After the target code is modified, the target code is re-audited in response to a submission operation of the modified target code.
[0116] The above-mentioned code auditing device scans at least one sub-code in the target code in sequence. When the current sub-code in the scan result has no syntax errors, a running virtual tree is generated for the current sub-code, and at least one preset target use case is injected into the running virtual tree. The running virtual tree is traversed, and when a running error occurs in the traversal result, the error message for the running error is displayed. In this way, a running virtual tree is generated for each piece of code, and each branch of the virtual tree is traversed, which is the same as each branch of the running code. The process of traversing each node of the tree is the process of running the code. Therefore, dynamic code scanning can find many problems that may only occur in specific scenarios when the code is running. It is difficult for people to test all scenarios through code review or testing, but through dynamic code scanning, all branches of the code can be run, which plays an important role in reducing the bug rate of the code and improving product quality.
[0117] like Figure 5 As shown, Figure 5 This is a schematic diagram of the structure of an electronic device 500 provided in an embodiment of the present application. The electronic device 500 includes:
[0118] A processor 501, a storage medium 502 and a bus 503, wherein the storage medium 502 stores machine-readable instructions executable by the processor 501. When the electronic device 500 is running, the processor 501 communicates with the storage medium 502 through the bus 503, and the processor 501 executes the machine-readable instructions to perform the steps of the code review method described in the embodiment of the present application.
[0119] In actual application, the various components in the electronic device 500 are coupled together via the bus 503. It is understood that the bus 503 is used to realize the connection and communication between these components. In addition to the data bus, the bus 503 also includes a power bus, a control bus, and a status signal bus. However, for the sake of clarity, Figure 5 Various buses are labeled as bus 503.
[0120] The electronic device sequentially scans at least one subcode in the target code. When the current subcode in the scan result does not have a syntax error, a virtual runtime tree is generated for the current subcode. At least one preset target use case is injected into the virtual runtime tree, and the virtual runtime tree is traversed. When a runtime error occurs in the traversal result, an error message for the runtime error is displayed. In this way, a virtual runtime tree is generated for each code segment, and traversing each branch of the virtual runtime tree is equivalent to running each branch of the code. The process of traversing each node of the tree is equivalent to the process of running the code. Therefore, dynamic code scanning can discover many problems that may only occur in specific scenarios when the code is running. It is difficult for humans to test all scenarios through code review or testing, but dynamic code scanning can run all branches of the code, which plays an important role in reducing the bug rate of the code and improving product quality.
[0121] The embodiment of the present application further provides a computer-readable storage medium, which stores executable instructions. When the executable instructions are executed by at least one processor 501, the code review method described in the embodiment of the present application is implemented.
[0122] In some embodiments, the storage medium can be a magnetic random access memory (FRAM), a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a flash memory, a magnetic surface storage, an optical disc, or a compact disc read-only memory (CD-ROM); it can also be various devices including one or any combination of the above memories.
[0123] In some embodiments, executable instructions may be in the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a stand-alone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.
[0124] As an example, executable instructions may, but do not necessarily, correspond to a file in a file system, may be stored as part of a file that stores other programs or data, for example, in one or more scripts in a HyperText Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple coordinated files (for example, files storing one or more modules, subroutines, or code portions).
[0125] By way of example, executable instructions may be deployed to be executed on one computing device, or on multiple computing devices at one site, or on multiple computing devices distributed across multiple sites and interconnected by a communication network.
[0126] The computer-readable storage medium sequentially scans at least one subcode in the target code. When the current subcode in the scan result has no syntax errors, a virtual runtime tree is generated for the current subcode, at least one preset target use case is injected into the virtual runtime tree, and the virtual runtime tree is traversed. When a runtime error occurs in the traversal result, an error message for the runtime error is displayed. In this way, a virtual runtime tree is generated for each section of code, and traversing each branch of the virtual tree is equivalent to running each branch of the code. The process of traversing each node of the tree is equivalent to the process of running the code. Therefore, dynamic code scanning can discover many problems that may only occur in specific scenarios when the code is running. It is difficult for humans to test all scenarios through code review or testing, but dynamic code scanning can run all branches of the code, which plays an important role in reducing the bug rate of the code and improving product quality.
[0127] In the several embodiments provided in this application, it should be understood that the disclosed methods and electronic devices can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is merely a logical function division. In actual implementation, there may be other division methods, such as: multiple units or components can be combined, or can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of the devices or units can be electrical, mechanical or other forms.
[0128] The modules described as separate components may or may not be physically separate, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed across multiple network elements. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0129] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0130] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium and includes several instructions for enabling a computer device (which can be a personal computer, platform server, or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes various media that can store program codes, such as a USB flash drive, a mobile hard disk, a ROM, a RAM, a magnetic disk, or an optical disk.
[0131] The above are only specific embodiments of the present application, but the scope of protection of this application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
Claims
1. A code review method, characterized in that: The following steps are involved: sequentially scanning at least one subcode in the target code, wherein the target code is written in a dynamic language; When the current subcode in the scan result has no syntax errors, a running virtual tree is generated for the current subcode, wherein the root node of the running virtual tree is the entry of the current subcode, and the child nodes of the running virtual tree are the running statements of the current subcode; Injecting at least one preset target use case into the running virtual tree, wherein the at least one target use case is used to test the code virtual tree; Traversing the running virtual tree, and when a running error occurs in the traversal result, displaying error information for the running error; The step of generating a running virtual tree for the current subcode includes: Obtaining an entry of the current subcode, and using the entry as the root node of the running virtual tree; The execution statements are sequentially acquired from the entry point and used as first child nodes of the execution virtual tree. When a function or interface exists in the execution statement, the function or interface in the execution statement is used as a second child node of the first child node. The injecting at least one preset target use case into the running virtual tree includes: Presetting the at least one target use case, wherein the target use case includes at least one of a numerical boundary test case and an array or string call method non-empty judgment case; Each target use case in the at least one target use case is injected into a corresponding child node in the running virtual tree.
2. The method according to claim 1, characterized in that The step of sequentially scanning at least one subcode in the target code includes: Acquiring the target code and performing a reading process on the target code to obtain the at least one subcode in the target code; Each subcode in the at least one subcode is scanned and detected in turn according to a preset encoding rule to detect whether there is a grammatical error in each subcode.
3. The method according to claim 1, characterized in that The traversing the running virtual tree includes: The running virtual tree is traversed from left to right. When the at least one injected target use case is encountered during the traversal, the child nodes involved in the target use case are repeatedly traversed according to the number of the at least one target use case to determine the traversal result under the at least one target use case.
4. The method according to claim 1, wherein The displaying of error information for the operation error includes: Obtain the code line where the runtime error occurs, as well as the error type and prompt information of the runtime error; The erroneous code is marked in the target code according to the code line, and the error type and prompt information are displayed through a human-computer interaction interface.
5. The method according to claim 1, wherein The method further comprises: When the current subcode in the scanning result has the syntax error, or when the runtime error occurs in the traversal result, returning the target code; After the target code is modified, the target code is re-audited in response to a submission operation of the modified target code.
6. A code auditing device, characterized in that: The device comprises: a scanning module, configured to sequentially scan at least one subcode in a target code, wherein the target code is written in a dynamic language; A generation module is configured to generate a running virtual tree for the current subcode when there is no syntax error in the current subcode in the scanning result, wherein the root node of the running virtual tree is the entry of the current subcode, and the child nodes of the running virtual tree are the running statements of the current subcode; the generating of the running virtual tree for the current subcode includes: obtaining the entry of the current subcode and using the entry as the root node of the running virtual tree; obtaining the running statements from the entry in sequence backward, and using the obtained running statements in sequence as the first child nodes of the running virtual tree, and when there is a function or interface in the running statement, using the function or interface in the running statement as the second child node of the first child node; An injection module is used to inject at least one preset target use case into the running virtual tree, wherein the at least one target use case is used to test the code virtual tree; the injecting of at least one preset target use case into the running virtual tree comprises: presetting the at least one target use case, wherein the target use case comprises at least one of a numerical boundary test case and an array or string call method non-empty judgment case; injecting each target use case in the at least one target use case into a corresponding child node in the running virtual tree The traversal module is used to traverse the running virtual tree and display error information for the running error when a running error occurs in the traversal result.
7. An electronic device, characterized in that: include: A processor, a storage medium and a bus, wherein the storage medium stores machine-readable instructions executable by the processor. When the electronic device is running, the processor and the storage medium communicate via the bus, and the processor executes the machine-readable instructions to perform the code review method according to any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the code review method according to any one of claims 1 to 5 is executed.
Citation Information
Patent Citations
Static source code scanning method, device, computer device and storage medium
CN109117633A
Source code test method and device, electronic equipment and storage medium
CN114860566A