A fingerprint feature generation method and matching method for code fragments
Through the fingerprint feature generation and matching method of code fragments, the problem of inaccurate software composition analysis caused by modification of open source components in the existing technology is solved, and higher analysis accuracy and vulnerability protection capabilities are achieved.
Patent Information
- Application Number
- CN202111449816.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-30
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2041-11-30
AI Technical Summary
Existing software composition analysis methods are based on code fingerprints, which are prone to inaccurate matching results due to adaptive modifications of open source components, and cannot accurately identify the open source components used in the software.
The fingerprint feature generation method of code fragments is used to generate fingerprint features representing code fragments through code cleaning, fixed-length coding generation and screening, and the content of the code fragments is determined through fingerprint feature matching to improve the accuracy of analysis.
It improves the accuracy of software composition analysis and vulnerability finding capabilities, ensuring accurate identification and traceability of open source components.
Smart Images

Figure CN114138279B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software analysis, and in particular to a fingerprint feature generation method and a matching method for code fragments. Background Art
[0002] With the application of open source components in software development, software development work has been greatly facilitated, resulting in an increasing number of software being created in recent years.
[0003] When companies use outsourced software systems, they need to identify which open source components are used in the software for system security. Companies generally use software composition analysis tools to analyze the software.
[0004] At present, the mainstream software composition analysis method is based on code fingerprints, that is, the method adopted is to perform MD5 encoding on the software code, and then perform MD5 encoding on the source code and component details in major open source libraries. Then, the MD5 number corresponding to the software and the MD5 encoding corresponding to the source code are matched to see if they are consistent. If there is a consistency, it means that the corresponding open source component is applied in the software.
[0005] According to the above-mentioned related technologies, the inventor believes that software composition analysis based on code fingerprints is basically performed on the code line. However, in the process of software development, open source components are often adaptively modified. Once a letter is modified, the encoding of the entire line will change, which will cause the line encoding of the open source component to change due to the change of the line, and then lead to inconsistent matching results, making the software composition analysis inaccurate. Summary of the invention
[0006] In order to improve the accuracy of analysis, the present application provides a fingerprint feature generation method and a matching method for code snippets.
[0007] In a first aspect, the present application provides a method for generating fingerprint features of a code snippet, which adopts the following technical solution:
[0008] A method for generating fingerprint features of a code snippet comprises the following steps:
[0009] Get the source code of the code snippet;
[0010] Performing code cleaning on the source code to obtain a continuous string carrying code line number information, wherein the code cleaning includes at least one of the following: removing line breaks, removing spaces, and removing comment information;
[0011] Using a first window of a preset character length, slidingly selecting string fragments in the continuous string one by one, obtaining a plurality of continuous string fragments, and determining an end code line number corresponding to each string fragment;
[0012] Obtaining a fixed-length code for each string fragment, and obtaining a plurality of second fixed-length codes corresponding one-to-one to the plurality of string fragments;
[0013] Using a second window with a preset number of length codes, slidingly selecting fixed-length code sets from the plurality of second fixed-length codes one by one, to obtain a plurality of continuous fixed-length code sets;
[0014] Filter the third fixed-length code from each fixed-length code set to obtain multiple third fixed-length codes, and determine the end code line number of the string fragment corresponding to each third fixed-length code;
[0015] The multiple third fixed-length codes and the end code line number of each third fixed-length code are used as fingerprint features of the code fragment.
[0016] By adopting the above technical solution, the source code of the code snippet is split into string fragments, so that the source code is rounded to zero, and then the string fragment is replaced with a fixed-length code to achieve dimensionality reduction to reduce the subsequent matching amount, and the fixed-length code is screened to further reduce the subsequent matching amount. In addition, the screened fixed-length code also corresponds to the end code line number, and after the matching is completed, the end code line number can be used to trace the source of similar codes.
[0017] Optionally, the following steps are also included:
[0018] Obtaining a first fixed-length code of the source code;
[0019] The first fixed-length code, the multiple third fixed-length codes and the end code line number of each third fixed-length code are used as fingerprint features of the code fragment.
[0020] By adopting the above technical solution, the first fixed-length code is used to represent the entire source code, which is suitable for fast matching between source codes and complements the fragment matching method corresponding to the third fixed-length code.
[0021] Optionally, selecting a third fixed-length code from each fixed-length code set to obtain a plurality of third fixed-length codes comprises the following steps:
[0022] Selecting a fixed-length code from each fixed-length code set as the third fixed-length code;
[0023] Determine whether the adjacent third fixed-length codes are the same.
[0024] If adjacent third fixed-length codes are the same, only the rightmost third fixed-length code is retained.
[0025] By adopting the above technical scheme, due to the generation method of fixed-length code sets, the contents of adjacent fixed-length code sets are highly overlapped, resulting in the situation that the screened out third fixed-length code is the same third fixed-length code, and the repeated third fixed-length code has no meaning, so only one needs to be retained. At the same time, the weight of the third fixed-length code can be reduced, reducing the bloat of the data.
[0026] Optionally, get the storage path of the source code of the code snippet;
[0027] A storage path of the source code of the code snippet is added to each third fixed-length code of the corresponding code snippet.
[0028] By adopting the above technical solution, in order to facilitate the tracing of the code snippet, the storage path is synchronized to the third fixed-length code, and the location of the corresponding code snippet can be clearly understood according to the storage path in the matched third fixed-length code.
[0029] In a second aspect, the present application provides a fingerprint feature matching method for code snippets, which adopts the following technical solution:
[0030] A fingerprint feature matching method for code snippets includes the following steps:
[0031] The fingerprint feature of the code snippet is extracted based on the method described above, and similar code snippets are matched to the code snippet based on the fingerprint feature.
[0032] By adopting the above technical solution, a variety of existing codes can also extract corresponding fingerprint features through the above method. By matching the corresponding fingerprint features from the fingerprint features of the code fragments, it is possible to understand which existing codes the code fragments use, thereby completing the tracing of the code fragments with higher accuracy.
[0033] Optionally, matching similar code snippets for the code snippet based on fingerprint features includes the following steps:
[0034] Establish result sets corresponding to existing codes;
[0035] Traversing the third fixed-length code in the fingerprint feature of the code fragment to match a fourth fixed-length code of a preset code corresponding to the third fixed-length code from fourth fixed-length codes stored in a preset database;
[0036] Determine whether the corresponding fourth fixed-length code is stored in the corresponding result set,
[0037] If the corresponding fourth fixed-length code is not stored in the result set, copy the corresponding fourth fixed-length code into the result set;
[0038] A similar code snippet is determined based on the fourth fixed-length code in the result set.
[0039] By adopting the above technical solution, the existing code is an open source code or other code known to the user, and a code fragment may be composed of multiple fragments of existing codes. In this case, fingerprint features of multiple different existing codes will be matched during the matching process. In order to make a distinction, a structure set corresponding to the existing code is pre-established to store the fourth fixed-length code of the matched corresponding existing code.
[0040] Optionally, determining whether the corresponding fourth fixed-length code is stored in the corresponding result set further includes the following steps:
[0041] If the corresponding fourth fixed-length code is not stored in the result set, the corresponding fourth fixed-length code is copied to the result set, and the number of hits, which is initially one, is added to the corresponding fourth fixed-length code;
[0042] If the corresponding fourth fixed-length code has been stored in the preset result set, the number of hits in the corresponding fourth fixed-length code is increased by one;
[0043] Determining similar code fragments based on the fourth fixed-length code in the result set also includes the following steps:
[0044] The code information of similar code fragments is determined based on the fourth fixed-length code in the result set, and the code information is sorted according to the number of hits.
[0045] By adopting the above technical solution, the fingerprint feature of the code snippet may match the same fourth fixed-length code multiple times. By adding the number of hits, the number of matches can be reflected for the user to understand; the higher the number of hits, the more times the code corresponding to the fourth fixed-length code is used in the code snippet, that is, the higher the coupling.
[0046] Optionally, determining a similar code snippet based on the fourth fixed-length code in the result set includes the following steps:
[0047] Count the total length of the fourth fixed-length codes in all result sets, and determine whether the total length exceeds the first preset length.
[0048] If the total length exceeds the first preset length, all fourth fixed-length codes are filtered based on the preset first filtering rule, and the filtered fourth fixed-length codes are stored in a preset candidate set;
[0049] If the total length is less than or equal to the first preset length, the fourth fixed-length codes in each result set are filtered based on the preset second filtering rule, and the filtered fourth fixed-length codes are stored in the preset candidate set;
[0050] Determine the corresponding code fragment according to the fourth fixed-length code in the candidate set.
[0051] By adopting the above technical solution, by setting the first preset length to make a preliminary judgment on the number of fourth fixed-length codes, it is determined which filtering rule to use to filter the fourth fixed-length codes, making the filtering more targeted, and reducing the number of fourth fixed-length codes involved in the matching while avoiding affecting the matching accuracy as much as possible, thereby reducing the amount of calculation.
[0052] Optionally, the fourth fixed-length code has the number of lines of code corresponding to the string fragment;
[0053] Filtering all fourth fixed-length codes based on a preset first filtering rule, and storing the filtered fourth fixed-length codes into a preset candidate set, includes the following steps:
[0054] Determine whether the number of code lines of the fourth fixed-length code exceeds a preset minimum number of code lines, and determine whether the number of hits of the corresponding fourth fixed-length code exceeds a preset minimum number of hits,
[0055] If the number of code lines of the fourth fixed-length code exceeds the preset minimum number of code lines, and / or the hit count of the fourth fixed-length code exceeds the preset minimum hit count, the corresponding fourth fixed-length code is added to the candidate set.
[0056] Optionally, the fourth fixed-length code has an end code line number corresponding to the string fragment,
[0057] Filtering the fourth fixed-length code in each result set based on the preset second filtering rule, and storing the filtered fourth fixed-length code in the preset candidate set, includes the following steps:
[0058] It is determined in turn whether the number of fourth fixed-length codes in a single result set exceeds a predetermined number,
[0059] If the number of fourth fixed-length codes in the result set exceeds the predetermined number, continue to determine whether the total length of the fourth fixed-length codes in the result set exceeds the second preset length;
[0060] If the total length of the fourth fixed-length code in the result set exceeds the second preset length, then judging in sequence according to the sequence of the end code line numbers whether the number of hits of the fourth fixed-length code in the result set exceeds the predetermined number;
[0061] If the hit count of the fourth fixed-length code exceeds a predetermined number, the current fourth fixed-length code is added to the candidate set, and a predetermined row skip instruction is executed to delete a predetermined number of fourth fixed-length codes arranged after the hit count of the current fourth fixed-length code;
[0062] If the number of hits of the fourth fixed-length code is less than or equal to the predetermined number, the current fourth fixed-length code is added to the candidate set.
[0063] To summarize, the present application includes at least one of the following beneficial technical effects: generating fingerprint features for characterizing code snippets by means of code cleaning, window conversion, and fixed-length code selection, and then determining multiple fourth fixed-length codes by fingerprint feature matching. According to the matched fourth fixed-length codes, it can be clarified which existing code snippets the content of the current code snippet involves, thereby obtaining accurate matching results and improving the ability to find and protect vulnerabilities. BRIEF DESCRIPTION OF THE DRAWINGS
[0064] Figure 1 It is a flowchart of the fingerprint feature generation method according to an embodiment of the present application.
[0065] Figure 2 It is a flowchart of the fingerprint feature matching method according to an embodiment of the present application. DETAILED DESCRIPTION
[0066] The following is combined with Figure 1 to Figure 2 This application is described in further detail.
[0067] The present application embodiment discloses a method for generating fingerprint features of a code snippet. Figure 1 ,The method for generating fingerprint features of code snippets includes the following steps:
[0068] S100. Obtain source code of the code snippet.
[0069] The source code can be obtained by manually inputting the code or directly retrieving the stored code from a preset database, as long as the source code can be obtained.
[0070] S200: Clean the source code to obtain a continuous string carrying code line number information.
[0071] Among them, code cleaning includes at least one of the following: removing line breaks, removing spaces, and removing comment information. In addition to the above-mentioned cleaning methods, the staff can also set objects that need to be additionally removed.
[0072] The purpose of code cleaning is to convert source code into continuous strings, which is convenient for generating string fragments in subsequent steps. However, in order to show the relationship between the subsequent memorized string fragments and the source code, it is necessary to retain the relationship between the continuous string and the source code by carrying the code line number information in the continuous string.
[0073] For example, the source code is:
[0074] “Int;
[0075] sha;
[0076] ign”.
[0077] Then, the continuous string after cleaning is: "Intshaign", where the row number corresponding to "Int" is 1, the row number corresponding to "sha" is 2, and the row number corresponding to "ign" is 3.
[0078] S300: Using a first window of a preset character length, slide and select character string segments in the continuous character string one by one to obtain a plurality of continuous character string segments, and determine the end code line number corresponding to each character string segment.
[0079] The preset character length is set manually.
[0080] Sliding one by one means that after obtaining the current string segment, the first window moves one position backward along the continuous string, and obtains one string segment each time it moves, until the string segment containing the last character of the continuous string is obtained.
[0081] The ending code line number corresponding to the string fragment refers to the line number corresponding to the last character in the string fragment in the source code.
[0082] Here, still taking the continuous string "Intshaign" as an example, assuming that the preset character length is 5, then according to the continuous string "Intshaign", we can get the string fragments "Intsh", "ntsha", "tshai", "shaig" and "haign", and the corresponding row numbers of these string fragments are 2, 2, 3, 3, 3 respectively.
[0083] Of course, in actual use, the preset character length is generally set to 30, which results in the situation where the line numbers corresponding to adjacent string fragments are rarely the same.
[0084] S400: Obtain a fixed-length code for each character string fragment, and obtain a plurality of second fixed-length codes corresponding one-to-one to the plurality of character string fragments.
[0085] Fixed-length encoding refers to an encoding value used to characterize string characteristics, such as a message digest or hash value obtained through a hash algorithm.
[0086] In some embodiments of the present application, the fixed-length code is preferably obtained by using a variational autoencoder (VAE). A variational autoencoder is a generative model based on a neural network, which is usually trained based on unsupervised learning. Compared with a hash algorithm, a hash algorithm is a method of reducing the dimension of a target character by compression to obtain a string of fixed-length code values; while the fixed-length code generated by a variational autoencoder is not reduced by compression, but by generation. The obtained code value carries semantic information, which is particularly suitable for the generation of code for code snippets with semantic information.
[0087] The second fixed-length code includes the code value and the end code line number corresponding to the corresponding string fragment.
[0088] S500, using a second window with a preset number of length codes, slidingly selecting fixed-length code sets from the multiple second fixed-length codes one by one to obtain multiple continuous fixed-length code sets.
[0089] Before selecting a fixed-length code set, the fixed-length codes in the plurality of second fixed-length codes are sorted in order of row numbers to form a continuous fixed-length code string similar to a continuous character string.
[0090] The method of obtaining a fixed-length encoding set is similar to the method of obtaining a string fragment.
[0091] Assuming that the length of the second window is 4, that is, the number of preset fixed-length codes is 4, then for the continuous fixed-length code string "11, 12, 13, 14, 15, 16, 17", several fixed-length code sets "11, 12, 13", "12, 13, 14", "13, 14, 15", "14, 15, 16" and "15, 16, 17" can be formed.
[0092] S600, screening a third fixed-length code from each fixed-length code set to obtain a plurality of third fixed-length codes, and determining an end code line number of a character string segment corresponding to each third fixed-length code.
[0093] The third fixed-length code includes a fixed-length code and an end code line number corresponding to the fixed-length code, and the fixed-length code of the third fixed-length code is the most representative fixed-length code in the corresponding fixed-length code set.
[0094] In one embodiment, selecting a third fixed-length code from each fixed-length code set to obtain a plurality of third fixed-length codes includes the following steps:
[0095] A fixed-length code is selected from each fixed-length code set as the third fixed-length code.
[0096] Determine whether the adjacent third fixed-length codes are the same.
[0097] If the adjacent third fixed-length codes are the same, only the rightmost third fixed-length code is retained;
[0098] If adjacent third fixed-length codes are different, the adjacent third fixed-length codes are retained.
[0099] The method of screening fixed-length codes can be to select the fixed-length code with the smallest value from the fixed-length code set, or to select the fixed-length code with the largest data from the fixed-length code set, as long as it can reduce the number of third fixed-length codes.
[0100] In addition, the above method of generating a fixed-length code collection will undoubtedly result in the third fixed-length code selected from adjacent fixed-length code collections being the same. In order to further reduce the number of third fixed-length codes, when adjacent third fixed-length codes are the same, only one third fixed-length code is retained and the other third fixed-length code is deleted. And according to the moving direction of the second window, the retained third fixed-length code is on the far right to avoid omission when at least three consecutively adjacent third fixed-length codes are the same.
[0101] In this embodiment, the fixed-length code for screening the minimum value is used as the third fixed-length code for illustration. Assume that the multiple fixed-length code sets are (22, 23, 24), (23, 24, 11), (24, 11, 12), (11, 12, 24), (12, 24, 25), and the fixed-length codes screened out by each fixed-length code set are 22, 11, 11, 11, 12, respectively. However, since the three fixed-length codes in the middle are the same, only the last 11 is retained. Finally, the end code line number corresponding to the fixed-length code must be carried, so that the third fixed-length code is presented as (22, 3), (11, 8), (12, 14), where the former value is the fixed-length code and the latter value is the corresponding end code line number.
[0102] In one embodiment, when the source code of the code snippet is obtained, the storage path of the source code of the code snippet is also obtained.
[0103] A storage path of the source code of the code snippet is added to each third fixed-length code of the corresponding code snippet.
[0104] The storage path refers to the storage location of the source code. Different source codes correspond to different storage paths. Therefore, after the storage path is added to the third fixed-length code, the corresponding source code can be traced back through the storage path in the third fixed-length code.
[0105] S700: Use the multiple third fixed-length codes and the end code line number of each third fixed-length code as the fingerprint feature of the code fragment.
[0106] In one embodiment, before code cleaning is performed on the source code, the following steps are also included:
[0107] A first fixed-length code of the source code is obtained, and the first fixed-length code is used as one of the fingerprint features of the code fragment.
[0108] The first fixed-length code is also a fixed-length code value corresponding to the source code calculated by the variational autoencoder (VAE) with the source code as input. However, unlike the single third fixed-length code which only represents part of the source code, the first fixed-length code represents the entire source code.
[0109] The embodiment of the present application also discloses a fingerprint feature matching method of a code snippet, which is applied to the source tracing of the code snippet. The source code of the code snippet can use the codes of multiple open source components, and the use of the open source components needs to meet the corresponding open source agreement.
[0110] Different open source components correspond to different open source protocols, and different open source protocols have different usage requirements.
[0111] For example, the GPL agreement requires that if the open source components corresponding to the GPL agreement are used in a software, the software product must also be open source and free; the BSD agreement allows the use or development of commercial software based on the BSD code for release and sale.
[0112] Therefore, it is very important to understand the open source components used in a piece of source code. This application adopts the above-mentioned feature fingerprint generation method to generate the feature fingerprint of the code fragment and the feature fingerprint of the open source component respectively, and through the matching between the feature fingerprints, the open source component corresponding to the code fragment can be determined.
[0113] A fingerprint feature matching method for code snippets, see Figure 2 , including the following steps:
[0114] S800: Extract fingerprint features of code snippets based on a fingerprint feature generation method of code snippets.
[0115] S900: Match similar code snippets to the code snippet based on fingerprint features.
[0116] The fingerprint features of the code snippet include the first fixed-length code and the third fixed-length code, but not every code snippet needs to generate the first fixed-length code and the third fixed-length code.
[0117] For example, if the source code of the code snippet is directly obtained from the existing code without any modification, the source code of the code snippet can match the same existing code as long as the first fixed-length code is generated. In an embodiment, the existing code is an open source code.
[0118] When the source code of a code snippet is generated by making adaptive changes based on an open source code, the source code of the code snippet that needs to be traced can only be matched to a similar open source code by generating a third fixed-length code.
[0119] Therefore, in order to improve efficiency, step S800 is divided into:
[0120] S810: Extract a first fixed-length code of the code fragment based on a fingerprint feature generation method of the code fragment.
[0121] S820. Extract a third fixed-length code of the code snippet based on the fingerprint feature generation method of the code snippet.
[0122] And step S900 is split into:
[0123] S910: Match a similar code snippet for the code snippet based on the first fixed-length code.
[0124] S920. Match a similar code snippet for the code snippet based on the third fixed-length code.
[0125] Among them, step S810 and step S910 are executed before step S820. And if similar open source code can be matched through the first fixed-length code when executing step S910, there is no need to execute subsequent steps S820 and step S920. Only in step S910, if similar open source code cannot be matched through the first fixed-length code, step S820 and step S920 are executed in sequence.
[0126] In one embodiment, matching a similar code snippet for the code snippet based on the first fixed-length encoding includes the following steps:
[0127] A fifth fixed-length code corresponding to the first fixed-length code is matched from the fifth fixed-length codes stored in the preset database, and a similar code fragment is determined according to the fifth fixed-length code.
[0128] The fifth fixed-length code is the code value calculated by using a variational autoencoder (VAE) on the open source code. Its essence is the same as the formation method of the first fixed-length code, except that the first fixed-length code corresponds to the code fragment that needs to be traced, while the fifth fixed-length code corresponds to the open source component.
[0129] In one embodiment, matching similar code snippets for the code snippet based on the third fixed-length encoding includes the following steps:
[0130] S921. Create a result set corresponding to the existing code.
[0131] A result set is a virtual space used to store data.
[0132] One code snippet corresponds to one result set.
[0133] The purpose of establishing the result set is to store the fingerprint features of scattered and numerous similar code fragments matched by the third fixed-length encoding.
[0134] S922. Traverse the third fixed-length code of the code fragment to match a fourth fixed-length code of a preset code corresponding to the third fixed-length code from fourth fixed-length codes stored in a preset database.
[0135] The fourth fixed-length code is generated in the same manner as the third fixed-length code, except that the source code obtained in step S100 is an open source code. Therefore, the fourth fixed-length code also includes the corresponding fixed-length code, the corresponding end code line number, and the storage path of the corresponding open source component.
[0136] Matching the corresponding fourth fixed-length code according to the third fixed-length code means matching the fixed-length code in the third fixed-length code with the fixed-length code in the fourth fixed-length code. If the same fixed-length code is matched, it means that the corresponding third fixed-length code corresponds to the fourth fixed-length code.
[0137] S923. Determine whether the corresponding fourth fixed-length code is stored in the corresponding result set. If the corresponding fourth fixed-length code is not stored in the result set, copy the corresponding fourth fixed-length code to the result set.
[0138] If the corresponding fourth fixed-length code is not stored in the result set, in addition to copying the corresponding fourth fixed-length code to the result set, a hit count initially equal to one is added to the corresponding fourth fixed-length code.
[0139] If the corresponding fourth fixed-length code has been stored in the preset result set, the number of hits in the corresponding fourth fixed-length code is increased by one.
[0140] The hit count is used to indicate the number of corresponding third fixed-length codes matched by the same fourth fixed-length code. The higher the hit count, the more frequently the existing code corresponding to the corresponding fourth fixed-length code is used.
[0141] S924. Determine similar code fragments based on the fourth fixed-length code in the result set.
[0142] The storage path in the fourth fixed-length code is traced back to the corresponding existing code, and information such as the component name, license, version number, manufacturer, included vulnerabilities, etc. of the existing code is displayed.
[0143] Since the source code of the code snippet that needs to be traced is often long, a large number of fourth fixed-length codes are matched. Although identifying similar code snippets for all fourth fixed-length codes can make the result more accurate, there is also a problem of large amount of computation. Therefore, the fourth fixed-length codes need to be properly filtered.
[0144] In one embodiment, determining similar code snippets based on the fourth fixed-length code in the result set includes the following steps:
[0145] Count the total length of the fourth fixed-length codes in all result sets, and determine whether the total length exceeds the first preset length.
[0146] If the total length exceeds the first preset length, all fourth fixed-length codes are filtered based on the preset first filtering rule, and the filtered fourth fixed-length codes are stored in a preset candidate set;
[0147] If the total length is less than or equal to the first preset length, the fourth fixed-length codes in each result set are filtered based on the preset second filtering rule, and the filtered fourth fixed-length codes are stored in the preset candidate set;
[0148] Determine the corresponding code fragment according to the fourth fixed-length code in the candidate set.
[0149] The first preset length is set manually. In this embodiment, the first preset length is 36000.
[0150] When the total length of the fourth fixed-length codes in all result sets exceeds the first preset length, it indicates that the number of fourth fixed-length codes is too large and needs to be filtered using the preset first filtering rule.
[0151] However, when the total length of the fourth fixed-length codes in all result sets does not exceed the first preset length, it does not mean that the number of fourth fixed-length codes is less, and the preset second filtering rule is still required to be used for filtering.
[0152] The method of filtering all fourth fixed-length codes based on a preset first filtering rule and storing the filtered fourth fixed-length codes into a preset candidate set includes the following steps:
[0153] Determine whether the number of code lines of the fourth fixed-length code exceeds a preset minimum number of code lines, and determine whether the number of hits of the corresponding fourth fixed-length code exceeds a preset minimum number of hits,
[0154] If the number of code lines of the fourth fixed-length code exceeds the preset minimum number of code lines, and / or the hit count of the fourth fixed-length code exceeds the preset minimum hit count, the corresponding fourth fixed-length code is added to the candidate set.
[0155] The number of code lines is obtained by subtracting the end code line numbers in adjacent fourth fixed-length codes. For example, if the end code line number corresponding to the previous fourth fixed-length code is 6, and the end code line number corresponding to the next fourth fixed-length code is 16, then the number of code lines corresponding to the next fourth fixed-length code is 10.
[0156] The preset minimum number of code lines and the minimum number of hits are set by the worker. In order to achieve effective filtering, in this embodiment, the preset minimum number of code lines is 9 and the minimum number of hits is 4.
[0157] In addition, the judgment criteria are also set manually. If strict filtering criteria are required, the fourth fixed-length code can be added to the candidate set only when the number of code lines exceeds the preset minimum number of code lines and the number of hits exceeds the preset minimum number of hits.
[0158] If a looser filtering standard is adopted, the fourth fixed-length code can be added to the candidate set when the number of code lines exceeds the preset minimum number of code lines, or the number of hits exceeds the preset minimum number of hits.
[0159] Filtering the fourth fixed-length code in each result set based on the preset second filtering rule, and storing the filtered fourth fixed-length code in the preset candidate set, includes the following steps:
[0160] It is determined in turn whether the number of fourth fixed-length codes in a single result set exceeds a predetermined number,
[0161] If the number of fourth fixed-length codes in the result set exceeds the predetermined number, continue to determine whether the total length of the fourth fixed-length codes in the result set exceeds the second preset length;
[0162] If the total length of the fourth fixed-length code in the result set exceeds the second preset length, then judging in sequence according to the sequence of the end code line numbers whether the number of hits of the fourth fixed-length code in the result set exceeds the predetermined number;
[0163] If the hit count of the fourth fixed-length code exceeds a predetermined number, the current fourth fixed-length code is added to the candidate set, and a predetermined row skip instruction is executed to delete a predetermined number of fourth fixed-length codes arranged after the hit count of the current fourth fixed-length code;
[0164] If the number of hits of the fourth fixed-length code is less than or equal to the predetermined number, the current fourth fixed-length code is added to the candidate set.
[0165] The second preset length, the predetermined number, the predetermined times, and the predetermined number are all set manually, and the second preset length must be smaller than the first preset length. In this embodiment, the second preset length is 4000, the predetermined number is 50, the predetermined times is 4, and the predetermined number is 5.
[0166] When the result set simultaneously satisfies that the number of fourth fixed-length codes exceeds the predetermined number and the total length of the fourth fixed-length codes in the result set exceeds the second preset length, it indicates that the number of fourth fixed-length codes in the corresponding result set is large, so filtering can be performed by skipping rows.
[0167] If the result set cannot satisfy the above two conditions at the same time, it means that the number of fourth fixed-length codes in the result set is small and there is no need to skip lines.
[0168] Of course, the prerequisite for executing the jump instruction is that the hit times of the fourth fixed-length code exceeds the predetermined times, and the fourth fixed-length code whose hit times exceed the predetermined times already has a high role in representing the corresponding existing code fragment. Even if the subsequent several fourth fixed-length codes with high adjacency also have a high hit times, it will not cause much impact. Therefore, the subsequent several fourth fixed-length codes with high adjacency are deleted to reduce the number of fourth fixed-length codes.
[0169] In addition, in order to reflect the priority of the dependency between the current code snippet and each existing code, after determining the corresponding existing code through the fourth fixed-length encoding, the matched existing codes are sorted, wherein the sorting basis includes at least one of the number of hits, the number of hit lines, the storage path, etc.
[0170] In one embodiment, the number of hit rows and the number of hits are weighted to obtain a weighted value.
[0171] Sort the existing codes in descending order of weighted values. If the weighted values are the same, sort them according to the length of the storage path, and the existing codes with longer storage paths are placed behind the existing codes with shorter storage paths. If the lengths of the storage paths are still the same, sort them according to the size of the single hit line number, and the existing codes with a larger hit line number are placed in front of the existing codes with a smaller hit line number.
[0172] The weighted calculation formula is the number of rows*K1+the number of hits*K2. In this embodiment, K1=K2=1.
[0173] The above are all preferred embodiments of the present application, and the protection scope of the present application is not limited thereto. Therefore, any equivalent changes made according to the structure, shape, and principle of the present application should be included in the protection scope of the present application.
Claims
1. A method for generating fingerprint features of a code fragment, characterized in that: The following steps are involved: Get the source code of the code snippet; Performing code cleaning on the source code to obtain a continuous string carrying code line number information, wherein the code cleaning includes at least one of the following: removing line breaks, removing spaces, and removing comment information; Using a first window of a preset character length, slidingly selecting character string fragments in the continuous character string one by one, obtaining a plurality of continuous character string fragments, and determining an end code line number corresponding to each character string fragment; Obtaining a fixed-length code for each string fragment, and obtaining a plurality of second fixed-length codes corresponding one-to-one to the plurality of string fragments, wherein the fixed-length codes are obtained by using a variational autoencoder; Using a second window with a preset number of length codes, slidingly selecting fixed-length code sets from the plurality of second fixed-length codes one by one, to obtain a plurality of continuous fixed-length code sets; Filter the third fixed-length code from each fixed-length code set to obtain multiple third fixed-length codes, and determine the end code line number of the string fragment corresponding to each third fixed-length code; Using the plurality of third fixed-length codes and the end code line number of each third fixed-length code as the fingerprint feature of the code fragment; Obtaining a first fixed-length code of the source code; Using the first fixed-length code, the plurality of third fixed-length codes, and the end code line number of each third fixed-length code as the fingerprint feature of the code fragment; Selecting a third fixed-length code from each fixed-length code set to obtain a plurality of third fixed-length codes includes the following steps: Selecting a fixed-length code from each fixed-length code set as the third fixed-length code; Determine whether the adjacent third fixed-length codes are the same. If the adjacent third fixed-length codes are the same, only the rightmost third fixed-length code is retained; Get the storage path of the source code of the code snippet; A storage path of the source code of the code snippet is added to each third fixed-length code of the corresponding code snippet.
2. A fingerprint feature matching method for code snippets, characterized in that: The following steps are involved: Extract fingerprint features of the code snippet based on the method described in any one of claims 1, and match similar code snippets to the code snippet based on the fingerprint features.
3. The fingerprint feature matching method of a code fragment according to claim 2, characterized in that: Matching similar code snippets for the code snippet based on the fingerprint feature includes the following steps: Establish result sets corresponding to existing codes; Traversing the third fixed-length code in the fingerprint feature of the code fragment to match a fourth fixed-length code of a preset code corresponding to the third fixed-length code from fourth fixed-length codes stored in a preset database; Determine whether the corresponding fourth fixed-length code is stored in the corresponding result set, If the corresponding fourth fixed-length code is not stored in the result set, copy the corresponding fourth fixed-length code into the result set; A similar code snippet is determined based on the fourth fixed-length code in the result set.
4. The fingerprint feature matching method of a code fragment according to claim 3 is characterized in that: Determining whether the corresponding fourth fixed-length code is stored in the corresponding result set also includes the following steps: If the corresponding fourth fixed-length code is not stored in the result set, the corresponding fourth fixed-length code is copied to the result set, and the number of hits, which is initially one, is added to the corresponding fourth fixed-length code; If the corresponding fourth fixed-length code has been stored in the preset result set, the number of hits in the corresponding fourth fixed-length code is increased by one; Determining similar code fragments based on the fourth fixed-length code in the result set also includes the following steps: The code information of similar code fragments is determined based on the fourth fixed-length code in the result set, and the code information is sorted according to the number of hits.
5. The fingerprint feature matching method of a code fragment according to claim 4, characterized in that: Determining similar code snippets based on the fourth fixed-length code in the result set includes the following steps: Count the total length of the fourth fixed-length codes in all result sets, and determine whether the total length exceeds the first preset length. If the total length exceeds the first preset length, all fourth fixed-length codes are filtered based on the preset first filtering rule, and the filtered fourth fixed-length codes are stored in a preset candidate set; If the total length is less than or equal to the first preset length, the fourth fixed-length codes in each result set are filtered based on the preset second filtering rule, and the filtered fourth fixed-length codes are stored in the preset candidate set; Determine the corresponding code fragment according to the fourth fixed-length code in the candidate set.
6. The fingerprint feature matching method of a code fragment according to claim 5, characterized in that: The fourth fixed-length code has the number of code lines corresponding to the string fragment; Filtering all fourth fixed-length codes based on a preset first filtering rule, and storing the filtered fourth fixed-length codes into a preset candidate set, includes the following steps: Determine whether the number of code lines of the fourth fixed-length code exceeds a preset minimum number of code lines, and determine whether the number of hits of the corresponding fourth fixed-length code exceeds a preset minimum number of hits, If the number of code lines of the fourth fixed-length code exceeds the preset minimum number of code lines, and / or the hit count of the fourth fixed-length code exceeds the preset minimum hit count, the corresponding fourth fixed-length code is added to the candidate set.
7. The fingerprint feature matching method of a code fragment according to claim 5, characterized in that: The fourth fixed-length code has an end code line number corresponding to the string fragment, Filtering the fourth fixed-length code in each result set based on the preset second filtering rule, and storing the filtered fourth fixed-length code in the preset candidate set, includes the following steps: It is determined in turn whether the number of fourth fixed-length codes in a single result set exceeds a predetermined number, If the number of fourth fixed-length codes in the result set exceeds the predetermined number, continue to determine whether the total length of the fourth fixed-length codes in the result set exceeds the second preset length; If the total length of the fourth fixed-length code in the result set exceeds the second preset length, then judging in sequence according to the sequence of the end code line numbers whether the number of hits of the fourth fixed-length code in the result set exceeds the predetermined number; If the hit count of the fourth fixed-length code exceeds a predetermined number, the current fourth fixed-length code is added to the candidate set, and a predetermined row skip instruction is executed to delete a predetermined number of fourth fixed-length codes arranged after the hit count of the current fourth fixed-length code; If the number of hits of the fourth fixed-length code is less than or equal to the predetermined number, the current fourth fixed-length code is added to the candidate set.
Citation Information
Patent Citations
KPI (Key Performance Indicator) anomaly detection method and device based on conditional variation automatic encoder
CN113114529A
Coding device, decoding device, coding method, decoding method and program
CN1848692A
