Rule file generation method, electronic equipment and storage medium

By automatically selecting and generating rule files from the rule template library, the problem of complex and inefficient writing of rule files in the existing technology is solved, and the code scanning efficiency is improved.

CN119201087AActive Publication Date: 2024-12-27ZTE CORP
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202310773068.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-06-27
Publication Date
2024-12-27
Estimated Expiration
2043-06-27

AI Technical Summary

Technical Problem

In the prior art, the writing of rule files is complex and inefficient, which causes developers to spend a lot of time manually writing, affecting the efficiency of code scanning.

Method used

By obtaining code information and the information of the rule file template, selecting the corresponding template from the rule template library, and automatically generating the rule file to find the target code in the code to be scanned.

Benefits of technology

It improves the efficiency of rule files generation and code scanning efficiency, reduces the need to manually write rule files, and lowers the threshold for learning semantic patch languages.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119201087A_ABST
    Figure CN119201087A_ABST
Patent Text Reader

Abstract

The invention discloses a rule file generation method, which belongs to the field of computer security, is used for improving the generation efficiency of a rule file, and comprises the following steps: obtaining first code information and information of a rule file template corresponding to the first code information; selecting a rule file template from a rule template library according to the information of the rule file template; the first code information is written into the rule file template, a rule file is generated, and the rule file is used for searching target codes matched with the first code information in codes to be scanned.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of computer security, and specifically relates to a method for generating a rule file, an electronic device, and a storage medium. Background Art

[0002] Usually, source code defects are introduced in the coding stage, but most of them are not discovered in the coding stage, but in the testing stage. And the cost of fixing source code defects becomes higher as time goes by. And troubleshooting source code defects usually takes a lot of time for developers. Based on this situation, developers use static scanning tools to replace manual troubleshooting of defective codes. Static scanning tools scan defective codes by matching defective codes through rule files.

[0003] However, the writing of rule files is often very complicated, requiring developers to write them manually and spend a lot of time studying grammatical knowledge to write the corresponding rule files. This leads to a long cycle for writing rule files and low efficiency in generating rule files. Summary of the invention

[0004] The embodiments of the present application provide a method for generating a rule file, an electronic device, and a storage medium, which can solve the problem that the period of manually writing a rule file is too long, resulting in low efficiency in generating the rule file.

[0005] In a first aspect, an embodiment of the present application provides a method for generating a rule file, the method comprising: obtaining first code information and information of a rule file template corresponding to the first code information; selecting a rule file template from a rule template library based on the information of the rule file template; writing the first code information into the rule file template to generate a rule file, wherein the rule file is used to search for a target code in a code to be scanned that matches the first code information.

[0006] In second aspect, an embodiment of the present application provides a device for generating a rule file, the device comprising: an acquisition module for acquiring first code information and information of a rule file template corresponding to the first code information; a selection module for selecting a rule file template from a rule template library based on the information of the rule file template; an execution module for writing the first code information into the rule file template to generate a rule file, and the rule file is used to search for a target code in a code to be scanned that matches the first code information.

[0007] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method described in the first aspect.

[0008] In a fourth aspect, an embodiment of the present application provides a readable storage medium, on which a program or instruction is stored, and when the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.

[0009] In an embodiment of the present application, by obtaining first code information and information of a rule file template corresponding to the first code information; selecting a rule file template from a rule template library according to the information of the rule file template; writing the first code information into the rule file template to generate a rule file, the rule file is used to find a target code that matches the first code information in the code to be scanned, and by selecting a rule file template from the rule template library, the rule file is automatically generated according to the first code information, thereby avoiding manually writing a corresponding rule file for each code information, improving the generation efficiency of the rule file, and thereby improving the code scanning efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0010] Figure 1 It is a flowchart of a method for generating a rule file provided in an embodiment of the present application;

[0011] Figure 2 A rule file template and a rule file structure schematic diagram provided in an embodiment of the present application are shown;

[0012] Figure 3 This is a schematic diagram of an electronic device framework provided by an embodiment of the present application;

[0013] Figure 4 This is a schematic diagram of a rule generator workflow provided by an embodiment of the present application;

[0014] Figure 5 It is a structural schematic diagram of a device for generating a rule file provided in an embodiment of the present application;

[0015] Figure 6 It is a schematic diagram of the structure of another electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0016] 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 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.

[0017] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here, and the objects distinguished by "first", "second", etc. are generally of one type, and the number of objects is not limited. For example, the first object can be one or more. In addition, "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally indicates that the objects associated with each other are in an "or" relationship.

[0018] The following, in conjunction with the accompanying drawings, describes in detail the rule file generation method, electronic device and storage medium provided in the embodiments of the present application through specific embodiments and their application scenarios.

[0019] Figure 1 A method for generating a rule file provided by an embodiment of the present invention is shown. The method can be executed by an electronic device, and the electronic device may include: a server and / or a terminal device. In other words, the method can be executed by software or hardware installed in the electronic device, and the method includes the following steps:

[0020] Step 102: Obtain the first code information and the information of the rule file template corresponding to the first code information.

[0021] In the embodiment of the present application, a parameter input by a user can be received, and the parameter can be parsed to obtain the first code information, which can be a function name or a parameter name, etc., and the first code information is not limited here. At the same time, the information of the rule file template corresponding to the first code information can also be obtained, and the information of the rule file template is used to select a rule file template from a rule template library. Therefore, any information that can be used to select a rule file template corresponding to the first code information from the rule template library can be used as the information of the rule file template, and the information of the rule file template is not specifically limited here.

[0022] Step 104: Select a rule file template from a rule template library according to the information of the rule file template.

[0023] Specifically, after the information of the rule file template is obtained, a rule file template may be selected from a rule template library according to the information of the rule file template. There are a large number of different types of rule file templates in the rule template library.

[0024] Step 106: Write the first code information into the rule file template to generate a rule file.

[0025] Specifically, after obtaining the rule file template, the first code information can be written into the rule file template to generate a rule file, which is used to find the target code that matches the first code information in the code to be scanned. That is, the rule file can be used by a static scanning tool to find the target code that matches the first code information from the code to be scanned, and the target code may be a defective code.

[0026] The method for generating a rule file provided in an embodiment of the present application obtains first code information and information of a rule file template corresponding to the first code information; selects a rule file template from a rule template library according to the information of the rule file template; writes the first code information into the rule file template to generate a rule file, and the rule file is used to find a target code that matches the first code information in a code to be scanned. By selecting a rule file template from the rule template library, a rule file is automatically generated according to the first code information, thereby avoiding manually writing a corresponding rule file for each code information, improving the generation efficiency of the rule file, and thereby improving the code scanning efficiency.

[0027] In one implementation, after writing the first code information into a rule file template to generate a rule file, the method further includes:

[0028] When the target code is found in the code to be scanned through the rule file, the target code information corresponding to the target code is output.

[0029] Specifically, after the rule file is generated, the rule file can be used by a static scanning tool to search for a target code corresponding to the first code information from the code to be scanned. And through the rule file, when the target code is found in the code to be scanned, the target code information corresponding to the target code can be output. The target code information may include the target code and any information associated with it, and the target code information is not specifically limited here. In this way, through the rule file, when the target code is found in the code to be scanned, the target code information corresponding to the target code can be output, so that the developer can fully understand the target code information corresponding to the target code, and then the target code can be comprehensively analyzed and processed according to the target code information.

[0030] In one implementation, before obtaining the first code information and information of the rule file template corresponding to the first code information, the method further includes:

[0031] Analyze the defective source code to determine the second code information, which is of the same type as the first code information; determine the target embedding point based on the second code information, which is used to write the first code information; generate a rule file template through a semantic patch language and set the target embedding point in the rule file.

[0032] Specifically, before obtaining the first code information and the information of the rule file template corresponding to the first code information, it is necessary to analyze and summarize the defective source code encountered, analyze the logic of the defective source code, and determine the second code information of the defective source code. The second code information is of the same type as the first code information. For example, if the second code information is the function name of the defective code, then the first code information is also the function name of the defective code. According to the second code information, the target embedding point can be abstracted, and the target embedding point is used to write the first code information. The rule file template is generated by combining the semantic patch language (Semantic patch language, SmPL) with the target embedding point development, and the target embedding point is set in the rule file template. In this way, when the rule file is generated in combination with the first code information and the rule file template, the first code information can be written into the target embedding point to generate a rule file.

[0033] In an embodiment of the present application, the defective source code is analyzed to determine the second code information, and the target embedding point is determined according to the second code information. A rule file template is generated by a semantic patch language and the target embedding point is set in the rule file. The defective source code is logically abstracted to generate a corresponding rule file template and store it in a rule template library. A large number of rule file templates can be automatically generated for different code information, and the learning threshold of the SmPL language is lowered. When the developer needs to use the rule file template, he only needs to call the rule file template from the rule template library, and there is no need to study complex SmPL syntax knowledge, thereby improving the generation efficiency of the rule file.

[0034] In one implementation, the first code information is written into a rule file template to generate a rule file, including:

[0035] Write the first code information into the target embedding point in the rule file template to generate a rule file.

[0036] Specifically, the specific method of generating a rule file according to the first code information and the rule file template is to write the first code information into the target embedding point in the rule file template, and then the rule file can be generated. This avoids manually writing a corresponding rule file for each code information, improves the efficiency of generating the rule file, and then the rule file can be used to find the target code matching the first code information from the code to be scanned, thereby improving the efficiency of code scanning.

[0037] In one implementation, the rule file template includes a parameter declaration area and a code matching area, wherein the parameter declaration area is used to define parameters of the semantic patch language, and the code matching area includes target embedding points.

[0038] Specifically, Figure 2The rule file template and rule file structure diagram shown in the figure, the rule file template includes a parameter declaration area and a code matching area. The parameter declaration area is used to define the parameters of the SmPL language, and the code matching area is used to place the target tracking points. This area is the code embodiment of the abstract logic. The first code information obtained is written into this area. After the first code information is written into the code matching area, a rule file is generated. The rule file also includes a parameter declaration area and a code matching area.

[0039] In one implementation, the rule file template includes a print output area, and before outputting the target code information corresponding to the target code, the method further includes:

[0040] The warning information is outputted through the print output area, and the warning information is used to prompt that the target code is found.

[0041] Specifically, the rule templates in the rule template library are divided into four categories: patch, report, context, and org. Among them, the rule file templates in patch mode and context mode do not have a print output area, and the rule file templates in report and org mode include an output print area, such as Figure 2 As shown, the rule file template includes an output printing area, and the rule file generated according to the rule file template naturally also includes an output printing area, and the output printing area is used to print the output area output warning information, and the warning information is used to prompt that the target code is found, so as to remind the developer to find the target code matching the first code information.

[0042] In one implementation, obtaining the first code information and information of the rule file template corresponding to the first code information includes:

[0043] Receive command line parameters for instructing the generation of a rule file; obtain first code information, a storage path of a rule file template and a file name of the rule file template from the command line parameters; select a rule file template from a rule template library according to the information of the rule file template, including: selecting a rule file template from a rule template library according to the storage path and the file name.

[0044] Specifically, when it is necessary to scan the code to be scanned, a command line parameter input by the developer to instruct the generation of a rule file can be received, and the command line parameter includes the first code information and the information of the rule file template. Parsing the command line parameter can obtain the first code information and the storage path of the rule file template and the file name of the rule file template. According to the storage path of the rule file template and the file name of the rule file template, the rule file template can be selected from the rule template library. The command line parameters are as follows: python main.py -s dev_kfree_skb_irq -rttemplete / function.cocci, where dev_kfree_skb_irq is the information of the first code, templete / function.cocci is the storage path of the rule file template, and function.cocci is the file name of the rule file template. In this way, by receiving the command line parameters for indicating the generation of the rule file; obtaining the first code information, the storage path of the rule file template and the file name of the rule file template from the command line parameters; and according to the storage path and the file name, a rule file template can be selected from the rule template library to generate a rule file, and then the rule file can be used to perform code scanning, thereby avoiding manually writing a corresponding rule file for each code information, improving the generation efficiency of the rule file, and then improving the code scanning efficiency.

[0045] In one implementation, the first code information includes a function name and / or a parameter name corresponding to the target code.

[0046] Specifically, the first code information can be obtained by parsing the command line parameters. The first code information may be a function name and / or parameter name corresponding to the target code, such as the command line parameters shown below: python main.py -sdev_kfree_skb_irq -rt templete / function.cocci, where dev_kfree_skb_irq is the function name, and the function name is the information of the first code.

[0047] In one implementation, the target code is a defective code, and outputting target code information corresponding to the target code includes at least one of the following:

[0048] Output the position of the target code in the code to be scanned; output the target code and the context code of the target code, and the code line where the target code is located marked by the first marker; output the target code, the context code of the target code and the replacement code corresponding to the target code, mark the target code output by the second marker, and mark the replacement code output by the third marker, the replacement code is pre-set in the rule file template, and the replacement code is used to replace the target code.

[0049] Specifically, the target code is usually defective code. The templates in the rule template library are mainly divided into four categories: patch, report, context, and org. If the rule file template is in report mode or org mode, when the code to be scanned is scanned using the rule file generated according to the rule file template, the location of the target code in the code to be scanned will be output. As an example, the rule template of report mode is as follows:

[0050] virtual report / / report mode

[0051] @r@

[0052] position p; / / SmPL related parameters, used to bind and match the source code position.

[0053] @@

[0054] [function](...); @p / / target embedding point [function]

[0055] @script:python depends on report@

[0056] p< <r.p;

[0057] @@

[0058] msg="[Zeal Robot][function]has been discarded" / / The static scanning tool will prompt a warning message after hitting the target code

[0059] coccilib.report.print_report(msg,p[0]) / / Call function to print output information

[0060] Select the rule file template to generate the rule file:

[0061] python main.py -s dev_kfree_skb_irq -rt templete / function.cocci;

[0062] main.py is the rule generator, templete / is the template library, and the templates are stored in the directory. -s specifies the code information, and -rt specifies the report mode. The generated rule file is as follows:

[0063] Virtual Report

[0064] @r@

[0065] position p;

[0066] @@

[0067] dev_kfree_skb_irq(...); @p

[0068] @script:python depends on report@

[0069] p< <r.p;

[0070] @@

[0071] msg="[Zeal Robot]dev_kfree_skb_irq has been discarded"

[0072] coccilib.report.print_report(msg,p[0]);

[0073] Write dev_kfree_skb_irq code information into the buried point in the logic matching area, and finally generate a rule file dev_kfree_skb_irq.cocci. The function of this rule file is to find the dev_kfree_skb_irq function in the code, and then report the location of the function. Finally, the developer can decide whether to modify it.

[0074] The dev_kfree_skb_irq function is not safe and is not recommended to be used directly. The report mode rule file here is only responsible for querying and matching the dev_kfree_skb_irq function that appears in the code to be scanned, and does not provide a modification method. The specific modification method should be determined by the source code developer. Generally speaking, developers should use the dev_consume_skb_irq function instead.

[0075] The static scanning tool reads the rule file to match the target code. The command is as follows:

[0076] make coccicheck COCCI=<rule file> mode=report source code path to match=drivers / gpu / drm / msm / ;

[0077] Print information similar to the following:

[0078] drivers / gpu / drm / msm / dp / dp_audio.c:98:29-30:*****

[0079] drivers / gpu / drm / msm / dp / dp_ctrl.c:1299:46-47:*****

[0080] drivers / gpu / drm / msm / dp / dp_link.c:1202:25-26:*****

[0081] drivers / gpu / drm / msm / dp / dp_power.c:408:28-29:*****

[0082] Developers can then locate the problem code based on the information. For example:

[0083] skb_queue_walk_safe(&bcsp->unack,skb,tmp){

[0084] if (i>=pkts_to_be_removed)

[0085] break;

[0086] i++;

[0087] skb_unlink(skb,&bcsp->unack);

[0088] dev_kfree_skb_irq(skb);

[0089] }

[0090] As another example, here is the org-mode rules file template:

[0091] virtual org / / org mode

[0092] @r@

[0093] position p; / / SmPL related parameters, used to bind and match the source code position.

[0094] @@

[0095] [function](...); @p / / target embedding point [function]

[0096] @script:python depends on report@

[0097] p< <r.p;

[0098] @@

[0099] msg = "[Zeal Robot][function] has been discarded" / / The static scanning tool prints a warning message after hitting the target code

[0100] coccilib.org.print_todo(msg,p[0]) / / Output print information

[0101] The only difference between org mode and report mode is the mode declaration virtual xxx and the function coccilib.org.print_todo called to output print information.

[0102] Also generate the rule file through the rule generator:

[0103] python main.py -s dev_kfree_skb_irq -ro templete / function.cocci;

[0104] main.py is the rule generator, templete / is the template library, and the templates are stored in the directory. -s specifies the code information, -ro specifies the org mode, and the generated rule file is as follows:

[0105] virtual org

[0106] @r@

[0107] position p;

[0108] @@

[0109] dev_kfree_skb_irq(...); @p

[0110] @script:python depends on org@

[0111] p< <r.p;

[0112] @@

[0113] msg="[Zeal Robot]dev_kfree_skb_irq has been discarded"

[0114] coccilib.org.print_todo(msg,p[0]);

[0115] Scan using static tools:

[0116] make coccicheck COCCI=<rule file> pattern=org source code path to match=ipc /

[0117] org generates reports in org-mode format, for example:

[0118] *TODO[[view:. / ipc / sem.c::face=ovl-face1::linb=1967::colb=12::cole=13][[Ze al Robot]dev_kfree_skb_irq has been discarded. / ipc / sem.c::1967]];

[0119] The developer checks the target code based on the located position and makes the decision on the specific modification plan.

[0120] In addition, when the target code is found, the rule file generated by the rule file template of the context mode will output the target code and the context code of the target code, as well as the code line where the target code is output by marking it with a first marker, for example, the code line where the target code is located can be marked with "-". When the rule file generated by the rule file template of the patch mode is found, the target code, the context code of the target code and the replacement code corresponding to the target code are output, the target code is output by marking it with a second marker, for example, the code line where the target code is located can also be marked with "-", and the replacement code is output by marking it with a third marker, for example, the replacement code can be output by marking it with "+", and the replacement code is pre-set in the rule file template, and the replacement code is used to replace the target code.

[0121] As an example, the following is a rule file template for patch mode:

[0122] virtual patch / / Select patch mode

[0123] @r@

[0124] @@

[0125] -[Buried point 1](...);

[0126] +[Buried point 2](...);

[0127] The rule file template in patch mode does not need to output the print area.

[0128] Command line parameters: python main.py -s dev_kfree_skb_irq -d dev_consume_skb_irq -rp templete / function.cocci; -s puts dev_kfree_skb_irq into point 1, -d puts dev_consume_skb_irq into point 2, -rp selects the rule file template of patch mode to generate the rule file.

[0129] virtual patch

[0130] @r@

[0131] @@

[0132] -dev_kfree_skb_irq(...);

[0133] + dev_consume_skb_irq(...);

[0134] The "+" in the scan result indicates the suggested modification method, that is, replacing the code. This replacement code is determined by the developer who writes the rule file template. As for the target code scanned by the static scanning tool, it is up to the developer of the code to decide whether to modify it. The patch mode rule file only proposes a modification suggestion.

[0135] The static scanning tool reads the rule file to match the target code. The command is as follows:

[0136] make coccicheck COCCI=<rule file> pattern=patch source code path to match=test / ; the scanning results will include target code context and replacement code.

[0137] Here is the scan information:

[0138] diff-up a / test2.cb / test2.c

[0139] ---a / test2.c

[0140] +++b / test2.c

[0141] @@-2316,7+2316,7@@static inline void intel_pmu_ack_status(

[0142] skb_queue_walk_safe(&bcsp->unack,skb,tmp){

[0143] if (i>=pkts_to_be_removed)

[0144] break;

[0145] i++;

[0146] skb_unlink(skb,&bcsp->unack);

[0147] -dev_kfree_skb_irq(skb);

[0148] + dev_consume_skb_irq(skb);}

[0149] As another example, the rule file template for context mode is as follows:

[0150] @r@

[0151] pisition p; / / SmPL related parameters, used to bind and match the source code location.

[0152] @@

[0153] *[function](...); @p / / Target tracking point [function], the context mode uses "*" to indicate the focus line.

[0154] Like the patch mode, context mode does not require an output print area. Use the following command line to generate a specific rule file:

[0155] python main.py-s dev_kfree_skb_irq-rc templete / function.cocci;

[0156] -s puts dev_kfree_skb_irq into the embedding point, and -rc selects the rule file template of context mode to generate the rule file. The generated rule file is as follows:

[0157] virtual patch

[0158] @r@

[0159] @@

[0160] * dev_kfree_skb_irq(...)@p;

[0161] make coccicheck COCCI=<rule file> mode=context source code path to match=test / ;

[0162] Scan Results:

[0163] diff-up a / test2.cb / test2.c

[0164] ---. / test2.c

[0165] +++ / tmp / nothing / test2.c

[0166] @@-2316,7+2316,7@@static inline void intel_pmu_ack_status(

[0167] skb_queue_walk_safe(&bcsp->unack,skb,tmp){

[0168] if (i>=pkts_to_be_removed)

[0169] break;

[0170] i++;

[0171] skb_unlink(skb,&bcsp->unack);

[0172] -dev_kfree_skb_irq(skb);

[0173] The scanning results of context mode and patch mode will give the context code of the target code, but context does not give replacement code like patch mode. The specific modification is also decided by the code developer whether to modify and how to modify.

[0174] In an embodiment of the present application, the position of the target code in the code to be scanned is output, so that the developer can find the target code according to the position of the target code in the code to be scanned; the target code and the context code of the target code, as well as the code line where the target code outputted is located marked by a first marker, are output, so that the developer can find the target code according to the first marker and analyze it according to the target code and the context code of the target code to determine whether or how to modify the target code; the target code, the context code of the target code and the replacement code corresponding to the target code are output, the target code outputted is marked by a second marker, and the replacement code outputted is marked by a third marker, the replacement code is pre-set in a rule file template, the replacement code is used to replace the target code, and a solution for modifying the defective code can be given to improve work efficiency.

[0175] In an embodiment of the present application, the electronic device may include a rule generator and a rule template library, such as Figure 3 In the electronic device framework schematic diagram shown, the rule generator can parse the command line parameters, obtain the first code information and the rule file template information from the command line parameters, and select the rule file template from the rule template library according to the rule file template information, and then write the first code information into the rule file template to generate a rule file. There are various types of rule file templates in the rule template library.

[0176] In one implementation, Figure 4 This is a workflow diagram of a rule generator provided in an embodiment of the present application, such as Figure 4 As shown, the rule generator workflow includes the following steps:

[0177] Step 401: Get command line parameters.

[0178] Step 402: Parse command line parameters.

[0179] Specifically, the rule generator may parse the command line parameters, and obtain the first code information and the information of the rule file template corresponding to the first code information from the command line parameters.

[0180] Step 403: Select rule file template.

[0181] Specifically, after obtaining the information of the rule file template, the rule generator may select a rule file template corresponding to the information of the rule file template from the rule template library according to the information of the rule file template.

[0182] Step 404: Generate a rule file.

[0183] Specifically, after obtaining the rule file template, the rule generator may write the first code information into the embedding point of the rule file template to generate a rule file.

[0184] In an embodiment of the present application, a rule file template is selected from a rule template library by a rule generator, and a rule file is automatically generated according to the first code information, thereby avoiding manually writing a corresponding rule file for each code information, improving the generation efficiency of the rule file, and further improving the code scanning efficiency.

[0185] It should be noted that the execution subject of the method for generating a rule file provided in the embodiment of the present application may be a device for generating a rule file, or a control module in the device for generating a rule file for executing the method for generating a rule file. In the embodiment of the present application, the device for generating a rule file performing the method for generating a rule file is taken as an example to illustrate the device for generating a rule file provided in the embodiment of the present application.

[0186] Figure 5 Schematic diagram of the structure of a device for generating a rule file according to an embodiment of the present invention. Figure 5 As shown, the device 500 for generating a rule file includes: an acquisition module 510 , a selection module 520 and an execution module 530 .

[0187] The acquisition module 510 is used to obtain the first code information and the information of the rule file template corresponding to the first code information; the selection module 520 is used to select the rule file template from the rule template library according to the information of the rule file template; the execution module 530 is used to write the first code information into the rule file template to generate a rule file, and the rule file is used to find the target code that matches the first code information in the code to be scanned.

[0188] In one implementation, the execution module 530 is further configured to output target code information corresponding to the target code when the target code is found in the code to be scanned through the rule file.

[0189] In one implementation, the execution module 530 is also used to analyze the defective source code to determine second code information, which is of the same type as the first code information; determine a target embedding point based on the second code information, and the target embedding point is used to write the first code information; generate the rule file template through a semantic patch language and set the target embedding point in the rule file.

[0190] In one implementation, the execution module 530 is used to write the first code information into the target embedding point in the rule file template to generate the rule file.

[0191] In one implementation, the target code is a defective code, and the execution module 530 is used for at least one of the following: outputting the position of the target code in the code to be scanned; outputting the target code and the context code of the target code, and the code line where the target code is located, which is marked by a first marker; outputting the target code, the context code of the target code, and the replacement code corresponding to the target code, marking the target code output by a second marker, and the replacement code output by a third marker, wherein the replacement code is pre-set in the rule file template, and the replacement code is used to replace the target code.

[0192] In one implementation, the rule file template includes a parameter declaration area and a code matching area, wherein the parameter declaration area is used to define the parameters of the semantic patch language, and the code matching area includes the target embedding point.

[0193] In one implementation, the rule file template includes a print output area, and the execution module 530 is used to output warning information through the print output area, and the warning information is used to prompt that the target code is found.

[0194] In one implementation, the acquisition module 510 is used to receive command line parameters for instructing the generation of the rule file; obtain the first code information, the storage path of the rule file template and the file name of the rule file template from the command line parameters; and the selection module 520 is used to select the rule file template from the rule template library based on the storage path and the file name.

[0195] The generating device of the rule file in the embodiment of the present application can be a device, or a component, integrated circuit, or chip in a terminal. The device can be a mobile electronic device or a non-mobile electronic device. Exemplarily, the mobile electronic device can be a mobile phone, a tablet computer, a laptop computer, a PDA, an in-vehicle electronic device, a wearable device, an ultra-mobile personal computer (UMPC), a netbook, or a personal digital assistant (PDA), etc. The non-mobile electronic device can be a server, a network attached storage (NAS), a personal computer (PC), a television (TV), a teller machine or a self-service machine, etc., which is not specifically limited in the embodiment of the present application.

[0196] The device for generating the rule file in the embodiment of the present application may be a device having an operating system. The operating system may be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.

[0197] The rule file generation device provided in the embodiment of the present application can achieve Figure 1 To avoid repetition, the various processes implemented by the method embodiment are not described here.

[0198] Alternatively, if Figure 6As shown, an embodiment of the present application further provides an electronic device 600, including a processor 601 and a memory 602, wherein the memory 602 stores a program or instruction that can be run on the processor 601, and when the program or instruction is executed by the processor 601, it implements: obtaining first code information and information of a rule file template corresponding to the first code information; selecting a rule file template from a rule template library according to the information of the rule file template; writing the first code information into the rule file template to generate a rule file, and the rule file is used to find a target code that matches the first code information in the code to be scanned.

[0199] In one implementation, after the first code information is written into the rule file template to generate a rule file, when the target code is found in the code to be scanned through the rule file, the target code information corresponding to the target code is output.

[0200] In one implementation, before obtaining the first code information and the information of the rule file template corresponding to the first code information, the defective source code is analyzed to determine the second code information, and the second code information is of the same type as the first code information; a target embedding point is determined based on the second code information, and the target embedding point is used to write the first code information; the rule file template is generated by a semantic patch language and the target embedding point is set in the rule file.

[0201] In one implementation, the first code information is written into the target embedding point in the rule file template to generate the rule file.

[0202] In one implementation, the target code is a defective code, and the position of the target code in the code to be scanned is output; the target code and the context code of the target code, as well as the code line where the target code is located, which is marked by a first marker, are output; the target code, the context code of the target code, and the replacement code corresponding to the target code are output, the target code output is marked by a second marker, and the replacement code output is marked by a third marker, the replacement code is pre-set in the rule file template, and the replacement code is used to replace the target code.

[0203] In one implementation, the rule file template includes a parameter declaration area and a code matching area, wherein the parameter declaration area is used to define the parameters of the semantic patch language, and the code matching area includes the target embedding point.

[0204] In one implementation, the rule file template includes a print output area. Before outputting the target code information corresponding to the target code, warning information is output through the print output area, and the warning information is used to prompt that the target code is found.

[0205] In one implementation, a command line parameter for instructing generation of the rule file is received; the first code information, the storage path of the rule file template and the file name of the rule file template are obtained from the command line parameter; and the rule file template is selected from the rule template library based on the storage path and the file name.

[0206] The specific execution steps can refer to the various steps of the above-mentioned rule file generation method embodiment, and can achieve the same technical effect. To avoid repetition, they will not be repeated here.

[0207] It should be noted that the electronic devices in the embodiments of the present application include: servers, terminals, or other devices except terminals.

[0208] The above electronic device structure does not constitute a limitation on the electronic device. The electronic device may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently. For example, the input unit may include a graphics processing unit (GPU) and a microphone, and the display unit may be configured with a display panel in the form of a liquid crystal display, an organic light-emitting diode, etc. The user input unit includes a touch panel and at least one of other input devices. The touch panel is also called a touch screen. Other input devices may include, but are not limited to, a physical keyboard, function keys (such as volume control buttons, switch buttons, etc.), a trackball, a mouse, and a joystick, which will not be repeated here.

[0209] The memory can be used to store software programs and various data. The memory may mainly include a first storage area for storing programs or instructions and a second storage area for storing data, wherein the first storage area may store an operating system, an application program or instructions required for at least one function (such as a sound playback function, an image playback function, etc.), etc. In addition, the memory may include a volatile memory or a non-volatile memory, or the memory may include both volatile and non-volatile memories. Among them, the non-volatile memory may be 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), or a flash memory. Volatile memory can be random access memory (RAM), static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM) and direct memory bus random access memory (DRRAM).

[0210] The processor may include one or more processing units; optionally, the processor integrates an application processor and a modem processor, wherein the application processor mainly processes operations related to the operating system, user interface, and application programs, and the modem processor mainly processes wireless communication signals, such as a baseband processor. It is understandable that the modem processor may not be integrated into the processor.

[0211] An embodiment of the present application also provides a readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, each process of the above-mentioned rule file generation method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.

[0212] The processor is the processor in the electronic device described in the above embodiment. The readable storage medium includes a computer readable storage medium, such as ROM, RAM, magnetic disk or optical disk.

[0213] It should be noted that, in this article, the terms "comprise", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise one..." do not exclude the presence of other identical elements in the process, method, article or device including the element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in reverse order according to the functions involved, for example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.

[0214] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a computer software product, which is stored in a storage medium (such as ROM / RAM, a disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0215] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.

Claims

1. A method for generating a rule file, characterized in that: include: Acquire first code information and information of a rule file template corresponding to the first code information; Selecting a rule file template from a rule template library according to the information of the rule file template; The first code information is written into the rule file template to generate a rule file, and the rule file is used to search for a target code that matches the first code information in the code to be scanned.

2. The method according to claim 1, characterized in that After writing the first code information into the rule file template to generate a rule file, the method further includes: When the target code is found in the code to be scanned through the rule file, the target code information corresponding to the target code is output.

3. The method according to claim 1, characterized in that Before acquiring the first code information and information of the rule file template corresponding to the first code information, the method further includes: Analyze the defective source code to determine second code information, where the second code information is of the same type as the first code information; Determine a target embedding point according to the second code information, wherein the target embedding point is used to write the first code information; The rule file template is generated by a semantic patch language and the target embedding point is set in the rule file.

4. The method according to claim 3, characterized in that The step of writing the first code information into the rule file template to generate a rule file includes: The first code information is written into the target embedding point in the rule file template to generate the rule file.

5. The method according to claim 2, characterized in that: The target code is a defective code, and the outputting of target code information corresponding to the target code includes at least one of the following: Outputting the position of the target code in the code to be scanned; Outputting the target code and the context code of the target code, and marking the code line where the target code is outputted by a first marker; Output the target code, the context code of the target code and the replacement code corresponding to the target code, the target code outputted by marking the second marker, and the replacement code outputted by marking the third marker, the replacement code being pre-set in the rule file template, and the replacement code being used to replace the target code.

6. The method according to claim 3, characterized in that The rule file template includes a parameter declaration area and a code matching area, wherein the parameter declaration area is used to define the parameters of the semantic patch language, and the code matching area includes the target embedding point.

7. The method according to claim 2, characterized in that: The rule file template includes a print output area. Before outputting the target code information corresponding to the target code, the method further includes: The warning information is outputted through the print output area, and the warning information is used to prompt that the target code is found.

8. The method according to claim 1, characterized in that: The obtaining of the first code information and information of the rule file template corresponding to the first code information includes: Receiving a command line parameter for instructing to generate the rule file; Acquire the first code information, the storage path of the rule file template and the file name of the rule file template from the command line parameters; The selecting a rule file template from a rule template library according to the information of the rule file template includes: The rule file template is selected from the rule template library according to the storage path and the file name.

9. The method according to claim 1, characterized in that: The first code information includes a function name and / or a parameter name corresponding to the target code.

10. An electronic device, characterized in that: The method comprises a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method for generating a rule file as described in any one of claims 1 to 9.

11. A readable storage medium, characterized in that: The readable storage medium stores a program or instruction, and when the program or instruction is executed by a processor, the steps of the method for generating a rule file according to any one of claims 1 to 9 are implemented.

Citation Information

Patent Citations

  • Code quality scanning method and device, computer equipment and storage medium

    CN110716866A

  • Inspection method and device, computer equipment and storage medium

    CN111610999A

  • Code inspection rule generation method, code inspection method, code inspection device and medium

    CN113672512A

  • Code checking method and device, computer equipment and storage medium

    CN115328487A

  • Dynamic review of software updates after pull requests

    US20220214872A1