Code annotation processing method and electronic equipment

By adding and propagating null value annotations in the code, the shortcomings of static code analysis tools in detecting null pointer reference defects are addressed, enabling more comprehensive null pointer reference defect detection and improving detection effectiveness and coverage.

CN121833448APending Publication Date: 2026-04-10HONOR DEVICE CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
HONOR DEVICE CO LTD
Filing Date
2024-10-10
Publication Date
2026-04-10

AI Technical Summary

Technical Problem

Existing static code defect detection tools struggle to effectively detect null pointer reference defects in the absence of null value annotations, resulting in poor detection performance.

Method used

By building a null value knowledge base, null value type annotations are added to the code based on null value annotation information, and the annotations are propagated using code structure information, thereby expanding the annotation coverage and improving the detection effect.

Benefits of technology

It effectively improves the detection coverage and accuracy of null pointer reference defects, thereby improving code quality.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121833448A_ABST
    Figure CN121833448A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code annotation processing method and electronic equipment, and relates to the technical field of computers. The method comprises the steps that according to a first null value knowledge base and a second null value knowledge base, a first annotation of a null value type is added to a first position in a first code, a second code is obtained, the first null value knowledge base is generated according to a first log, the first log comprises record information of a fault caused by null pointer dereference, and the second null value knowledge base is generated according to a second log; the second null knowledge base is generated according to the standard library source code of the first programming language. According to code structure information corresponding to the second code, a second annotation of the null value type is added to a second position in the second code, a dependency relationship exists between code elements corresponding to the second position and code elements corresponding to the first position, and the code structure information is used for indicating the dependency relationship. In this way, the comprehensiveness of null value annotations in codes can be effectively improved, and then the detection effect of null pointer reference defects is improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer, in particular to a code annotation processing method and an electronic device. BACKGROUND

[0002] Null pointer dereference defect is a programming error, which occurs when a program attempts to reference an object or variable of a null pointer, as the null pointer does not point to a valid object or memory location.

[0003] In the related art, a static code defect detection tool can be used to scan the null pointer dereference defect to feed back the hidden defect code, so as to improve the problem of reducing the null pointer dereference defect. However, most static code defect detection tools rely on null value annotations in the code, and in the case of missing null value annotations in the code, the detection effect of the null pointer dereference defect is poor. SUMMARY

[0004] The embodiments of the present application provide a code annotation processing method and an electronic device, which are applied to the technical field of computer to improve the comprehensiveness of null value annotations in the code, and further effectively improve the detection effect of the null pointer dereference defect.

[0005] In a first aspect, the embodiments of the present application provide a code annotation processing method. The method comprises:

[0006] adding a first annotation of a null value type at a first position in the first code according to the first null value knowledge base and the second null value knowledge base, to obtain a second code, wherein the first null value knowledge base is generated according to a first log, the first log contains record information of a fault caused by null pointer dereference, and the second null value knowledge base is generated according to a standard library source code of the first programming language;

[0007] adding a second annotation of the null value type at a second position in the second code according to code structure information corresponding to the second code, wherein a code element corresponding to the second position has a dependency relationship with a code element corresponding to the first position, and the code structure information is used to indicate the dependency relationship.

[0008] In this way, the null value annotations can be added in the original first code based on the knowledge base containing the null value annotation information, so as to effectively improve the coverage comprehensiveness of the control annotations in the code. Further, after the second code is obtained by adding the null value annotations in the first code, the null value annotations can be further propagated in the second code based on the dependency relationship between the code elements, so as to further expand the coverage range of the null value annotations. Based on the effective addition of the null value annotations in the code, the detection effect of the null pointer dereference defect can be effectively improved.

[0009] In a possible implementation, the first null knowledge base includes identification information of a plurality of code elements and null annotation information of each of the plurality of code elements;

[0010] According to the first null knowledge base, a first annotation of a null type is added at a first code position in the first code, including:

[0011] For any code element in the first code, if identification information of the code element exists in the first null knowledge base, null annotation information of the code element is obtained from the first null knowledge base;

[0012] According to the null annotation information of the code element, a first annotation is added at a first position in the first code, where the first position is a code position in the first code corresponding to the code element, and the first annotation is consistent with the null annotation information of the code element.

[0013] In this way, based on the null annotation information corresponding to the historical fault reflected by the first null knowledge base, a corresponding null annotation can be added in the first code, so as to ensure the reliability and correctness of the added null annotation.

[0014] In a possible implementation, the second null knowledge base includes null annotation information of each of a plurality of functions;

[0015] According to the second null knowledge base, a first annotation of a null type is added at a first code position in the first code, including:

[0016] For any code element in the first code, if the code element is assigned a return value of the first function or the code element is passed the return value of the first function, if the second null knowledge base records null annotation information of the return value of the first function, a first annotation is added at a first position in the first code;

[0017] The first position is a code position in the first code corresponding to the code element, and the first annotation is consistent with the null annotation information of the first function.

[0018] In this way, based on the null annotation information of the source function corresponding to the first programming language reflected by the second null knowledge base, a corresponding null annotation can be added in the first code, so as to ensure the reliability and correctness of the added null annotation.

[0019] In a possible implementation, the method further includes:

[0020] In the first log, a plurality of record information of the first type of fault is found, and the first type of fault is a fault caused by null pointer dereferencing;

[0021] In any one of the plurality of record information, extract the identification information of the code element contained in the record information, and determine the null annotation information of the code element;

[0022] The identification information of the code element contained in the record information and the null annotation information of the code element are data in the first null knowledge base.

[0023] In this implementation, the historical null knowledge base (i.e., the first null knowledge base) can be generated based on historical failures caused by null pointer dereferencing, so that the reliability and accuracy of the null annotation information reflected by the historical null knowledge base can be effectively guaranteed.

[0024] In a possible implementation, the method further includes:

[0025] The standard library source code of the first programming language is searched for an annotation node, and a code segment of the annotation node is used to indicate the annotation information declared for a program node corresponding to the annotation node;

[0026] When the program node corresponding to the annotation node is of a function definition type, it is determined whether the null annotation information of the program node exists in the code segment of the annotation node;

[0027] If the null annotation information exists, the null annotation information of the return value of the function corresponding to the program node is recorded in the second null knowledge base.

[0028] In this way, the source function null knowledge base corresponding to the source function of the first programming language can be constructed based on the null annotation information corresponding to the standard library source code of the first programming language, so that the comprehensiveness of the null annotation information reflected by the knowledge base can be effectively improved.

[0029] In a possible implementation, the method further includes:

[0030] For any one code element in the first code, if the writing mode of the code line contained in the code element conforms to the first mode, a first annotation of a null value type is added to a code position corresponding to the code element in the first code;

[0031] The first mode includes at least one of the following: assigning a null value to a variable, and comparing a variable with a null value.

[0032] In this way, the code mode related to the null value is identified, and the null value annotation is added at a specific position, so that more comprehensive null value annotation processing can be implemented.

[0033] In a possible implementation, the adding of the second annotation of the null value type to the second position in the second code according to the code structure information corresponding to the second code includes:

[0034] determining a first code element corresponding to the first annotation in the second code according to the first annotation;

[0035] finding a second code element having a dependency relationship with the first code element in the second code according to the code structure information corresponding to the second code;

[0036] adding a second annotation of a null type at a second position in the second code, the second position being a code position corresponding to the second code element in the second code, and the second annotation being identical to the first annotation.

[0037] In this way, the propagation of the null annotation is performed according to the dependency relationship between the code elements reflected by the code structure information, so that the propagation of the null annotation of the static program analysis technology which is cross-module, cross-method, path and context sensitive can be implemented, and the generated annotations can be iteratively propagated, and the range of the annotations can be further expanded.

[0038] In a possible implementation, the code structure information includes at least one of the following: an abstract syntax tree (AST), a control flow graph (CFG), a call graph (CG) and a data flow graph (DFG).

[0039] In a possible implementation, the code element includes at least one of the following types: a class member variable, a function return value and a function parameter.

[0040] The code position corresponding to the code element is a definition position of the code element.

[0041] In a second aspect, an embodiment of the present application provides a code annotation processing apparatus. The code annotation processing apparatus can be an electronic device, or a chip or a chip system in the electronic device. The code annotation processing apparatus can include a display unit and a processing unit.

[0042] When the code annotation processing apparatus is an electronic device, the display unit can be a display screen. The display unit is configured to perform the step of displaying, so that the electronic device implements the code annotation processing method in the first aspect or any possible implementation manner of the first aspect.

[0043] When the code annotation processing apparatus is an electronic device, the processing unit can be a processor. The code annotation processing apparatus can further include a storage unit, which can be a memory. The storage unit is configured to store instructions, and the processing unit is configured to execute the instructions stored in the storage unit, so that the electronic device implements the code annotation processing method in the first aspect or any possible implementation manner of the first aspect.

[0044] When the code annotation processing apparatus is a chip or a chip system in an electronic device, the processing unit can be a processor. The processing unit executes instructions stored in the storage unit, so that the electronic device implements a code annotation processing method described in the first aspect or any possible implementation manner of the first aspect. The storage unit can be a storage unit (for example, a register, a cache, etc.) in the chip, or a storage unit (for example, a read-only memory, a random access memory, etc.) in the electronic device and located outside the chip.

[0045] In a third aspect, an embodiment of the present application provides an electronic device, including a processor and a memory. The memory is configured to store code instructions. The processor is configured to execute the code instructions to perform the method described in the first aspect or any possible implementation manner of the first aspect.

[0046] In a fourth aspect, an embodiment of the present application provides a computer readable storage medium, and the computer readable storage medium stores a computer program or instructions. When the computer program or instructions are executed on a computer, the computer is caused to perform the method described in the first aspect or any possible implementation manner of the first aspect.

[0047] In a fifth aspect, an embodiment of the present application provides a computer program product including a computer program. When the computer program is executed on a computer, the computer is caused to perform the method described in the first aspect or any possible implementation manner of the first aspect.

[0048] In a sixth aspect, the present application provides a chip or a chip system. The chip or the chip system includes at least one processor and a communication interface. The communication interface and the at least one processor are interconnected through a line. The at least one processor is configured to execute a computer program or instructions to perform the method described in the first aspect or any possible implementation manner of the first aspect. The communication interface in the chip can be an input / output interface, a pin or a circuit, etc.

[0049] In a possible implementation, the chip or the chip system described in the present application further includes at least one memory. The at least one memory stores instructions. The memory can be a storage unit (for example, a register, a cache, etc.) in the chip, or a storage unit (for example, a read-only memory, a random access memory, etc.) of the chip.

[0050] It should be understood that the second aspect to the sixth aspect of the present application correspond to the technical solution of the first aspect of the present application. The beneficial effects obtained by each aspect and the corresponding possible implementation manner are similar, and will not be described again. BRIEF DESCRIPTION OF DRAWINGS

[0051] Figure 1 A flowchart of a code annotation method provided by an embodiment of the present application is shown.

[0052] Figure 2 A construction diagram of a history null knowledge base provided for an embodiment of the present application is shown in FIG. 1.

[0053] Figure 3 A construction diagram of a source function null knowledge base provided for an embodiment of the present application is shown in FIG. 2.

[0054] Figure 4 An analysis diagram of code structure information provided for an embodiment of the present application is shown in FIG. 3.

[0055] Figure 5 A diagram of a dependency relationship of a code element provided for an embodiment of the present application is shown in FIG. 4.

[0056] Figure 6 A hardware structure diagram of an electronic device provided for an embodiment of the present application is shown in FIG. 5. DETAILED DESCRIPTION

[0057] In order to clearly describe the technical solutions of the embodiments of the present application, the following briefly introduces some terms and technologies involved in the embodiments of the present application:

[0058] 1. Pointer reference defect

[0059] Pointer reference defect (Null Pointer Dereference, NPE) is also known as null pointer dereference, which is a common memory access related code defect. When a pointer expects to access a reference object, but the object is null (Null), a null pointer exception problem is triggered, which often leads to program crash and causes serious backend stability problems.

[0060] In simple terms, when a program tries to call a method, access a field, or perform other operations through a reference that does not point to an actual object, a null pointer exception problem is triggered.

[0061] 2. Function parameters

[0062] Function parameters are input values passed to a function or method. These parameters are passed when the function is called, and are used for calculations or operations within the function.

[0063] The relationship with null pointer is that if the parameter passed to the function is null, and the function does not perform appropriate null check, and directly dereferences the parameter (such as calling a method or accessing a property), a null pointer exception will be caused.

[0064] 3. Function return value

[0065] Return value is the result returned after the execution of a function or method, which can be a basic type value, an object reference, or null.

[0066] The relationship with null pointer is that if a function returns null and the caller does not check for null before dereferencing the return value, a null pointer exception will be thrown.

[0067] 4、Class member variable

[0068] Class member variable is a property that belongs to a class and is stored in an instance of the class. They can be accessed and modified throughout the lifetime of the class.

[0069] The relationship with null pointer is that if a class member variable is not properly initialized or is assigned null at some point, dereferencing the variable in subsequent code may cause a null pointer exception.

[0070] 5、Nullability annotations

[0071] Nullability annotations can be used in code to explicitly indicate whether certain methods, parameters, or return values are allowed to be null. These annotations help improve code readability and reduce the risk of null pointer exceptions (NullPointerException). Common nullability annotations can include Nullable and NonNull.

[0072] Nullable is used to indicate that an element (such as a parameter, function return value, field, etc.) can be null. The caller or user needs to handle the null case. For example, when Nullable is annotated for a function parameter, it means that the parameter passed to the method can be null. For example, when Nullable is annotated for a function return value, it means that the method may return null. For example, when Nullable is annotated for a class member variable, it means that the member variable can be null.

[0073] NonNull is used to indicate that an element should not be null. Using this annotation indicates that passing or returning null in this position is illegal, and violating this agreement may cause a runtime exception or other undesirable behavior.

[0074] For example, when NonNull is annotated for a function parameter, it means that the parameter passed to the method cannot be null. For example, when NonNull is annotated for a function return value, it means that the method cannot return null. For example, when NonNull is annotated for a class member variable, it means that the member variable cannot be null.

[0075] 6、Abstract Syntax Tree (AST)

[0076] An Abstract Syntax Tree (AST) is a tree-like data structure used to represent the structure of program source code. An AST is an abstract representation of program code that removes certain syntactic details (such as parentheses and semicolons) while preserving the semantic structure of the code.

[0077] It can be used to help compilers or other tools understand code structure, such as expressions, statement blocks, and functions. Nodes in an AST typically represent various elements of a programming language, such as variable declarations, function calls, and conditional statements.

[0078] 7. Call Graph (CG)

[0079] A call graph is a graph used to represent the call relationships between functions or methods, where nodes represent functions and edges represent function calls. It can be used to help analyze the call hierarchy of a program, identifying circular calls, unused functions (functions that are not called), etc.

[0080] Call graphs can include static call graphs and dynamic call graphs. Static call graphs can be analyzed based on the structure of the source code, while dynamic call graphs can be analyzed based on the function calls made during program execution.

[0081] 8. Control Flow Graph (CFG)

[0082] A control flow graph is a graph that represents the execution flow in a program. Nodes represent basic blocks (indivisible code segments), and edges represent control flow (conditional statements, loops, etc.).

[0083] It can be used to analyze the execution path of a program and understand control structures such as conditional statements and loops. It's important to understand that statements within a basic block are executed sequentially without branching or jumping. A control flow graph shows the possible execution paths of a program.

[0084] 9. Data Flow Graph (DFG)

[0085] A data flow graph represents the dependencies between data in a program. Nodes represent operations or computations, and edges represent data dependencies (data flowing from one node to another). It can be used to analyze the transit of variables or data and identify which values ​​affect subsequent calculations.

[0086] 10. Full Path

[0087] The full path of a code element can be understood as the unique identifier of that code element within the entire project code.

[0088] For example, a function full path refers to the unique identification of a function in the entire project code, which can include the module name (i.e. the file name), the package name (if any), and the function name.

[0089] A class full path refers to the unique identification of a class in the entire project code. It can include the module name (i.e. the file name), the package name (if any), and the class name.

[0090] A parameter type full path refers to the full path of the type of a function or method parameter. It explicitly indicates the module or package where the type of the parameter is located in the entire project code, ensuring that the correct type can be found when the function is called.

[0091] A return value type full path refers to the full path of the type of a function or method return value. Similar to the parameter type, it is used to explicitly indicate which module or package in the entire project code the return type comes from.

[0092] A member variable type full path refers to the full path of the type of a class member variable (also known as an attribute). This is used in class definitions to explicitly indicate that the class member variable is a type from a module or package in the entire project code.

[0093] Here, the difference between path and full path is emphasized. The path can be relative, that is, it depends on the current file location. When importing modules or referring to functions or classes in other modules, using paths can simplify the reference according to the hierarchy of the current file.

[0094] The full path is absolute, it always starts from the root of the project or the root of the namespace, and points to a specific module, class, function or variable. No matter which file is referenced, the complete package name, module name and function / class name must be written out, so that the corresponding code element can be uniquely identified.

[0095] 11. Other terms

[0096] In the embodiments of the present application, "first", "second", and the like are used to distinguish the same or similar items with basically the same function and role. For example, the first chip and the second chip are only used to distinguish different chips, and do not limit the order. Those skilled in the art can understand that "first", "second", and the like do not limit the quantity and execution order, and "first", "second", and the like do not necessarily mean different.

[0097] It is to be noted that in the embodiments of the present application, the words "exemplary" or "for example" are used to mean serving as an example, instance, or illustration. Any embodiment or design described herein as "exemplary" or "for example" should not be construed as being preferred or advantageous over other embodiments or designs. Rather, the embodied words are used to present concepts in a concrete manner.

[0098] In the embodiments of the present application, "at least one" means one or more, and "multiple" means two or two more. The association relationship of the associated objects is described, which means that there can be three relationships, for example, A and / or B, which can represent the following three cases: A exists alone, A and B exist together, and B exists alone, wherein A and B can be singular or plural. The character " / " generally represents an "or" relationship between the associated objects before and after it. "At least one of the following" or similar expressions means any combination of these items, including any combination of single item or multiple items. For example, at least one of a, b, or c can represent a, b, c, a-b, a-c, b-c, or a-b-c, wherein a, b, and c can be single or multiple.

[0099] In order to better understand the technical solutions of the present application, the related technologies involved in the present application are further described in detail.

[0100] Pointer reference defect is a programming error commonly found in object-oriented programming languages, especially in languages such as Java, C++, C#, etc. It occurs when a program tries to reference an object or variable of a null pointer, because the null pointer does not point to a valid object or memory location.

[0101] Among them, the pointer reference defect usually involves three code types of function parameters, function return values and class member variables. The following will illustrate the scenarios of null pointer dereferencing corresponding to the three code types with several specific examples.

[0102] Example 1: Null pointer dereferencing corresponding to function parameters

[0103] Suppose there is a piece of code as follows:

[0104] public void processString(String str) { / / processString is the function name, and str is the function parameter of the string type

[0105] int length = str.length(); / / get the length of the string str and assign it to the variable length

[0106] }

[0107] In the above code, processString is a function that takes str as an argument. When this function is called, if the argument str is null, a null pointer exception is raised and the corresponding line of code throws a pointer reference defect.

[0108] Example Two, Null Pointer Dereference Corresponding to Function Return Value

[0109] private void initPackageName(){

[0110] packageName = getPackageName(KEY); / / Call the getPackageName() method to get the package name using the constant KEY

[0111] if (!packageName.equals("Name")){

[0112] return; / / If the obtained package name is not equal to "Name", directly return and terminate method execution

[0113] }

[0114] packageName = packageName + " / Name2"; / / Otherwise, update the value of packageName to "Name / Name2"

[0115] }

[0116] private string getPackageName(int key){ / / Return the corresponding package name according to the given key

[0117] if (key >= 1){

[0118] return "Name"; / / If key is greater than or equal to 1, return the package name as "Name"

[0119] }

[0120] return null; / / Otherwise, return null, indicating that there is no matching package name

[0121] }

[0122] In the code above, the package name is retrieved by calling the external function `getPackageName`. Referring to the code, we can confirm that the return value of `getPackageName` may or may not be null. If the developer directly assigns the return value of this external function to the variable `packageName` (i.e., the line `packageName = getPackageName(KEY)`) without checking for null values, assuming that `packageName` is not null, it could lead to a null pointer dereference.

[0123] For example, suppose you directly call the `packageName.equals()` method on the variable `packageName`. If the variable `packageName` is null, a pointer reference defect will be triggered, causing the program to crash.

[0124] Example 3: Dereferencing null pointers corresponding to class member variables

[0125] private Boolean mBoolean0bj; / / Class member variable, storing a boolean value

[0126] private void setBoolean0bj(Boolean mBoolean0bjArg){ / / setBoolean0bj is the function name, mBoolean0bjArg is the boolean function parameter.

[0127] mBooleanobj = mBoolean0bjArg; / / Assign the parameter value to the member variable mBooleanObj

[0128] }

[0129] private void setPackageName(string name) { / / Sets the value of packageName, where name is the package name to be set.

[0130] / / Setting packageName is only allowed when mBooleanObj is true.

[0131] if(mBooleanobj{

[0132] packageName = name; / / Assign the parameter name to packageName

[0133] }

[0134] }

[0135] In the above code, the judgment of whether the class member variable of the encapsulated type is empty or not is involved. In the first line of the code, a Boolean variable mBooleanObj of the encapsulated type of the basic type bool is declared, and it is not initialized.

[0136] However, unlike the default value of the basic type bool which is false, the default value of Boolean is null. Although mBooleanObj is assigned in the setBooleanObj function in the third line of the code, since the setBooleanObj function is not a function constructor, it is not necessarily executed in all function execution paths, and therefore, mBooleanObj still has the possibility of being empty. Here, the developer does not consider the empty case and defaults to believe that mBooleanObj is not empty and performs the unpacking operation, and therefore, when mBooleanObj is null, the pointer reference defect is triggered, causing the program to interrupt.

[0137] As can be understood from the above introduction, the code defect of the null pointer exception is involved in the three types of code of function parameters, function return values and class member variables, and it can be understood that the scenario of the pointer reference defect is not limited to the three types of code introduced above. In fact, the problem of the pointer reference defect may occur in various types of code segments.

[0138] The cause of the pointer reference defect is relatively complex. The most common scenario is that the developer does not fully consider the complex situation that the current used object may be empty when writing the code, thereby causing the problem of the pointer reference defect in the code running process.

[0139] Currently, the static code defect detection tool can be used to scan the null pointer reference defect. The static code checking tool can detect, intercept and repair the code defects. For example, during the software development stage, the detection tool can be used to detect and front-end intercept the code submitted to the library, and feedback the hidden defect code to the developer for modification, thereby helping to improve the code quality. The detection report fed back to the developer can be used to report the objects, positions and actions that may be empty and trigger the null pointer reference problem. The code checking scenarios can include local script running check / IDE (Integrated Development Environment) plug-in, online CI (Continuous Integration) access interception / CI pipeline check, etc.

[0140] It can be understood that the static code defect detection tool can intercept defects in the front end of the code as much as possible to avoid triggering a null pointer exception in the back end execution. Then, the scanning results of the existing tools and schemes often face a large number of false positives and false negatives, mainly due to three technical challenges:

[0141] 1. The code defect mode of the null pointer exception is very complex, and the function parameters, function return values, and class member variables involve semantic logic, and it is difficult to judge null, for example: external API(Application Programming Interface, application programming interface) interface return value judgment null, incoming parameter judgment null, external variable (member variable) judgment null, multi-threading / global / shared variable judgment null and other cases.

[0142] 2. At present, most static code defect detection tools usually only use conservative strategies to ensure accuracy, resulting in a large number of false negatives, for example: some tools cover many scenarios, but because they will only report completely trusted problems, they will miss some effective error reports. For example, when the pointer variable comes from the function return value, whether the function return value is NULL depends on the user input, and some tools cannot determine whether it will be NULL in static analysis, in order to ensure accuracy, the pointer error will be missed. For example, some tools do not check the initialization of member variables, and do not use previously checked NULL information transmission for inference.

[0143] 3. Most static code defect detection tools rely on annotations for effective inspection, but many self-developed codes do not have annotations, and are limited by the lack of annotations, and cannot actually play a role in code inspection.

[0144] In view of the technical problems introduced above, the present application proposes a technical concept: in the software development process, due to the large and complex project code library, the developer may neglect to add necessary annotations, so in the technical solution of the present application, null value annotations can be added to the code or the null value annotations can be improved, and then code defect detection is performed based on the code containing relatively complete null value annotations, so that the detection effect of the null pointer reference defect can be effectively improved, for example, the coverage and success rate of detection can be improved.

[0145] The technical solutions of the embodiments of the present application and how the technical solutions of the embodiments of the present application solve the above technical problems will be described in detail below with specific embodiments in conjunction with the drawings. The following specific embodiments can be implemented independently, or can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.

[0146] The code annotation processing method of the embodiments of the present application can be executed by an electronic device provided with a processor, wherein the electronic device can be a server or the like. Alternatively, the code annotation processing method can also be executed by a chip, a chip system or a processor supporting the electronic device, or a logic module or software capable of implementing all or part of the functions of the electronic device, and the present application does not make a specific limitation in this regard. The code annotation processing method of the embodiments of the present application will be described in detail below taking the electronic device as an example.

[0147] Figure 1 The flowchart of the code annotation method provided by the embodiments of the present application is shown in FIG. 1. Referring to FIG. 1, the technical solution of the present application can be divided into two stages: Figure 1

[0148] The first stage is null annotation generation.

[0149] Based on the pre-constructed external knowledge base, the null position in the target source code (which can be referred to as the first code) is located, and then the corresponding null annotation is added in the first code based on the located null position, so as to obtain the second code with the added null annotation. The null annotation may, for example, include @Nullable (indicating that it can be null) and @NonNull (indicating that it cannot be null).

[0150] The second stage is null annotation propagation.

[0151] Based on the static program analysis technology, the null annotation in the second code is taken as a source point, and the path-sensitive data flow dependency constraint analysis is performed across modules and methods, so as to propagate and enhance the null annotation within the second code according to the dependency relationship in the code.

[0152] After the propagation and enhancement of the null annotation for the second code, the source code with the added null annotation can be understood, and then based on the source code with the added null annotation, a static null pointer reference defect detection tool based on annotation analysis can be used for detection, so as to prompt the abnormal position that can cause the pointer application defect.

[0153] That is, the technical solution of the present application can automatically generate and / or propagate the null annotation (such as @Nullable@NonNull) for the source code with part of the annotations or without any annotations, so as to generate the source code with the null annotation, and then take the source code with the null annotation as an intermediate input and implement the null pointer reference defect detection based on the annotation enhancement. The detection of the null pointer reference defect can be implemented in a similar manner as in the related art, for example, by using a corresponding tool, and the present embodiment does not make a detailed introduction in this regard. The focus of the present application is to add the null annotation in the source code, so as to improve the detection effect of the subsequent null pointer reference defect.

[0154] ​The two stages described above will be described in detail below. Based on the above description, it can be determined that in the process of generating the null annotation in the first stage, the pre-constructed external knowledge base needs to be relied on to locate the null position in the target source code.

[0155] Firstly, the following will be described in combination with Figures 2-3 The related implementation of constructing the knowledge base will be described. Figure 2 The construction schematic diagram of the historical null knowledge base provided by the embodiment of the present application is shown in the following figure, Figure 3 The construction schematic diagram of the source function null knowledge base provided by the embodiment of the present application is shown in the following figure.

[0156] In the present application, the external knowledge base can be understood as a knowledge base related to null. In one implementation, the external knowledge base in the present embodiment may, for example, include a historical null knowledge base and a source function null knowledge base of a first programming language.

[0157] The historical null knowledge base may, for example, be a null knowledge base generated according to a first log, wherein the first log is a log record used to record historical faults in the development process of a project, and the historical null knowledge base can also be referred to as a first null knowledge base. For example, in the historical null knowledge base, custom function parameters, function return values, class member variables, and the like in the project may be included.

[0158] The following will be described in combination with Figure 2 The construction of the historical null knowledge base will be described.

[0159] In the present embodiment, the first log is a log used to record historical faults in the development process of a project, so that a plurality of record information of faults can be included in the first log, wherein the fault types can be various. Corresponding to the technical solution in the present application, for example, the record information of a first type of fault can be searched from the plurality of record information included in the first log, wherein the first type of fault may, for example, be a fault caused by null pointer dereferencing. That is, in the present embodiment, the record information of the fault caused by null pointer dereferencing can be searched from the plurality of record information of the first log first, and then the historical null knowledge base can be generated based on the record information of the fault caused by null pointer dereferencing.

[0160] The following will be described in combination with Figure 2 In one example, it is assumed that the parameter of function a is null, which causes the code to throw a fault of pointer reference defect, so that the record information 1 shown in the following figure can be recorded in the first log accordingly. Figure 2 The record information 1 is used to indicate that the parameter (for example, the parameter is str1) of function a is null, which causes the fault. The record information 1 is also the record information of the first type of fault described above.

[0161] For example, if the return value of function b is null, causing the code to throw a fault of pointer reference defect, the corresponding record information 2 shown in Figure 2 may be recorded in the first log. The record information 2 is used to indicate that the return value of function b is null, causing the fault. The record information 2 is also the record information of the first type of fault as described above.

[0162] The embodiment does not limit the specific form and specific content of the record information contained in the first log. In general, the first log can contain record information of a fault caused by null pointer dereference. Then, the record information of the fault caused by null pointer dereference can be used for corresponding data extraction to generate a historical null knowledge base.

[0163] In one implementation, in the process of corresponding data extraction according to the record information of the fault caused by null pointer dereference, the identification information of the code element contained in the record information can be extracted, and the null annotation information of the code element can be determined accordingly.

[0164] The code element may, for example, include at least one of the following types: function parameter, function return value, and class member variable. For details, refer to Figure 2 The null annotation information of the code element is used to indicate that the code element can be null (Nullable) or cannot be null (NonNull).

[0165] The implementations of different types of code elements corresponding to each other will be introduced below. Figure 2

[0166] For details, refer to Figure 2 The identification information of the function parameter may, for example, include one or more of the following contents: fault ID, function full path, parameter name, and parameter type full path. The fault ID is the fault identification corresponding to the record information for which the data extraction is currently performed. Each fault has its own corresponding ID. For details of the function full path, parameter name, and parameter type full path, refer to the explanation and description of the above embodiment, which will not be repeated here.

[0167] It can be understood that the fault ID, function full path, parameter name, and parameter type full path can uniquely identify a function parameter, so that the identification of a specific function parameter can be effectively implemented in this way.

[0168] For details, refer to Figure 2 ​The identification information of the function return value may include one or more of the following, for example: fault ID, function full path, and return value type full path. Similar to the above, a function return value can be uniquely identified based on these, and thus the identification of a specific function return value can be effectively implemented in this way.

[0169] and refer to Figure 2 The identification information of the class member variable may include one or more of the following, for example: fault ID, class full path, member variable name, and member variable type full path. Similar to the above, a class member variable can be uniquely identified based on these, and thus the identification of a specific class member variable can be effectively implemented in this way.

[0170] Here, refer to Figure 2 The above is only one possible implementation of the identification information of the function parameter, function return value, and class member variable. In actual implementation, as long as the identification information can be used to uniquely indicate a specific function parameter, function return value, or class member variable, the specific implementation of the identification information can be arbitrarily set according to actual needs, and the present embodiment does not limit this.

[0171] Referring to the example of Figure 2 , it can be introduced that after extracting the identification information of the code element contained in the record information and determining the null annotation information of the code element, the identification information and the null annotation information of the code element can be used as a piece of data in the historical null knowledge base. Then, the corresponding data extraction is performed on each piece of first-type record information, and thus the historical null knowledge base shown in Figure 3 is formed.

[0172] Further, in order to improve the correctness of the data recorded in the historical null knowledge base, two nodes can be set to independently perform the above-described data extraction process, and then the results extracted by the two nodes are further screened. For example, only when the extraction results of the two are the same, the extraction result is collected into the historical null knowledge base; otherwise, the extraction result is skipped. Further, the Kappa value can be used as an index for evaluating the consistency test of the two, for example, when the total Kappa value is greater than or equal to 0.95, it proves that the consistency of the two is very good, and thus it can be determined that the data recorded in the historical null knowledge base is relatively reliable.

[0173] The node may be a device, or a program, a thread, a process, etc., as long as the node can be used to perform corresponding data processing, and the present embodiment does not limit this.

[0174] In summary, in the technical solution of the application, function parameters, function return values and class member variables that have caused null pointer exceptions in the project history can be extracted in advance to form a historical null knowledge base of the project.

[0175] Secondly, in combination with Figure 3 The construction of the source function null knowledge base of the first programming language is described.

[0176] The first programming language is the programming language used to write the target source code, such as Java or C++, and the source function null knowledge base of the first programming language can be extracted from the standard library source code of the first programming language, such as the Java jdk source code. The source function file corresponding to the standard library source code defines the most basic classes, functions and data structures in the programming language, and is the basis for the operation of the programming language.

[0177] For example, taking Java as the first programming language, the corresponding standard library source code can be Java jdk source code. The source function null knowledge base can be generated based on the source function parameters and return values defined in the Java jdk source code.

[0178] For example, taking C++ as the first programming language, a series of basic data structures and algorithm implementations are defined in the standard library source code of C++, and the source function null knowledge base can be generated based on the source function parameters and return values defined in the standard library source code of C++.

[0179] In this embodiment, the source function null knowledge base can also be referred to as a second null knowledge base. The implementation of constructing the source function null knowledge base is described below. Figure 3 The implementation of constructing the source function null knowledge base is described.

[0180] As Figure 4 shown, the implementation of constructing the source function null knowledge base can include the following steps:

[0181] 1. Obtain a set of definition files of the standard library source code of the first programming language.

[0182] It can be understood that the standard library source code of the first programming language can be distributed in multiple definition files, so a set of definition files of the standard library source code of the first programming language can be obtained. Taking Java as the first programming language, for example, a set of definition files of the Java jdk source code can be obtained, and the set of definition files of the Java jdk source code can include file 1, file 2 and file 3, and the like. The specific division and definition of the set of definition files of the standard library source code are not limited in this embodiment, and can be set arbitrarily according to actual needs.

[0183] 2. Static program analysis is performed on each file in the defined file set to obtain a plurality of nodes, and then the plurality of nodes are sequentially traversed.

[0184] In this embodiment, similar processing can be performed on each file in the defined file set, and thus any one file is taken as an example for description below.

[0185] The code contained in the single file is specific code content, which can be used to define a corresponding source function, for example. Static program analysis can then be performed on the code in the file to implement modeling processing of the source function of the first programming language. For example, the corresponding modeling processing can be implemented by extracting the AST corresponding to the code to extract a plurality of nodes. The embodiment does not limit the manner of extracting the nodes, which can be arbitrarily extended according to actual needs, as long as the modeling result can contain a plurality of nodes and can reflect the relationship between the nodes. The nodes are used to represent various elements of the programming language.

[0186] The plurality of nodes can then be traversed to perform corresponding processing on each node.

[0187] 3. It is determined whether the node currently traversed is an annotation node.

[0188] It can be understood that, before the code segment of the function definition, there is usually annotation information that declares the relevant features of the function, and the annotation information is abstracted as an annotation node. The null annotation information of the parameters or return value of the function is usually contained in the code line of the annotation node.

[0189] Therefore, in this embodiment, each annotation node can be found first, for example, it can be determined whether the node currently traversed is an annotation node in the process of traversing the nodes. If it is determined that the node is an annotation node, subsequent processing is performed, and if it is determined that the node is not an annotation node, the next node is continuously traversed.

[0190] 4. The program node corresponding to the annotation node is obtained.

[0191] It can be understood that the annotation node usually has a corresponding program node, and the code segment corresponding to the program node is a code segment that is not of the annotation nature but has actual functions. The code segment of the annotation node is used to indicate the annotation information declared for the corresponding program node.

[0192] A specific example is used for description here, assuming that there is a code segment as follows:

[0193]

[0194] In the above code segment, the isNullOrEmpty function is used to check whether the input string is null or empty. The function parameter is input, which is the input string of the function. The function returns true if the string is null or empty, and false otherwise.

[0195] The function definition is preceded by a comment that describes the input parameter. The @param input: description of the input parameter explains its purpose, which is "String to check for null or empty."

[0196] The @return: description of the return value explains the conditions under which the function returns true and false.

[0197] In the example above, the code segment between the / ** and * / symbols can be abstracted as an annotation node, and the code segment defining the isNullOrEmpty function can be abstracted as a program node. The annotation node and program node are related, meaning that the code segment of the annotation node is used to declare annotation information for the code segment of the program node.

[0198] 5. Determine whether the current program node is a function definition type.

[0199] It should be understood that annotation information can be written for various types of program nodes. However, in this embodiment, the function parameter and function return value null annotation information is currently being extracted, which is usually in the annotation information of the function definition type program node. Therefore, after obtaining the annotation node corresponding to the program node, it can be further determined whether the current program node is a function definition type.

[0200] If the current program node is a function definition type, continue to execute subsequent processing for the program node. If the current program node is not a function definition type, return to step 2 and continue to traverse the next node.

[0201] 6. In the code line of the annotation node corresponding to the program node, find the null annotation information corresponding to the program node.

[0202] It can be determined with reference to the above examples that in the case where the program node is a function definition type, the parameter of the function and whether the return value can be null or not and the like are usually declared in the code line of the annotation node corresponding to the program node. Therefore, the null annotation information corresponding to the program node can be directly found in the code line of the annotation node corresponding to the program node. Exemplarily, the null annotation information corresponding to the program node may, for example, contain the null annotation information of the function parameter and / or the function return value of the program node.

[0203] Similar to the embodiments described above, the null annotation information can be used to indicate that the function parameter and / or the function return value can be nullable or non-nullable.

[0204] Then, according to the null annotation information of the plurality of function parameters and / or the function return values, the source function null knowledge base introduced in the embodiment can be formed. That is, in the source function null knowledge base, the null annotation information of each of the plurality of function parameters and / or the plurality of function return values can be contained.

[0205] It can be understood that the class member variable is not modeled in the current embodiment because the construction of the source function null knowledge base only focuses on the scenario of the null pointer exception problem caused by calling the source function of the first programming language.

[0206] After the historical null knowledge base and the source function null knowledge base are constructed, the first code (i.e., the target source code) can be statically analyzed based on the two knowledge bases, so as to add the null type annotation in the first code. The implementation of adding the null annotation in the first code will be described below in combination with specific embodiments. It may, for example, contain the logic of the following three parts:

[0207] The first part is to add the null annotation according to the historical null knowledge base.

[0208] It can be understood that the first code includes a plurality of code elements, and the code element can be a function parameter, a function return value or a class member variable and the like. Then for any code element in the first code, the identification information of the code element may, for example, be first searched in the historical null knowledge base.

[0209] In the case where the identification information of the code element is contained in the historical null knowledge base, it can be determined that the code element has ever appeared the null pointer dereference fault in history, and therefore the null annotation information of the code element can be obtained from the first null knowledge base.

[0210] Then, according to the nullability annotation information of the code element, a first annotation is added at a first position in the first code, where the first position is a code position corresponding to the code element in the first code, and the first annotation is consistent with the nullability annotation information of the code element.

[0211] For example, for a code element of a function parameter type, a corresponding first annotation can be added at a definition of the function parameter. For another example, for a code element of a function return value type, a corresponding first annotation can be added at a definition of the function return value. For another example, for a code element of a class member variable type, a corresponding first annotation can be added at a definition of the class member variable. The adding position of the first annotation can be referred to a position of a conventional nullability annotation, and the embodiment does not limit the specific position of the first annotation, as long as it can be used to indicate the nullability of the corresponding code element.

[0212] If the nullability annotation information is used to indicate that the code element can be null, the first annotation can be the above-mentioned Nullable. If the nullability annotation information is used to indicate that the code element cannot be null, the first annotation can be the above-mentioned NonNull.

[0213] The implementation of the three types of code elements, i.e., function parameters, function return values and class member variables, will be described below.

[0214] 1.1 for function parameters

[0215] For example, the function parameter a1 is taken as an example, and it is assumed that the historical nullability knowledge base contains the identification information of the function parameter a1. Then, the nullability annotation information of the function parameter a1 can be obtained from the historical nullability knowledge base. According to the nullability annotation information, a first annotation is added at the definition of the function parameter a1 in the first code, where the first annotation can be Nullable or NonNull, or other forms, as long as it can be used to indicate whether the function parameter a1 can be null.

[0216] 1.2 for function return values

[0217] Herein, the function return value b1 is taken as an example for illustration. It is assumed that the identification information of the function return value b1 is contained in the historical null knowledge base. Then, the null annotation information of the function return value b1 can be acquired from the historical null knowledge base. And according to the null annotation information, the first annotation is added at the definition of the function return value b1 in the first code. The first annotation can be, for example, Nullable or NonNull, or can also be other forms of expression, as long as the first annotation can be used to indicate whether the function return value b1 can be null.

[0218] 1.3 For class member variables

[0219] Herein, the class member variable c1 is taken as an example for illustration. It is assumed that the identification information of the class member variable c1 is contained in the historical null knowledge base. Then, the null annotation information of the class member variable c1 can be acquired from the historical null knowledge base. And according to the null annotation information, the first annotation is added at the definition of the class member variable c1 in the first code. The first annotation can be, for example, Nullable or NonNull, or can also be other forms of expression, as long as the first annotation can be used to indicate whether the class member variable c1 can be null.

[0220] In summary, the technical scheme of the present application can effectively improve the comprehensive coverage of null annotations by constructing a historical null knowledge base, and then adding null annotations at the definitions of the partial code elements in the first code that have occurred historical null faults according to the historical null knowledge base.

[0221] Second part, adding null annotations according to the source function null knowledge base.

[0222] Similarly, the first code can include multiple code elements, which can be function parameters, function return values, or class member variables, etc. And the source function null knowledge base records null annotation information of multiple function parameters and null annotation information of multiple function return values.

[0223] Then, for any code element in the first code, if the code element is assigned the return value of the first function, or the code element is passed the return value of the first function, and at the same time, the null annotation information of the function return value of the first function is recorded in the source function null knowledge base, it means that the code element can be assigned or passed null, or can be assigned or passed a value that is not allowed to be null. Therefore, the first annotation can be added at the first position in the first code.

[0224] Similar to the above introduction, the code position corresponding to the code element can be, for example, the definition of the code element. And the first annotation is consistent with the null annotation information, which will not be described here.

[0225] The implementation of the three types of code elements, i.e., function parameters, function return values and class member variables, will be described below respectively.

[0226] 2.1 For function parameters

[0227] Take function parameter a2 as an example. Suppose that the null annotation information of the return value of function z recorded in the source function null knowledge base is Nullable or NonNull. Meanwhile, the argument of the return value of function z is passed to function parameter a2 in the downstream calling process, which means that the nullability of function parameter a2 is consistent with that of the return value of function z. Therefore, the first annotation, which can be Nullable or NonNull for example, can be added to the definition of function parameter a2 in the first code, and the first annotation is consistent with the null annotation information of the return value of function z.

[0228] 2.2 For function return values

[0229] Take function return value b2 as an example. Suppose that the null annotation information of the return value of function z recorded in the source function null knowledge base is Nullable or NonNull. Meanwhile, the argument of the return value of function z is assigned to function return value b2 in the definition, which means that the nullability of function return value b2 is consistent with that of the return value of function z. Therefore, the first annotation, which can be Nullable or NonNull for example, can be added to the definition of function return value b2 in the first code, and the first annotation is consistent with the null annotation information of the return value of function z.

[0230] Take function return value b2 as an example. Suppose that the null annotation information of the return value of function z recorded in the source function null knowledge base is Nullable or NonNull. Meanwhile, the argument of the return value of function z is assigned to function return value b2 in the definition, which means that the nullability of function return value b2 is consistent with that of the return value of function z. Therefore, the first annotation, which can be Nullable or NonNull for example, can be added to the definition of function return value b2 in the first code, and the first annotation is consistent with the null annotation information of the return value of function z.

[0231] 2.3 For class member variables

[0232] Here, the class member variable c2 is taken as an example for illustration. It is assumed that the nullity annotation information of the return value of the function z recorded in the source function nullity knowledge base is Nullable or NonNull. Meanwhile, the class member variable c2 is assigned the return value of the function z when defined, which means that the nullability of the class member variable c2 is consistent with the nullability of the return value of the function z. Therefore, the first annotation can be added at the definition of the class member variable c2 in the first code, where the first annotation can be, for example, Nullable or NonNull, which is consistent with the nullity annotation information of the return value of the function z.

[0233] In summary, the technical solution of the present application can construct a source function nullity knowledge base, and then add nullity annotations at the definitions of some code elements in the first code according to the assignment relationship or the passing relationship between the code elements in the first code and the functions contained in the source function nullity knowledge base, thereby effectively improving the comprehensive coverage of the nullity annotations.

[0234] The third part is to add nullity annotations according to code writing modes.

[0235] The above two parts introduce the addition of nullity annotations in the first code according to the knowledge base. In addition, the addition of nullity annotations in the present application can also be performed according to the code writing mode corresponding to each code element.

[0236] For example, for any code element in the first code, a first annotation can be added at the code location corresponding to the code element in the first code if the writing mode of the code line contained in the code element conforms to a first mode. The first mode includes at least one of the following: assigning a variable to a null value, comparing a variable with a null value. The specific implementation of the first mode can also be arbitrarily extended according to actual needs, where the first mode can be understood as a code mode related to a null value. For example, any coding mode that may cause the code element to be null can be understood as the first mode in the present embodiment.

[0237] Here, the first programming language is taken as an example to simply list the code modes related to control in the Java language, as shown in Table 1:

[0238]

[0239] The above-mentioned code patterns related to null can be understood as follows: when a code element corresponds to a code pattern listed in Table 1, a null annotation can be added to the definition of the code element. For example, when a code element corresponds to a code pattern listed in Table 1 and related to the Nullable null annotation, a Nullable null annotation can be added to the definition of the code element.

[0240] In actual implementation, the code patterns related to null can be extended according to actual needs, which are not limited in the embodiments.

[0241] The following describes the implementation of three types of code elements: function parameters, function return values, and class member variables.

[0242] 3.1 For function parameters

[0243] The function parameter a3 is taken as an example. It is assumed that the function parameter a3 is identified as a code pattern that can cause null in the definition of the function (i.e., direct use of the parameter), for example, a null comparison and other code patterns related to null. If the code pattern corresponds to the code pattern that can be null, a first Nullable annotation can be added to the definition of the function parameter a3. If the code pattern corresponds to the code pattern that cannot be null, a first NonNull annotation can be added to the definition of the function parameter a3.

[0244] 3.2 For function return values

[0245] The function return value b3 is taken as an example. It is assumed that the function return value b3 is identified as a code pattern that can cause null in the definition, for example, a direct assignment of null, a null comparison, and other code patterns related to null. If the code pattern corresponds to the code pattern that can be null, a first Nullable annotation can be added to the definition of the function return value b3. If the code pattern corresponds to the code pattern that cannot be null, a first NonNull annotation can be added to the definition of the function return value b3.

[0246] 3.3 For class member variables

[0247] Here, the class member variable c3 is taken as an example for illustration. If the class member variable c3 is identified as a code pattern that may cause a null value in use, for example, a code pattern of directly assigning a null value to the class member variable c3, comparing with a null value, and other null value related code patterns. If it conforms to the code pattern that may be null introduced here, for example, the first annotation of @Nullable can be added at the definition of the class member variable c3; if it conforms to the code pattern that may not be null, for example, the first annotation of @NonNull can be added at the definition of the class member variable c3.

[0248] It can be understood from the introduction of the above embodiments that in the technical solution of the present application, the first annotation of the null value type can be added in the first code based on the pre-constructed historical null value knowledge base and the source function null value knowledge base. Among them, based on the historical null value knowledge base, the code elements that have appeared in history null pointer dereference fault can be effectively annotated with null value, and based on the source function null value knowledge base, the code elements that have a dependency relationship with the source function related to the null value in the first programming language can be effectively annotated with null value, so as to effectively improve the coverage of the null value annotation, and also ensure the correctness and effectiveness of the added null value annotation. Further, the corresponding null value annotation can be added for the code element in the case that the code pattern of the code element conforms to the code pattern related to the null value, which can further improve the coverage of the null value annotation.

[0249] In actual implementation process, the above-mentioned processing can be performed for a plurality of code elements in the first code in turn, so as to add the first annotation of the null value type at a plurality of positions in the first code. Here, the position of adding the null value annotation is further introduced. Based on the above introduction, it can be determined that the code element in the present application can include the following types, for example: function parameter, function return value and class member variable.

[0250] In one implementation, for the code element of the function parameter type, for example, the function definition position corresponding to the function parameter can be found, and further the parameter position corresponding to the function can be found, and then the first annotation of the null value type is added in front of or behind the parameter position. And for the code element of the function return value type, for example, the function definition position corresponding to the function return value can be found, and then the first annotation of the null value type is added in the previous line of the function definition position. And for the code element of the class member variable type, for example, the definition position of the class member variable can be referred to, and then the first annotation of the null value type is added in the previous line of the definition position.

[0251] After adding the first annotation of the null value type in the first code to obtain the second code, for example, the second code can be further verified to ensure that the second code after adding the annotation can be compiled through.

[0252] The above embodiments introduced the first stage of null value annotation processing, namely the implementation of null value annotation generation. The following will introduce the second stage of null value annotation processing, namely the implementation of null value annotation propagation, using specific embodiments.

[0253] In the technical solution of this application, null value annotations existing in the second code can be propagated based on the syntactic and semantic dependencies within the second code. It is understandable that if code element 'a' might be null, a first nullable annotation can be added to code element 'a' in the second code. Then, code element 'b', which depends on code element 'a', might also be null. Therefore, based on this dependency, a first nullable annotation can also be added to code element 'b' in the second code, thus achieving one instance of null value annotation propagation.

[0254] In one implementation, during the propagation of null value annotations, for example, each null value annotation in the second code (including those originally in the first code and those added in the first code) can be located first, and these null value annotations can be used as source points. Then, based on static program analysis techniques, cross-module, cross-method, path-sensitive, and context-sensitive data flow dependency constraint analysis is performed to obtain downstream code elements that have value flow dependencies on the source points. Finally, corresponding null value annotations are added to all downstream code elements.

[0255] Furthermore, the downstream code element with the added annotation can be used as the source again, and the process can be iterated repeatedly until no new null value annotations are added, thereby effectively enhancing the propagation of null value annotations.

[0256] Static program analysis techniques can include at least one of the following: AST, CFG, CG, and DFG. In other words, these techniques can be used to analyze the code structure information of the second code, which can effectively reflect the dependencies between various code elements in the second code. This code structure information can then be used to effectively enhance the propagation of null value annotations.

[0257] The following is combined Figure 4 The implementation of extracting various code structures using static program analysis techniques is explained. Figure 4 This is a schematic diagram illustrating the analysis of code structure information provided in the embodiments of this application.

[0258] like Figure 4 As shown, the second code can contain multiple modules; for example, a module can be understood as a file. Figure 4In the example of FIG. 1, it is assumed that the second code contains module 1, module 2 and module 3. Among them, there is a module dependency relationship between module 1 and module 2, and there is a dependency relationship between module 2 and module 3. For example, module 1 can be understood as an upstream module, and module 2 and module 3 can be understood as two downstream modules.

[0259] In the technical solution of the present application, single-module static program analysis can be performed for each module to achieve the propagation enhancement of null annotations within the single module. In addition, based on the propagation enhancement of null annotations for single modules, the propagation enhancement of null annotations between modules can be further performed according to the dependency relationship between the modules.

[0260] First, the following will be introduced in combination with Figure 4 The implementation of the propagation enhancement of null annotations for single modules will be introduced. Referring to Figure 5 For each module in the second code, an AST can be generated, for example. The AST can reflect the syntax structure of the program and is the basis for the compilation and analysis process. It can be understood that the AST contains all syntax elements in the program, such as variable declarations, function definitions, control flow statements, etc.

[0261] Further, based on the AST, a CFG can also be generated. For example, the CFG can be constructed by analyzing the control flow statements (such as if-else, loops, etc.) in the AST, representing the possible paths in the program execution process, thereby generating the CFG.

[0262] In addition, a DFG can also be generated based on the AST. The DFG focuses more on analyzing the data flow in the program, such as variable definition, use and assignment. The generation of DFG usually requires the information of CFG, because the analysis of data flow depends on the understanding of program control flow. Therefore, it can be understood that the DFG is generated based on the CFG. In one implementation, the DFG generated in the present embodiment can be a context-sensitive type of DFG, thereby achieving the precision enhancement of context sensitivity in the analysis process of code dependency.

[0263] At the same time, a CG can also be generated based on the AST to record the calling relationship between functions or methods in the program. In addition, context-sensitive technology is used to increase the precision of static program analysis.

[0264] Based on the analysis of the AST, CFG, DFG and CG code structure information, because these code structure information can reflect the dependency relationship between each code element in the second code, the propagation of null annotations can be performed according to the code dependency relationship reflected by these code structure information.

[0265] For example, for any first annotation in the second code, let's assume the code element corresponding to that first annotation is called the first code element. Then, based on the code structure information of the second code, we can find the second code element in the second code that has a dependency relationship with the first code element. Then, we can add a null-type second annotation at a second position in the second code, where the second position is the code position corresponding to the second code element in the second code, similar to the understanding described above. Furthermore, the added second annotation is the same as the first annotation, thus achieving the propagation of null-type annotations within the module.

[0266] After propagating null value annotations within a single module, cross-module propagation can be performed based on dependencies between modules. The implementation is similar to the method described above: it analyzes the dependencies between code elements across modules and then propagates null value annotations based on these dependencies. This effectively enhances the propagation of null value annotations across the entire second codebase.

[0267] For example, you can refer to Figure 5 Understanding the propagation of null annotations. Figure 5 This is a schematic diagram illustrating the dependency relationships of code elements provided in the embodiments of this application.

[0268] Reference Figure 5 As shown in section 501, assuming there are code elements a, b, c, d, e, and f, their dependencies can be referenced... Figure 5 To understand.

[0269] Then, annotations can be propagated based on this dependency relationship, as shown below. Figure 5 As shown in 501, assuming that code element a in the second code has a nullable annotation that can be @Nullable (that is, the nullable annotation described in the attached figure), then the nullable annotation of code element a can be used as the source point for propagating nullable annotations.

[0270] Reference Figure 5 As shown in section 502, because code elements b and c have a dependency on code element a, the `@Nullable` annotation can also be added to code elements b and c. This can be understood as implementing one round of null annotation propagation. After adding the `@Nullable` annotation to code elements b and c, their null annotations can also serve as source points for the next round of propagation.

[0271] Reference Figure 5As code element d and code element e have a dependency relationship with code element b, and code element f has a dependency relationship with code element c, as shown in 503, the nullity annotation of @Nullable can also be added for code element d, code element e and code element f, which can be understood as another round of nullity annotation propagation.

[0272] According to the above Figure 6 The logic introduced above continues to propagate the nullity annotation, which can effectively achieve the accurate acquisition of cross-module, cross-method, path and context-sensitive value flow dependency and flow path from the nullity source point to the downstream code element in the second code, so as to continue to add the nullity annotation of @Nullable or @NonNull for the code element downstream based on the upstream nullity annotation as the source point, so as to effectively realize the propagation enhancement of the nullity annotation.

[0273] On the basis of the above embodiment introduction, the following will be further introduced in combination with several specific examples.

[0274] Example 1: Assuming that according to the project development history, it is determined that mLiteAutoClipPreviewDelegate may be null, and the information is recorded in the historical nullity knowledge base.

[0275] Therefore, for example, the nullity annotation of @Nullable can be added in the following code segment:

[0276]

[0277] After adding the nullity annotation to the above code segment, the code checking tool can output the null pointer reference defect detection report for the code segment calling the above function.

[0278] For example, the code segment calling the above function is as follows:

[0279] Class SubClassName extends SuperClassName{

[0280] @Override

[0281] public View getCustomView(){

[0282] if(mContentView==null){

[0283] hideView(mLiteAutoClipPreviewDelegate.currentScene())

[0284] Correspondingly, the inputted null pointer reference defect detection report is used for indicating that "SubClassName.mLiteAutoClipPreviewDelegate" is nullable, so as to prompt the developer to process the null value in this place.

[0285] Example 2: Assuming that the return value of the Map.get() function in the source function of the first programming language may be null, the annotation information that the return value of the function may be null can be added in the source function null value knowledge base.

[0286] Then, assuming that the Map.get() function is called in the following code, the null value annotation of @Nullable can be added in the code.

[0287] ++@Nullable

[0288] public String getFilterInfo(String key){

[0289] return mFilterInfoMap.get(key);

[0290] }

[0291] After adding the null value annotation to the above code segment, the null pointer reference defect detection report can be output by the corresponding code checking tool for the code segment calling the above function.

[0292] For example, the code segment calling the above function is as follows:

[0293] filterName=getFilterInfo(resourceId).getName();

[0294] Correspondingly, the inputted null pointer reference defect detection report is used for indicating that "getFilterInfo(...)" is nullable, and there is no local check whether it is null when calling 'getName()', so as to prompt the developer to process the null value in this place.

[0295] Example 3: Code mode conforms to the null value related code mode

[0296] Suppose there is a piece of code containing code content compared with null, for example, a null annotation of @Nullable can be added for this piece of code:

[0297] ++@Nullable

[0298] Public String getInfo(String key){

[0299] if(key!=null){ / / Code pattern compared with null

[0300] return mInfoMap.get(key);

[0301] }

[0302] return null;

[0303] }

[0304] And after adding the null annotation for the above code segment, the corresponding code inspection tool can output a null reference defect detection report for the code segment calling the above function.

[0305] For example, the code segment calling the above function is as follows:

[0306] filterName=getFilterInfo(resourceId).getName();

[0307] Correspondingly, the output null reference defect detection report indicates that "getFilterInfo(...)" is nullable, and there is no local check whether it is null when calling 'getName()', prompting the developer to handle the null value in this place.

[0308] Example 4: Annotation Propagation

[0309] Suppose the second code contains a piece of code as follows:

[0310]

[0311] This means that in the second code, the Nullable null annotation has been added to ClassNameB.getMusicListDefault(). Assuming that there is a dependency between ClassNameA.mMusicList in the second code and ClassNameB.getMusicListDefault(), a null annotation can be added to, for example, ClassNameA.mMusicList in the second code as follows:

[0312]

[0313] After adding the null annotation to the above code segment, the corresponding code inspection tool can output a null pointer reference defect detection report for the code segment that calls the above function.

[0314] For example, the code segment that calls the above function is as follows:

[0315] if(mMusicList.equals(“SAA”)){

[0316] Correspondingly, the output null pointer reference defect detection report indicates, for example, that “mMusicList” is nullable, and there is no local check for null when calling ‘equals()’, prompting the developer to handle the null value at this place.

[0317] In summary, the technical solution of the present application can automatically generate / deliver null annotations (such as @Nullable and @NonNull) for target source code containing partial null annotations / without null annotations, to generate source code with null annotations, thereby greatly improving the existing null pointer exception detection and interception effect.

[0318] In the specific implementation process, by constructing the historical null knowledge base and the source function null knowledge base of the first programming language, the null information of the project self-developed or the source function code commonly used in the first programming language can be counted and summarized, so as to improve the scalability and maintainability of the knowledge base as a reference in the null adding process.

[0319] In addition, the null-related code patterns in the first programming language can also be systematically summarized, and then by comparing whether the code patterns of each code element conform to a specific code pattern, the addition of null annotations can be realized, so that more comprehensive null annotation processing can be realized.

[0320] Further, the technical solution of the present application also performs null annotation propagation of cross-module, cross-method, path and context-sensitive static program analysis technology, so that the generated annotations can be iteratively propagated, further expanding the annotation range. And through high-precision static program analysis technology, the upstream and downstream dependencies across modules and methods can also be analyzed, and based on the dependency constraints, the null annotations are propagated with very low false positive rate.

[0321] It should be noted that the module names involved in the embodiments of the present application can be defined as other names, as long as the functions of the modules can be realized, and the names of the modules are not limited specifically.

[0322] It should be noted that the user information (including but not limited to user equipment information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in the embodiments of the present application are all information and data authorized by the user or authorized by all parties, and the collection, use and processing of related data need to comply with relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation portal for user to choose authorization or refusal.

[0323] The code annotation processing method of the embodiments of the present application has been described above, and the device provided by the embodiments of the present application for executing the above method will be described below. Those skilled in the art can understand that the method and the device can be combined and referenced with each other, and the related device provided by the embodiments of the present application can execute the steps in the above code annotation processing method.

[0324] The code annotation processing method provided by the embodiments of the present application can be applied in an electronic device with data processing function. The possible implementation of the electronic device will be introduced below.

[0325] In one implementation manner, the embodiments of the present application provide an electronic device, Figure 6 The hardware structure schematic diagram of the electronic device provided by the embodiments of the present application is shown in the figure.

[0326] As Figure 1 shown, the electronic device 60 includes: a processor 601 and a memory 602; the memory 602 stores computer execution instructions; the processor 601 executes the computer execution instructions stored in the memory 602, so that the electronic device 60 executes the above method.

[0327] When the memory 602 is independently arranged, the electronic device further includes a bus 603 for connecting the memory 602 and the processor 601.

[0328] An embodiment of the present application provides a chip. The chip comprises a processor configured to invoke a computer program in a memory to execute the technical solutions in the above embodiments. The implementation principle and technical effects are similar to those of the above related embodiments, and will not be repeated here.

[0329] An embodiment of the present application further provides a computer readable storage medium. The computer readable storage medium stores a computer program. The computer program is executed by a processor to implement the above method. The method described in the above embodiments can be implemented by software, hardware, firmware or any combination thereof, in whole or in part. If implemented in software, the functions can be stored in or transmitted as one or more instructions or codes on a computer readable medium. The computer readable medium can include computer storage medium and communication medium, and can also include any medium that can carry computer programs from one place to another. The storage medium can be any target medium that can be accessed by a computer.

[0330] In a possible implementation, the computer readable medium can include RAM, ROM, compact disc read-only memory (CD-ROM) or other optical disk storage, magnetic disk storage or other magnetic storage devices, or any other medium that is targeted to carry desired program codes in the form of instructions or data structures and can be accessed by a computer. Moreover, any connection is appropriately referred to as a computer readable medium. For example, if software is transmitted from a website, server or other remote source using a coaxial cable, optical fiber cable, twisted pair, digital subscriber line (DSL) or wireless technology (such as infrared, radio and microwave), the coaxial cable, optical fiber cable, twisted pair, DSL or wireless technology (such as infrared, radio and microwave) is included in the definition of the medium. As used herein, magnetic disks and optical disks include compact disks, laser disks, optical disks, digital versatile disks (DVD), floppy disks and Blu-ray disks, in which magnetic disks usually reproduce data magnetically, and optical disks reproduce data optically with a laser. Combinations of the above should also be included in the scope of the computer readable medium.

[0331] An embodiment of the present application provides a computer program product, which comprises a computer program, when the computer program is executed, causes a computer to execute the above method.

[0332] The embodiments of the present application are described with reference to the flowcharts and / or block diagrams of the methods, devices (systems) and computer program products according to the embodiments of the present application. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, as well as a combination of flows and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing devices to produce a machine, so that the instructions executed by the computer or other programmable data processing devices generate a device that implements the functions specified in the flowcharts and / or block diagrams. Figure 1 one flow or a plurality of flows and / or blocks ​ one flow or a plurality of flows and / or blocks

[0333] The above detailed description of the specific implementation, the purpose, technical solutions and beneficial effects of the present application are further described in detail, and it should be understood that the above is only a specific implementation of the present application, and is not used to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made on the basis of the technical solutions of the present application shall be included in the protection scope of the present application.

Claims

1. A code annotation processing method, characterized in that, include: Based on the first null value knowledge base and the second null value knowledge base, a first annotation of null value type is added to the first position in the first code to obtain the second code. The first null value knowledge base is generated based on the first log, which contains record information of failures caused by null pointer dereference. The second null value knowledge base is generated based on the standard library source code of the first programming language. Based on the code structure information corresponding to the second code, a second annotation of type null is added at a second position in the second code, wherein the code element corresponding to the second position has a dependency relationship with the code element corresponding to the first position, and the code structure information is used to indicate the dependency relationship.

2. The method according to claim 1, characterized in that, The first null value knowledge base includes identification information of multiple code elements and null value annotation information of each of the multiple code elements; The step of adding a first annotation of type null value at a first code position in the first code according to the first null value knowledge base includes: For any code element in the first code, if the identification information of the code element exists in the first null value knowledge base, the null value annotation information of the code element is obtained from the first null value knowledge base; Based on the null value annotation information of the code element, the first annotation is added at a first position in the first code, wherein the first position is the code position in the first code corresponding to the code element, and the first annotation matches the null value annotation information of the code element.

3. The method according to claim 1 or 2, characterized in that, The second null value knowledge base includes null value annotation information for each of the multiple functions; The step of adding a first annotation of type null value at the first code position in the first code according to the second null value knowledge base includes: For any code element in the first code, if the code element is assigned the return value of the first function, or if the code element is passed the return value of the first function, and if the second null value knowledge base records null value annotation information of the function return value of the first function, then the first annotation is added at the first position in the first code. Wherein, the first position is the code position in the first code corresponding to the code element, and the first annotation is consistent with the null value annotation information of the first function.

4. The method according to any one of claims 1-3, characterized in that, The method further includes: Search the first log for multiple records of the first type of fault, which is a fault caused by null pointer dereference; For any one of the multiple record information, extract the identification information of the code element contained in the record information, and determine the null value annotation information of the code element; The identification information of the code element and the null value annotation information of the code element contained in the record information are used as a piece of data in the first null value knowledge base.

5. The method according to any one of claims 1-3, characterized in that, The method further includes: The code segment of the comment node is used to indicate the comment information declared for the program node corresponding to the comment node in the standard library source code of the first programming language. When the program node corresponding to the comment node is a function definition type, search the code segment of the comment node for null value annotation information of the program node; If it exists, then record the null value annotation information of the function return value corresponding to the program node in the second null value knowledge base.

6. The method according to any one of claims 1-5, characterized in that, The method further includes: For any code element in the first code, if the writing pattern of the code lines contained in the code element conforms to the first pattern, the first annotation is added to the code position corresponding to the code element in the first code; The first mode includes at least one of the following: assigning a null value to a variable, or comparing the variable with a null value.

7. The method according to any one of claims 1-6, characterized in that, The step of adding a second annotation of type null at a second position in the second code based on the code structure information corresponding to the second code includes: For any first annotation in the second code, determine the first code element corresponding to the first annotation; Based on the code structure information corresponding to the second code, find the second code element that has a dependency relationship with the first code element in the second code; Add a second annotation of type null at a second position in the second code, where the second position is the code position corresponding to the second code element in the second code, and the second annotation is the same as the first annotation.

8. The method according to claim 7, characterized in that, The code structure information includes at least one of the following: Abstract Syntax Tree (AST), Control Flow Graph (CFG), Call Graph (CG), and Data Flow Graph (DFG).

9. The method according to any one of claims 1-8, characterized in that, The code elements include at least one of the following types: class member variables, function return values, and function parameters; The code location corresponding to the code element is the definition location of the code element.

10. An electronic device, characterized in that, The electronic device includes: one or more processors and a memory; the memory is coupled to the one or more processors, the memory being used to store computer program code, the computer program code including computer instructions, and the one or more processors invoking the computer instructions to cause the electronic device to perform the method as described in any one of claims 1 to 9.

11. A chip system, characterized in that, The chip system is applied to an electronic device, the chip system including one or more processors, the one or more processors being used to invoke computer instructions to cause the electronic device to perform the method as described in any one of claims 1 to 9.

12. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes computer instructions that, when executed on an electronic device, cause the electronic device to perform the method as described in any one of claims 1 to 9.

13. A computer program product, characterized in that, The computer program product includes computer program code that, when run on an electronic device, causes the electronic device to perform the method as described in any one of claims 1 to 9.