CN110297639A - Method and apparatus for detecting code - Google Patents

Method and apparatus for detecting code Download PDF

Info

Publication number
CN110297639A
CN110297639A CN201910584002.2A CN201910584002A CN110297639A CN 110297639 A CN110297639 A CN 110297639A CN 201910584002 A CN201910584002 A CN 201910584002A CN 110297639 A CN110297639 A CN 110297639A
Authority
CN
China
Prior art keywords
code
code element
detected
library
candidate
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN201910584002.2A
Other languages
Chinese (zh)
Other versions
CN110297639B (en
Inventor
刘志伟
邢永旭
张克鹏
白伟
李涛
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Baidu Netcom Science and Technology Co Ltd
Original Assignee
Beijing Baidu Netcom Science and Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Baidu Netcom Science and Technology Co Ltd filed Critical Beijing Baidu Netcom Science and Technology Co Ltd
Priority to CN201910584002.2A priority Critical patent/CN110297639B/en
Publication of CN110297639A publication Critical patent/CN110297639A/en
Application granted granted Critical
Publication of CN110297639B publication Critical patent/CN110297639B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F8/00Arrangements for software engineering
    • G06F8/40Transformation of program code
    • G06F8/41Compilation
    • G06F8/44Encoding
    • G06F8/443Optimisation
    • G06F8/4434Reducing the memory space required by the program code
    • G06F8/4435Detection or removal of dead or redundant code

Landscapes

  • Engineering & Computer Science (AREA)
  • General Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Software Systems (AREA)
  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Stored Programmes (AREA)

Abstract

The embodiment of the present application discloses the method and apparatus for detecting code.This method comprises: parsing code library to be detected with the call relation between the code element in determination code library to be detected;The complexity for determining the call relation between code element, it is candidate code element that the complexity for filtering out the call relation between other code elements in code library to be detected, which is no more than the code element of default complexity,;Dependence between code library to be detected and other code libraries is parsed;In response to determining that candidate code element not there are other code libraries of dependence to call with code library to be detected, determine that candidate code element is invalid code element.The method achieve the accurate detections of invalid code.

Description

Method and apparatus for detecting code
Technical field
The invention relates to field of computer technology, and in particular to technical field of data processing, more particularly, to The method and apparatus for detecting code.
Background technique
Invalid code is to be not involved in the code actually executed in non-annotated code or participate in executing the result still generated There is no the codes used, are the codes of redundancy.Invalid code is generally embedded in normal code, no image of Buddha annotated code Equally by programmer, Direct Recognition is come out from grammer.
With continuous exploitation, the code quantity in enterprise is more and more, to the maintenance of code need to consume it is huge at This.And invalid code can not only occupy machine resources and reduce properties of product, also will increase engineer and read the difficulty understood, make At the waste of resource.
Summary of the invention
Embodiment of the disclosure proposes method, apparatus, electronic equipment and computer-readable medium for detecting code.
In a first aspect, embodiment of the disclosure provides a kind of method for detecting code, comprising: parsing is to be detected Code library is with the call relation between the code element in determination code library to be detected;Determine that the calling between code element is closed The complexity of system, the complexity for filtering out the call relation between other code elements in code library to be detected are no more than The code element of default complexity is candidate code element;To the dependence between code library to be detected and other code libraries It is parsed;Other code library tune that there is no dependence with code library to be detected in response to determining candidate code element With, determine candidate code element be invalid code element.
In some embodiments, the complexity of the call relation between above-mentioned determining code element, comprising: for be detected Code library in a code element, whether had with other code elements in code library to be detected based on code element Call relation and with code element have call relation other code elements whether be candidate code element, determine code The complexity of call relation in element and code library to be detected between other code elements.
In some embodiments, the above-mentioned calling filtered out between other code elements in code library to be detected is closed The code element that the complexity of system is no more than default complexity is candidate code element, include at least one of the following: filter out with Other code elements in code library to be detected do not have the code element of call relation, as candidate code element;Screening Do not called by other code elements in code library to be detected out and be not the execution of the corresponding program of code library to be detected The code element of entrance, as candidate code element.
In some embodiments, the above-mentioned calling filtered out between other code elements in code library to be detected is closed The code element that the complexity of system is no more than default complexity is candidate code element, further includes: filters out fixed candidate Called code element of the code element that code element is called as candidate code element;In response to determining called code Element is not called by other code elements in addition to fixed candidate code element, determines that called code element is candidate Code element.
In some embodiments, above-mentioned parsing code library to be detected is with the code element in determination code library to be detected Between call relation, comprising: code library to be detected is parsed to obtain abstract syntax tree using code parser;It is based on Abstract syntax tree extracts the call relation between the code element in code library to be detected.
In some embodiments, the above method further include: in response to determine candidate code element by with code to be detected There are other code libraries of dependence to call in library, and in other code libraries for having dependence with code library to be detected All code elements for calling candidate code element are invalid code elements, determine that candidate code element is invalid code element.
Second aspect, embodiment of the disclosure provide a kind of for detecting the device of code, comprising: the first parsing is single Member is configured as parsing code library to be detected with the call relation between the code element in determination code library to be detected; Screening unit is configured to determine that the complexity of the call relation between code element, filter out in code library to be detected Other code elements between the complexity of call relation to be no more than the code element of default complexity be candidate code element; Second resolution unit is configured as parsing the dependence between code library to be detected and other code libraries;It determines Unit is configured to respond to determine other codes that candidate code element is not had dependence with code library to be detected Library is called, and determines that candidate code element is invalid code element.
In some embodiments, above-mentioned screening unit is configured as determining the calling between code element as follows The complexity of relationship: for a code element in code library to be detected, based on code element whether with generation to be detected Whether other code elements of other code elements with call relation and with code element with call relation in code library For candidate code element, the complexity of the call relation in code element and code library to be detected between other code elements is determined Degree.
In some embodiments, above-mentioned screening unit is configured as filtering out candidate code according to following at least one mode Element: the code element for not having call relation with other code elements in code library to be detected is filtered out, as candidate Code element;It filters out and is not called by other code elements in code library to be detected and be not code library pair to be detected The code element of the execution entrance for the program answered, as candidate code element.
In some embodiments, above-mentioned screening unit is additionally configured to filter out candidate code element as follows: Filter out the called code element of code element that fixed candidate code element is called as candidate code element;It rings It should not called by other code elements in addition to fixed candidate code element in determining called code element, determine quilt Calling code element is candidate code element.
In some embodiments, above-mentioned first resolution unit is configured as parsing code library to be detected as follows With the call relation between the code element in determination code library to be detected: using code parser to code library to be detected It is parsed to obtain abstract syntax tree;The tune between the code element in code library to be detected is extracted based on abstract syntax tree With relationship.
In some embodiments, above-mentioned determination unit be also configured in response to determine candidate code element by with it is to be checked The code library of survey have dependence other code libraries call, and with code library to be detected have dependence other It is invalid code element that all code elements of candidate code element are called in code library, determines that candidate code element is invalid generation Data code.
The third aspect, embodiment of the disclosure provide a kind of electronic equipment, comprising: one or more processors;Storage Device, for storing one or more programs, when one or more programs are executed by one or more processors so that one or Multiple processors realize the method for detecting code provided such as first aspect.
Fourth aspect, embodiment of the disclosure provide a kind of computer-readable medium, are stored thereon with computer program, Wherein, the method for detecting code that first aspect provides is realized when program is executed by processor.
Method and apparatus, electronic equipment and computer-readable Jie for being used to detect code of above-described embodiment of the disclosure Matter, by parsing code library to be detected with the call relation between the code element in determination code library to be detected, then The complexity for determining the call relation between code element filters out between other code elements in code library to be detected The complexity of call relation to be no more than the code element of default complexity be candidate code element;Then to code to be detected Dependence between library and other code libraries is parsed;Later in response to determine candidate code element not by with it is to be detected There are code library other code libraries of dependence to call, and determine that candidate code element is invalid code element, it is invalid to realize The accurate detection of code can provide reliable foundation for the automatic cleaning of code, to reduce code maintenance cost.
Detailed description of the invention
By reading a detailed description of non-restrictive embodiments in the light of the attached drawings below, the application's is other Feature, objects and advantages will become more apparent upon:
Fig. 1 is that embodiment of the disclosure can be applied to exemplary system architecture figure therein;
Fig. 2 is the flow chart according to one embodiment of the method for detecting code of the disclosure;
Fig. 3 is the process signal that candidate code element is filtered out in the method for detecting code according to the disclosure Figure;
Fig. 4 is the flow chart according to another embodiment of the method for detecting code of the disclosure;
Fig. 5 is the structural schematic diagram of one embodiment of the device for detecting code of the disclosure;
Fig. 6 is adapted for the structural schematic diagram for the computer system for realizing the electronic equipment of the embodiment of the present disclosure.
Specific embodiment
The application is described in further detail with reference to the accompanying drawings and examples.It is understood that this place is retouched The specific embodiment stated is used only for explaining related invention, rather than the restriction to the invention.It also should be noted that in order to Convenient for description, part relevant to related invention is illustrated only in attached drawing.
It should be noted that in the absence of conflict, the features in the embodiments and the embodiments of the present application can phase Mutually combination.The application is described in detail below with reference to the accompanying drawings and in conjunction with the embodiments.
Fig. 1 is shown can be using the application for detecting the method for code or the example of the device for detecting code Property system architecture 100.
As shown in Figure 1, system architecture 100 may include terminal device 101, network 102 and server 103.Network 102 To provide the medium of communication link between terminal device 101 and server 103.Network may include various connection types, Such as wired, wireless communication link or fiber optic cables etc..
User 110 can be used terminal device 101 and be interacted by network 102 with server 103, to receive or send message Deng.The application programs such as Software Development Tools, software management application, code administration application can be installed on terminal device 101.
Server 103 can be to provide the background server of code administration service.The available engineer of server 103 exists The code write in software development process, and code is stored in an orderly manner (such as according to affiliated product).Server 103 can be with Symbol lookup and management service are provided.User 110 can be code library administrative staff, and terminal device 101 can be used to clothes Business device 103 issues code administration instruction, such as issues the instruction that invalid code is detected and deleted to code.Server 103 It can be operated in response to the management for receiving the instruction and being responded to the code of storage, such as detect invalid code and clear up Invalid code.The result that server 103 can also manage progress to 101 return code of terminal and code administration operates.
It should be noted that above-mentioned terminal device 101 can be hardware, it is also possible to software.When terminal device 101 is hard When part, the various electronic equipments of operation code administration application, including but not limited to plate are can be with display screen and supported Computer, pocket computer on knee and desktop computer etc..When terminal device 101 is software, above-mentioned institute may be mounted at In the electronic equipment enumerated.Multiple softwares or software module may be implemented into (such as providing the multiple of Distributed Services in it Software or software module), single software or software module also may be implemented into.It is not specifically limited herein.
Server 103 can be hardware, be also possible to software.When server 103 is hardware, multiple clothes may be implemented into The distributed server cluster of business device composition, also may be implemented into individual server.When server 103 is software, Ke Yishi Ready-made multiple softwares or software module (such as providing multiple softwares of Distributed Services or software module), also may be implemented At single software or software module.It is not specifically limited herein.
Method provided by the embodiment of the present application for detecting code can be held by terminal device 101 or server 103 Row, correspondingly, the device for detecting code can be set in terminal device 101 or server 103.
It should be understood that the terminal device, network, the number of server in Fig. 1 are only schematical.According to realization need It wants, can have any number of terminal device, network, server.
With continued reference to Fig. 2, it illustrates the processes according to one embodiment of the method for detecting code of the application 200.The method for being used to detect code, comprising the following steps:
Step 201, code library to be detected is parsed with the calling between the code element in determination code library to be detected Relationship.
In the present embodiment, first may be used for detecting the executing subject (such as server shown in FIG. 1) of the method for code To obtain code library to be detected.Code library to be detected can be specified code library, or can will be in code database Each code library of storage executes code detection operation as code library to be detected.A usual code library, which can be, to be made With the set for the code that same programming language is write.In practice, code file tissue in a manner of code library, usual one Product or a project have one or more code libraries for realizing its function.The function that can be realized according to code is corresponding Code is stored in different code libraries by product or project.
Code in code library to be detected can then be parsed.Specifically language can be carried out based on the expression formula of code The parsing such as method, morphology, obtains the call relation in code library between code element.Wherein, code element is that have solely in code The element of vertical grammer meaning can include but is not limited at least one of following: function, variable, method, class.
Call relation between code element can be the relationship called and be called between each code element.Such as it can be with It is function and variable, class, the call relation between function, method and function, class, call relation between variable, etc..
In generation to be detected, can be parsed using the methods of morphological analysis, syntactic analysis, control flow analysis and data-flow analysis Code library is to extract the call relation between code element therein.By taking syntactic analysis as an example, usually every kind of programming language has pre- The normative grammar format of definition, the calling between code element follow scheduled calling grammer.It can be predetermined according to programming language Syntax format parse the call relation in code library, and record in call relation as the code element of caller and conduct The code element of callee.
It is alternatively possible to be parsed to obtain abstract syntax tree to code library to be detected using code parser, then The call relation between the code element in code library to be detected is extracted based on abstract syntax tree.
Different programming languages has corresponding code analysis tool, such as C Plus Plus can use LLVM (Low Level Virtual Machine, underlying virtual machine), the compiling such as GCC (GNU Compiler Collection, GNU compiler external member) Device parses code, and the code write in the eclipse platform based on Java language can be using the CDT (C/C++ of open source Development Tooling, C/C++ developing instrument) plug-in unit etc. carries out code analysis.
After parsing using code parser to code library to be detected, the pumping of code library to be detected can be obtained As syntax tree.Abstract syntax tree is a kind of abstract representation of source code syntax structure, and the grammer of code is indicated with tree Structure.Each node is each code element in abstract syntax tree.Abstract syntax tree is parsed again, available code Call relation between element.
It in abstract syntax tree, is connected with each other between two code elements with the relationship that calls directly, passes through parsing and take out As the connection relationship and arrow of the node in syntax tree are directed toward, can successively determine between code element call directly relationship and Indirect call relation.
Step 202, the complexity for determining the call relation between code element, filter out in code library to be detected The code element that the complexity of call relation between other code elements is no more than default complexity is candidate code element.
The calling between code element can be determined according to the call relation between the code element that step 201 parsing obtains The complexity of relationship.Herein, each code element can have answering for the call relation between corresponding code element Miscellaneous degree attribute.Whether the complexity and code element are called by other code elements, whether call other code elements, by other Whether the number of code element calling the number for calling other code elements, is that program executes the correlations such as entrance.
The stage division to complexity can be preset, the more high corresponding complexity of grade is higher.Such as code element There is no calling and when called relationship between other code elements, the complexity level of corresponding call relation is the Level-one or 0;When code element is not called by other code elements and is not that program executes entrance, corresponding call relation is answered Miscellaneous degree grade is 1;It is not invalid that code element, which calls and call other code elements of the code element by other code elements, When code element, corresponding call relation grade is 3, etc..
It may determine that whether the complexity of the corresponding call relation of each code element reaches preset complexity level, thus Determine whether to be no more than preset complexity.For example, preset complexity level is 1, then the complexity level of call relation is 0 Or 1 complexity of call relation of code element is no more than preset complexity, it can be using these code elements as candidate Code element.Herein, candidate code element is candidate invalid code element, needs to further determine by subsequent step It whether is invalid code element.
Optionally, other code elements that candidate code element is not determined in code library to be detected can be determined For non-invalid code element.
Step 203, the dependence between code library to be detected and other code libraries is parsed.
By the candidate code element that in the available code library of step 202 may be invalid code element.It can be with Further parse the dependence between different code library.Herein, the dependence between code library characterizes a code library It needs when being executed dependent on another code library namely a code library using the code element in another code library, such as One code library calls function or method in another code library when executing.
Dependence between code library can by read and parse the file stated comprising dependence between code library come It obtains.For example, in C Plus Plus, the dependence between code library is parsed by reading " makefile " file, in Java The dependence between code library is obtained by describing the field of dependence in parsing " pom.xml " file.
Step 204, in response to determine candidate code element not by with code library to be detected have dependence other Code library calls, and determines that candidate code element is invalid code element.
After parsing the dependence between code library, it can be determined that have the generation of dependence with code library to be detected Whether code library has invoked the candidate code element in code library to be detected.If above-mentioned candidate code element not by with generation to be detected There are other code libraries of either one or two of dependence to call in code library, can determine that candidate code element is invalid code element.
Invalid code element can be cleared up, it in this way can be to avoid execution invalid code element when running code library Lead to the wasting of resources, while Optimized code library, reduces engineer and read the difficulty understood, to reduce cost.
The above-mentioned method for detecting code, by parsing code library to be detected in determination code library to be detected Call relation between code element, it is later determined that the complexity of the call relation between code element, filter out with it is to be detected Code library in other code elements between the complexity of call relation to be no more than the code element of default complexity be to wait Select code element;Then the dependence between code library to be detected and other code libraries is parsed;Later in response to It determines that candidate code element not there are other code libraries of dependence to call with code library to be detected, determines candidate code Element is invalid code element, realizes the accurate detection of invalid code.Can for code it is automatic cleaning provide reliably according to According to reduce code maintenance cost.
Optionally, when step 202 in the process 200 for executing the above-mentioned method for detecting code, can for A code element in the code library of detection, it is whether first with other codes in code library to be detected based on the code element Element has whether other code elements of call relation are candidate code element with call relation and with the code element, Determine the complexity of the call relation in the code element and code library to be detected between other code elements.If the code is first Element has call relation with other code elements, and other code elements with the code element with call relation are not candidate Code element, then the complexity of the corresponding call relation of the code element is higher;If the code element and other code elements are not It (never calls other code elements with call relation and is not called by other code elements), or have with the code element and adjust Be confirmed as candidate code element with other code elements of relationship, the complexity of the corresponding call relation of the code element compared with It is low.
Further, with reference to Fig. 3, it illustrates filter out candidate generation in the method for detecting code according to the disclosure One flow diagram of data code, namely show the flow diagram of an optional implementation of above-mentioned steps 202.Such as Shown in Fig. 3, filter out candidate code process 300 the following steps are included:
Step 301, the code member for not having call relation with other code elements in code library to be detected is filtered out Element, as candidate code element.
Can successively judge whether the code element in code library to be detected is first with other codes in identical code library Element has call relation.If other code elements in code element never call identical code library and not by identical code library Other code elements call, then the code element is the element of " isolated " in code library to be detected, determines the code element The complexity for meeting the call relation between above-mentioned other code elements with code library to be detected is no more than default complexity The condition of degree, the i.e. code element may be invalid code element, which can be determined as to candidate code element.
Step 302, it filters out and is not called by other code elements in code library to be detected and be not generation to be detected The code element of the execution entrance of the corresponding program in code library, as candidate code element.
If code element is not called by other code elements in identical code library, also, code element is not program Entrance is executed, then when executing the code in code library, which will not be performed, it may be possible to invalid code element, it can To determine it as candidate code element.
The execution entrance of program is usually predetermined, such as main function.Under normal circumstances, as the execution of program The code element of entrance is not called by other code elements, by further judging whether code element is that the execution of program enters Mouthful, can be avoided the execution entrance erroneous judgement of program is invalid code.
It should be noted that in embodiment of the disclosure, it can be according to any one in step 301 and step 302 Screening mode filter out may be invalid code element candidate code element, can also be in conjunction with the two of step 301 and step 302 Kind of method screens candidate code element.
Optionally, after filtering out the part candidate code element in a code library, can also in the following way into One step filters out other candidate code elements:
Step 303, code element that fixed candidate code element is called is filtered out as candidate code element Called code element, in response to determining called code element not by other generations in addition to fixed candidate code element Data code calls, and determines that called code element is candidate code element.
The candidate code element filtered out in step 301 and step 302 is fixed candidate code element.Especially Ground, on the basis of step 302, if code element is not called by other code elements in code library to be detected and is not The code element of the execution entrance of the corresponding program of code library to be detected, by the code element be determined as candidate code element it Afterwards, the code element that can further call to candidate code element determining in this way is analyzed one by one.If should The code element that candidate code element calls is not called by other code elements, alternatively, optionally, if the candidate code element tune Code element is not called by other non-candidate code elements in code library to be detected, can be by the candidate code element The code element of calling is also determined as candidate code element.
If a code element is only deactivated code element calling, it is invalid generation which, which will not be performed, Data code.It is called relationship analysis by the code element further called to candidate code element, can be closed according to calling System quickly determine may be invalid code element candidate code element, thus realize more fully invalid code element examine It surveys.
With continued reference to Fig. 4, it illustrates the streams according to another embodiment of the method for detecting code of the disclosure Cheng Tu.As shown in figure 4, the process 400 of the method for detecting code of the present embodiment, comprising the following steps:
Step 401, code library to be detected is parsed with the calling between the code element in determination code library to be detected Relationship.
In the present embodiment, morphological analysis, syntactic analysis, control flow analysis sum number can be carried out to code library to be detected According to the parsing of the modes such as flow point analysis, the call relation between wherein code element is extracted.
It is alternatively possible to be parsed to obtain abstract syntax tree to code library to be detected using code parser, then The call relation between the code element in code library to be detected is extracted based on abstract syntax tree.
Step 402, the complexity for determining the call relation between code element, filter out in code library to be detected The code element that the complexity of call relation between other code elements is no more than default complexity is candidate code element.
May be based on whether to be called, whether call other code elements, whether be the execution entrance of program, call or by Whether the code element of calling is the indexs such as invalid code element to determine other in code element and code library to be detected The complexity of call relation between code element.It specifically can be according to These parameters in preset complexity level mapping relations In find the corresponding complexity level of code element.Then judge whether complexity level is to judge more than preset grade Whether complexity is more than preset complexity threshold.If complexity level is less than preset grade, it is determined that corresponding code Element is candidate code element.
It is alternatively possible to for a code element in code library to be detected, based on the code element whether with to Other code elements in the code library of detection have other generations of call relation with call relation and with the code element Whether data code is candidate code element, determines the tune in the code element and code library to be detected between other code elements With the complexity of relationship.
It is closed moreover can filter out and not have to call with other code elements in code library to be detected The code element of system, as candidate code element;And/or it filters out not by other code element tune in code library to be detected With and not be the corresponding program of code library to be detected execution entrance code element, as candidate code element.It can be with It filters out and is not called by other code elements in code library to be detected and be not the corresponding program of code library to be detected The code element for executing entrance, as candidate code element.
Step 403, the dependence between code library to be detected and other code libraries is parsed.
The available file for the dependence between coded description library, such as " makefile " file in C++, Or " pom.xml " file in Java, it is parsed to obtain between code library to be detected and other code libraries Dependence.
Step 404, in response to determine candidate code element not by with code library to be detected have dependence other Code library calls, and determines that candidate code element is invalid code element.
If above-mentioned candidate code element is not had other code libraries of either one or two of dependence with code library to be detected It calls, can determine that candidate code element is invalid code element.
The step 401 of the present embodiment, step 402, step 403, step 404 respectively with the step 201 of previous embodiment, step Rapid 202, step 203, step 204 are consistent, and step 401, step 402, step 403, the specific embodiment of step 404 can be with The description in previous embodiment to step 201, step 202, step 203, the specific embodiment of step 204 is referred to respectively, this Place repeats no more.
Step 405, other generations that there is dependence with code library to be detected in response to determining candidate code element Code library is called, and all of candidate code element are called in other code libraries for having dependence with code library to be detected Code element is invalid code element, determines that candidate code element is invalid code element.
In the present embodiment, time will can be called with being had other code libraries of dependence with code library to be detected It selects code element as target candidate code element, is further detected.It may determine that in above-mentioned and code to be detected Library has whether the code element of invocation target candidate code element in other code libraries of dependence is invalid code element. If so, the target candidate code element for being deactivated code element calling also can be determined that invalid code element.
In this way, can between different code library there are call relation but code element as caller It is the invalid code element detected in the case where invalid code element as callee, further decreases invalid code element Omission factor, promoted invalid code detection accuracy.
Optionally, if other code library tune that candidate code element is had dependence with code library to be detected With, and at least one generation with calling candidate code element in other code libraries of code library to be detected with dependence Data code is not invalid code element, and can determine candidate code element not is invalid code element.In this case, although generation Data code is not called by other effective code elements in identical code library, but by effective code in other code libraries Element calls, then the candidate code element can be performed at runtime, be not belonging to invalid code element.
With further reference to Fig. 5, as the realization to method shown in above-mentioned each figure, this application provides one kind for detecting generation One embodiment of the device of code, the Installation practice is corresponding with Fig. 2 and embodiment of the method shown in Fig. 4, and the device is specific It can be applied in various electronic equipments.
As shown in figure 5, the device 500 for detecting code of the present embodiment includes: the first resolution unit 501, screening list First 502, second resolution unit 503 and determination unit 504.Wherein, the first resolution unit 501 is configured as parsing to be detected Code library is with the call relation between the code element in determination code library to be detected;Screening unit 502 is configured to determine that The complexity of call relation between code element filters out the tune between other code elements in code library to be detected The code element for being no more than default complexity with the complexity of relationship is candidate code element;Second resolution unit 503 is configured To be parsed to the dependence between code library to be detected and other code libraries;Determination unit 504 is configured to respond to In determining candidate code element there is no other code libraries calling of dependence with code library to be detected, determines candidate's generation Data code is invalid code element.
In some embodiments, above-mentioned screening unit 502 is configured as determining between code element as follows The complexity of call relation: for a code element in code library to be detected, based on code element whether with it is to be detected Code library in other code elements with call relation and with code element have call relation other code elements Whether it is candidate code element, determines the call relation in code element and code library to be detected between other code elements Complexity.
In some embodiments, above-mentioned screening unit 502 is configured as filtering out candidate according to following at least one mode Code element: filtering out the code element for not having call relation with other code elements in code library to be detected, as Candidate code element;It filters out and is not called by other code elements in code library to be detected and be not code to be detected The code element of the execution entrance of the corresponding program in library, as candidate code element.
In some embodiments, above-mentioned screening unit 502 is additionally configured to filter out candidate code member as follows Element: it is first as the called code of candidate code element to filter out the code element that fixed candidate code element is called Element;In response to determining that called code element is not called by other code elements in addition to fixed candidate code element, Determine that called code element is candidate code element.
In some embodiments, above-mentioned first resolution unit 501 is configured as parsing generation to be detected as follows Code library is with the call relation between the code element in determination code library to be detected: using code parser to generation to be detected Code library is parsed to obtain abstract syntax tree;It is extracted based on abstract syntax tree between the code element in code library to be detected Call relation.
In some embodiments, above-mentioned determination unit 504 be also configured in response to determine candidate code element by with to The code library of detection have dependence other code libraries call, and with code library to be detected have dependence its It is invalid code element that all code elements of candidate code element are called in his code library, determines that candidate code element is invalid Code element.
It should be appreciated that all units recorded in device 500 and each step phase in the method described with reference to Fig. 2 and Fig. 4 It is corresponding.It is equally applicable to device 500 and unit wherein included above with respect to the operation and feature of method description as a result, herein It repeats no more.
The device 500 for being used to detect code of the above embodiments of the present application, by parsing code library to be detected with determination The call relation between code element in code library to be detected, it is later determined that the complexity of the call relation between code element Degree, the complexity for filtering out the call relation between other code elements in code library to be detected are no more than default complexity The code element of degree is candidate code element;Then the dependence between code library to be detected and other code libraries is carried out Parsing;Other code library tune that there is no dependence with code library to be detected in response to determining candidate code element later With, determine candidate code element be invalid code element, realize the accurate detection of invalid code.
Below with reference to Fig. 6, it illustrates the electronic equipment that is suitable for being used to realize embodiment of the disclosure, (example is as shown in figure 1 Server) 600 structural schematic diagram.Electronic equipment shown in Fig. 6 is only an example, should not be to embodiment of the disclosure Function and use scope bring any restrictions.
As shown in fig. 6, electronic equipment 600 may include processing unit (such as central processing unit, graphics processor etc.) 601, random access can be loaded into according to the program being stored in read-only memory (ROM) 602 or from storage device 608 Program in memory (RAM) 603 and execute various movements appropriate and processing.In RAM 603, it is also stored with electronic equipment Various programs and data needed for 600 operations.Processing unit 601, ROM 602 and RAM 603 pass through the phase each other of bus 604 Even.Input/output (I/O) interface 605 is also connected to bus 604.
In general, following device can connect to I/O interface 605: including such as touch screen, touch tablet, keyboard, mouse, taking the photograph As the input unit 606 of head, microphone, accelerometer, gyroscope etc.;Including such as liquid crystal display (LCD), loudspeaker, vibration The output device 607 of dynamic device etc.;Storage device 608 including such as hard disk etc.;And communication device 609.Communication device 609 can To allow electronic equipment 600 wirelessly or non-wirelessly to be communicated with other equipment to exchange data.Although Fig. 6 is shown with various The electronic equipment 600 of device, it should be understood that being not required for implementing or having all devices shown.It can be alternatively Implement or have more or fewer devices.Each box shown in Fig. 6 can represent a device, also can according to need Represent multiple devices.
Particularly, in accordance with an embodiment of the present disclosure, it may be implemented as computer above with reference to the process of flow chart description Software program.For example, embodiment of the disclosure includes a kind of computer program product comprising be carried on computer-readable medium On computer program, which includes the program code for method shown in execution flow chart.In such reality It applies in example, which can be downloaded and installed from network by communication device 609, or from storage device 608 It is mounted, or is mounted from ROM 602.When the computer program is executed by processing unit 601, the implementation of the disclosure is executed The above-mentioned function of being limited in the method for example.It should be noted that computer-readable medium described in embodiment of the disclosure can To be computer-readable signal media or computer readable storage medium either the two any combination.Computer can Reading storage medium for example may be-but not limited to-the system of electricity, magnetic, optical, electromagnetic, infrared ray or semiconductor, device Or device, or any above combination.The more specific example of computer readable storage medium can include but is not limited to: tool There are electrical connection, the portable computer diskette, hard disk, random access storage device (RAM), read-only memory of one or more conducting wires (ROM), erasable programmable read only memory (EPROM or flash memory), optical fiber, portable compact disc read-only memory (CD- ROM), light storage device, magnetic memory device or above-mentioned any appropriate combination.In embodiment of the disclosure, computer Readable storage medium storing program for executing can be any tangible medium for including or store program, which can be commanded execution system, device Either device use or in connection.And in embodiment of the disclosure, computer-readable signal media may include In a base band or as the data-signal that carrier wave a part is propagated, wherein carrying computer-readable program code.It is this The data-signal of propagation can take various forms, including but not limited to electromagnetic signal, optical signal or above-mentioned any appropriate Combination.Computer-readable signal media can also be any computer-readable medium other than computer readable storage medium, should Computer-readable signal media can send, propagate or transmit for by instruction execution system, device or device use or Person's program in connection.The program code for including on computer-readable medium can transmit with any suitable medium, Including but not limited to: electric wire, optical cable, RF (radio frequency) etc. or above-mentioned any appropriate combination.
Above-mentioned computer-readable medium can be included in above-mentioned electronic equipment;It is also possible to individualism, and not It is fitted into the electronic equipment.Above-mentioned computer-readable medium carries one or more program, when said one or more When a program is executed by the electronic equipment, so that the electronic equipment: parsing code library to be detected with determination code to be detected The call relation between code element in library;The complexity for determining the call relation between code element, filter out with it is to be checked The code element that the complexity of the call relation between other code elements in the code library of survey is no more than default complexity is Candidate code element;Dependence between code library to be detected and other code libraries is parsed;It is waited in response to determining It selects code element not there are other code libraries of dependence to call with code library to be detected, determines that candidate code element is Invalid code element.
The behaviour for executing embodiment of the disclosure can be write with one or more programming languages or combinations thereof The computer program code of work, programming language include object oriented program language-such as Java, Smalltalk, C++ further include conventional procedural programming language-such as " C " language or similar program design language Speech.Program code can be executed fully on the user computer, partly be executed on the user computer, as an independence Software package execute, part on the user computer part execute on the remote computer or completely in remote computer or It is executed on server.In situations involving remote computers, remote computer can pass through the network of any kind --- packet Include local area network (LAN) or wide area network (WAN) --- it is connected to subscriber computer, or, it may be connected to outer computer (such as It is connected using ISP by internet).
Flow chart and block diagram in attached drawing are illustrated according to the system of the various embodiments of the application, method and computer journey The architecture, function and operation in the cards of sequence product.In this regard, each box in flowchart or block diagram can generation A part of one module, program segment or code of table, a part of the module, program segment or code include one or more use The executable instruction of the logic function as defined in realizing.It should also be noted that in some implementations as replacements, being marked in box The function of note can also occur in a different order than that indicated in the drawings.For example, two boxes succeedingly indicated are actually It can be basically executed in parallel, they can also be executed in the opposite order sometimes, and this depends on the function involved.Also it to infuse Meaning, the combination of each box in block diagram and or flow chart and the box in block diagram and or flow chart can be with holding The dedicated hardware based system of functions or operations as defined in row is realized, or can use specialized hardware and computer instruction Combination realize.
Being described in unit involved in the embodiment of the present application can be realized by way of software, can also be by hard The mode of part is realized.Described unit also can be set in the processor, for example, can be described as: a kind of processor packet Include the first resolution unit, screening unit, the second resolution unit and determination unit.Wherein, the title of these units is in certain situation Under do not constitute restriction to the unit itself, for example, the first resolution unit is also described as " parsing code to be detected Library is with the unit of the call relation between the code element in determination code library to be detected ".
Above description is only the preferred embodiment of the application and the explanation to institute's application technology principle.Those skilled in the art Member is it should be appreciated that invention scope involved in the application, however it is not limited to technology made of the specific combination of above-mentioned technical characteristic Scheme, while should also cover in the case where not departing from foregoing invention design, it is carried out by above-mentioned technical characteristic or its equivalent feature Any combination and the other technical solutions formed.Such as features described above has similar function with (but being not limited to) disclosed herein Can technical characteristic replaced mutually and the technical solution that is formed.

Claims (14)

1. a kind of method for detecting code, comprising:
Code library to be detected is parsed with the call relation between the code element in the determination code library to be detected;
The complexity for determining the call relation between the code element filters out and other in the code library to be detected The code element that the complexity of call relation between code element is no more than default complexity is candidate code element;
Dependence between the code library to be detected and other code libraries is parsed;
Other codes that there is no dependence with the code library to be detected in response to the determination candidate code element Library is called, and determines that the candidate code element is invalid code element.
2. according to the method described in claim 1, wherein, the complexity of the call relation between the determination code element Degree, comprising:
For a code element in the code library to be detected, based on the code element whether with it is described to be detected Other code elements in code library have other codes member of call relation with call relation and with the code element Whether element is candidate code element, determines the calling in the code element and code library to be detected between other code elements The complexity of relationship.
3. described to filter out and other codes in the code library to be detected according to the method described in claim 2, wherein The code element that the complexity of call relation between element is no more than default complexity is candidate code element, including with down toward One item missing:
The code element for not having call relation with other code elements in the code library to be detected is filtered out, as time Select code element;
It filters out and is not called by other code elements in the code library to be detected and be not the code library to be detected The code element of the execution entrance of corresponding program, as candidate code element.
4. described to filter out and other codes in the code library to be detected according to the method described in claim 3, wherein The code element that the complexity of call relation between element is no more than default complexity is candidate code element, further includes:
Filter out the called generation of code element that fixed candidate code element is called as the candidate code element Data code;
In response to the determination called code element not by other codes in addition to the fixed candidate code element Element calls, and determines that the called code element is candidate code element.
5. according to the method described in claim 1, wherein, the parsing code library to be detected is with the determination generation to be detected The call relation between code element in code library, comprising:
The code library to be detected is parsed to obtain abstract syntax tree using code parser;
The call relation between the code element in the code library to be detected is extracted based on the abstract syntax tree.
6. method according to claim 1-5, wherein the method also includes:
Other code libraries that there is dependence with the code library to be detected in response to the determination candidate code element It calls, and calls the candidate code element in other code libraries that there is dependence with the code library to be detected All code elements are invalid code elements, determine that the candidate code element is invalid code element.
7. a kind of for detecting the device of code, comprising:
First resolution unit is configured as parsing code library to be detected with the code member in the determination code library to be detected Call relation between element;
Screening unit is configured to determine that the complexity of the call relation between the code element, filter out with it is described to be checked The code element that the complexity of the call relation between other code elements in the code library of survey is no more than default complexity is Candidate code element;
Second resolution unit is configured as solving the dependence between the code library to be detected and other code libraries Analysis;
Determination unit, be configured to respond to determine the candidate code element not by with the code library to be detected have according to Other code libraries for the relationship of relying call, and determine that the candidate code element is invalid code element.
8. device according to claim 7, wherein the screening unit is configured as determining the generation as follows The complexity of call relation between data code:
For a code element in the code library to be detected, based on the code element whether with it is described to be detected Other code elements in code library have other codes member of call relation with call relation and with the code element Whether element is candidate code element, determines the calling in the code element and code library to be detected between other code elements The complexity of relationship.
9. device according to claim 8, wherein the screening unit is configured as sieving according to following at least one mode Select candidate code element:
The code element for not having call relation with other code elements in the code library to be detected is filtered out, as time Select code element;
It filters out and is not called by other code elements in the code library to be detected and be not the code library to be detected The code element of the execution entrance of corresponding program, as candidate code element.
10. device according to claim 9, wherein the screening unit is additionally configured to filter out as follows Candidate code element:
Filter out the called generation of code element that fixed candidate code element is called as the candidate code element Data code;
In response to the determination called code element not by other codes in addition to the fixed candidate code element Element calls, and determines that the called code element is candidate code element.
11. device according to claim 7, wherein first resolution unit is configured as parsing as follows Code library to be detected is with the call relation between the code element in the determination code library to be detected:
The code library to be detected is parsed to obtain abstract syntax tree using code parser;
The call relation between the code element in the code library to be detected is extracted based on the abstract syntax tree.
12. according to the described in any item devices of claim 7-11, wherein the determination unit is also configured to
Other code libraries that there is dependence with the code library to be detected in response to the determination candidate code element It calls, and calls the candidate code element in other code libraries that there is dependence with the code library to be detected All code elements are invalid code elements, determine that the candidate code element is invalid code element.
13. a kind of electronic equipment, comprising:
One or more processors;
Storage device, for storing one or more programs,
When one or more of programs are executed by one or more of processors, so that one or more of processors are real Now such as method as claimed in any one of claims 1 to 6.
14. a kind of computer-readable medium, is stored thereon with computer program, wherein real when described program is executed by processor Now such as method as claimed in any one of claims 1 to 6.
CN201910584002.2A 2019-07-01 2019-07-01 Method and apparatus for detecting code Active CN110297639B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910584002.2A CN110297639B (en) 2019-07-01 2019-07-01 Method and apparatus for detecting code

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910584002.2A CN110297639B (en) 2019-07-01 2019-07-01 Method and apparatus for detecting code

Publications (2)

Publication Number Publication Date
CN110297639A true CN110297639A (en) 2019-10-01
CN110297639B CN110297639B (en) 2023-03-21

Family

ID=68029670

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910584002.2A Active CN110297639B (en) 2019-07-01 2019-07-01 Method and apparatus for detecting code

Country Status (1)

Country Link
CN (1) CN110297639B (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112732335A (en) * 2021-01-12 2021-04-30 平安资产管理有限责任公司 Object code extraction method, device, computer equipment and storage medium
CN112732335B (en) * 2021-01-12 2024-05-31 平安资产管理有限责任公司 Object code extraction method, device, computer equipment and storage medium

Citations (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6029005A (en) * 1997-04-01 2000-02-22 Intel Corporation Method for identifying partial redundancies in a new processor architecture
CN101164062A (en) * 2005-01-13 2008-04-16 Hsbc北美控股有限公司 Framework for group systems software configuration and release management
CN101976318A (en) * 2010-11-15 2011-02-16 北京理工大学 Detection method of code similarity based on digital fingerprints
CN102231134A (en) * 2011-07-29 2011-11-02 哈尔滨工业大学 Method for detecting redundant code defects based on static analysis
CN105930162A (en) * 2016-04-24 2016-09-07 复旦大学 Subgraph search-based feature location method
CN106021116A (en) * 2016-06-07 2016-10-12 北京信息科技大学 Inaccessible function call path detection method in complex system
CN106294139A (en) * 2016-08-02 2017-01-04 上海理工大学 A kind of Detection and Extraction method of repeated fragment in software code
CN108549538A (en) * 2018-04-11 2018-09-18 深圳市腾讯网络信息技术有限公司 A kind of code detection method, device, storage medium and test terminal
CN108614707A (en) * 2018-04-27 2018-10-02 深圳市腾讯网络信息技术有限公司 Static code inspection method, device, storage medium and computer equipment
CN108776643A (en) * 2018-06-04 2018-11-09 腾讯科技(武汉)有限公司 A kind of object code merging control method and system based on Version Control flow
US10133557B1 (en) * 2013-01-11 2018-11-20 Mentor Graphics Corporation Modifying code to reduce redundant or unnecessary power usage

Patent Citations (11)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US6029005A (en) * 1997-04-01 2000-02-22 Intel Corporation Method for identifying partial redundancies in a new processor architecture
CN101164062A (en) * 2005-01-13 2008-04-16 Hsbc北美控股有限公司 Framework for group systems software configuration and release management
CN101976318A (en) * 2010-11-15 2011-02-16 北京理工大学 Detection method of code similarity based on digital fingerprints
CN102231134A (en) * 2011-07-29 2011-11-02 哈尔滨工业大学 Method for detecting redundant code defects based on static analysis
US10133557B1 (en) * 2013-01-11 2018-11-20 Mentor Graphics Corporation Modifying code to reduce redundant or unnecessary power usage
CN105930162A (en) * 2016-04-24 2016-09-07 复旦大学 Subgraph search-based feature location method
CN106021116A (en) * 2016-06-07 2016-10-12 北京信息科技大学 Inaccessible function call path detection method in complex system
CN106294139A (en) * 2016-08-02 2017-01-04 上海理工大学 A kind of Detection and Extraction method of repeated fragment in software code
CN108549538A (en) * 2018-04-11 2018-09-18 深圳市腾讯网络信息技术有限公司 A kind of code detection method, device, storage medium and test terminal
CN108614707A (en) * 2018-04-27 2018-10-02 深圳市腾讯网络信息技术有限公司 Static code inspection method, device, storage medium and computer equipment
CN108776643A (en) * 2018-06-04 2018-11-09 腾讯科技(武汉)有限公司 A kind of object code merging control method and system based on Version Control flow

Non-Patent Citations (2)

* Cited by examiner, † Cited by third party
Title
LUIS-J. SAIZ-ADALID: "MCU Tolerance in SRAMs Through Low-Redundancy Triple Adjacent Error Correction" *
方登辉: "基于抽象语法树的代码静态缺陷检测工具开发" *

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112732335A (en) * 2021-01-12 2021-04-30 平安资产管理有限责任公司 Object code extraction method, device, computer equipment and storage medium
CN112732335B (en) * 2021-01-12 2024-05-31 平安资产管理有限责任公司 Object code extraction method, device, computer equipment and storage medium

Also Published As

Publication number Publication date
CN110297639B (en) 2023-03-21

Similar Documents

Publication Publication Date Title
CN110365751B (en) Service processing method, device and equipment of gateway system
CN102279765A (en) Pre-compiling hosted managed code
CN108958992A (en) test method and device
CN111506900B (en) Vulnerability detection method and device, electronic equipment and computer storage medium
CN110300082A (en) A kind of interface creation method, device and storage medium
CN109815105A (en) Applied program testing method and device based on Btrace
CN114238948A (en) Application program detection method and device, electronic equipment and storage medium
CN101980546B (en) Intelligent network platform, service execution method and method for analyzing service abnormality
CN111488573A (en) Link library detection method and device, electronic equipment and computer readable storage medium
CN115826972A (en) Face recognition method and device, computer equipment and storage medium
CN113010892B (en) Method and device for detecting malicious behavior of small program
CN110188366A (en) A kind of information processing method, device and storage medium
CN113391811B (en) Function compiling method, function compiling device, electronic equipment and computer readable storage medium
US9703576B2 (en) Aspect scoping in a modularity runtime
CN110297639A (en) Method and apparatus for detecting code
CN110348226A (en) A kind of scan method of project file, device, electronic equipment and storage medium
CN109189591A (en) Method and apparatus for calling application component
CN110427190A (en) The recognition methods and its device of application program, electronic equipment and medium
CN109947423A (en) Code generating method, device, terminal and medium
CN110262841A (en) The method and apparatus that a kind of pair of bytecode is enhanced
CN109117139A (en) A kind of software building method and device, server and readable storage medium storing program for executing
US8904002B2 (en) Reducing overhead in component interactions
KR100948472B1 (en) Automatic updating of variables in a data language
CN111796865B (en) Byte code file modification method, device, terminal equipment and medium
KR100862937B1 (en) Method and Apparatus for Reengineering Java Library for Embedded System

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant