Abstract:
Embodiments of the present invention provide a method and system for finding uninitialized variables outside the local scope. Initially, a source code of a program is received. The source code may include multiple functions. A checked scope is determined for at least one variable in the source code. The checked scope includes a local scope. Additionally, the local scope is accessed using at least one function call of the functions. Static analysis is run, covering the checked scope for the at least one variable in the source code. At least one inter-procedural dependency is identified to determine whether the at least one variable in the local scope is uninitialized. Responsive to determining that the at least one variable in the local scope is uninitialized the at least one variable is displayed to a user.

Description:
BACKGROUND OF THE INVENTION 
       [0001]    The present invention relates generally to the field of computing and, in particular, to finding uninitialized variables which are not local to the scope where they are used to identify the possibility of a programming ‘bug’. 
         [0002]    In programming, an uninitialized variable is a variable that is declared in a programming code, but lacks a definite known value prior to being used. As such, an uninitialized variable will have some value but not a predictable one. Therefore, uninitialized variables often create programming errors as they are a common source of bugs in software. It is extremely difficult to determine manually if a variable always gets its value prior to use. 
       SUMMARY 
       [0003]    According to one embodiment of the present invention, a method is provided, comprising: receiving, by one or more processors, a source code of a program, wherein the source code of the program comprises a plurality of functions; determining, by one or more processors, a checked scope for at least one variable in the source code, wherein the checked scope comprises a local scope, and wherein the local scope is accessed using at least one function call of the plurality of functions; running, by one or more processors, a static analysis covering the checked scope for the at least one variable in the source code; identifying by one or more processors, at least one inter-procedural dependency, wherein the at least one inter-procedural dependency comprises determining whether the at least one variable in the local scope is uninitialized; and responsive to determining that the at least one variable in the local scope is uninitialized, displaying, by one or more processors, the at least one variable to a user. 
         [0004]    Another embodiment of the present invention provides a computer program product for finding uninitialized variables, based on the method described above. 
         [0005]    Another embodiment of the present invention provides a computer system for finding uninitialized variables, based on the method described above. 
     
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
         [0006]      FIG. 1  is a functional block diagram illustrating a computing device, in accordance with an embodiment of the present invention; 
           [0007]      FIG. 2  is a flowchart illustrating operational steps for finding uninitialized variables which are not local to the scope where they are used, in accordance with an embodiment of the present invention; 
           [0008]      FIGS. 3A-B  are flowcharts illustrating detailed exemplary operational steps for finding uninitialized variables, in accordance with an embodiment of the present invention; and 
           [0009]      FIG. 4  is a block diagram of internal and external components of a computer device, such as the computing device of  FIG. 1 , in accordance with an embodiment of the present invention. 
       
    
    
     DETAILED DESCRIPTION 
       [0010]    Compilers may detect a large variety of programming errors and inform the programmer of any such errors and/or warnings. This provides the programmer an ability to troubleshoot and fix any such found error. Therefore, any error a compiler detects, becomes a non-issue if properly handled and debugged, prior to the program leaving development. 
         [0011]    It is important to pinpoint any such programming errors to avoid an issue in running the program, a potential crash, corrupt data and/or incorrect outputs. Yet, there are such errors that a compiler may not detect. Typically, as the size of a program increases, its complexity escalates often obscuring various such errors (commonly referred to as ‘bugs’), due to the intricacies of its internal logic and the large number of paths of execution. Such intricacies may cause a bug to only intermittently appear making such bugs difficult to track down and debug. The present invention involves a method to help identify one such issue, uninitialized variables. 
         [0012]    Generally, a variable in the context of computer programming is typically a storage location paired with an associated symbolic name (also known as an ‘identifier’). The identifier references the stored source code and may be bound to a value during run time. As such, the value of the variable may change during the course of the program execution. 
         [0013]    For some programming languages, when a variable is declared, it is often mistakenly assumed that the variable is set to a known value, i.e., ‘0’. Rather, the variable has no known value. For example, in C++, memory allocated to a variable is not cleared or zeroed upon allocation. There are many cases where a variable scope is not local to the scope where they are used, e.g., fields in classes, static variables, global variables, local variables in JavaScript® may access inside nested functions, JavaScript® closure, etc. 
         [0014]    Consequently, such a variable is known as an uninitialized variable, as the variable will have some value, but not a predictable one. As such, an uninitialized variable is a variable that is declared but is not set to a definite, known value prior to being run. Furthermore, the value of an uninitialized variable may change over each iteration a program is run or executed. Therefore, an uninitialized variable in some of the execution paths lacks a set value; its value exists, but its value is unpredictable. Further, a variable&#39;s dependency which reaches across procedure boundaries further complicates the determination of whether the variable is initialized. 
         [0015]    The present invention will now be described in detail with reference to the Figures.  FIG. 1  is a functional block diagram illustrating a computing device  110 , in accordance with an embodiment of the present invention.  FIG. 1  provides only an illustration of one embodiment and does not imply any limitations with regard to the environments in which different embodiments may be implemented. Many modifications to the depicted environment may be made by those skilled in the art without departing from the scope of the invention, as recited by the claims. 
         [0016]    In various embodiments of the present invention, computing device  110  may be a laptop computer, a tablet computer, a netbook computer, a personal computer (PC) a desktop computer, a personal digital assistant (PDA), a smart phone, or any programmable electronic device capable of executing computer readable program instructions. Computing device  110  may be a server including a management server, a web server or any other electronic device or computing system capable of receiving and sending data. 
         [0017]    Computing device  110  includes user interface (UI)  122 , dominator graph builder  124 , source program files  126  and uninitialized variable finder program (hereafter “UVF program”)  128 . In various embodiments of the present invention, one or more of dominator graph builder  124 , source program files  126  and UVF program  128  may be included. Computing device  110  may include internal and external components, as depicted and described in further detail with respect to  FIG. 4 . 
         [0018]    UI  122  may be, for example, a graphical user interface (GUI) or a web user interface (WUI), and can display text, documents, web browser windows user options, applications interfaces and instructions for operation. 
         [0019]    Dominator graph builder  124  determines dominance relationships in the inter-procedural case. Inter-procedural dominance relationships may be portrayed in the form of a graph, where each path represents a dominance relationship. Generally, an inter-procedural dominance graph traces all dominators for a special set of nodes, for which new inter-procedural dominators can be discovered. One skilled in the art, will appreciate that any method for computing a dominator graph may be utilized. 
         [0020]    Source program files  126  are one or more computer language files, and may include expressions that perform assignment, arithmetic, and comparison, among other functions. In an embodiment of the present invention, source program files  126  are written in C++ programming language. In an alternative embodiment of the present invention, source program files  126  are written in JavaScript® programming language. In other embodiments of the present invention, source program files  126  may be source program file(s) written in any suitable programming language. 
         [0021]    UVF program  128  operates to examine the source program files  126  as well as a dominator graph produced by dominator graph builder  124  to derive uninitialized variables. In an embodiment of the present invention, UVF program  128  supports automated program execution, so that a program may run using a series of scripted commands. In an embodiment, the output from UVF program  128  may be a file, e.g., a log file. In an embodiment, the output from UVF program  128  may be presented on a graphical user interface, such as a display screen. For instance, the output displayed on a graphical user interface may be in a graphical form. Additional outputs may be utilized by those skilled in the art. 
         [0022]    Reference is now made to  FIG. 2 .  FIG. 2  is a flow chart  200  illustrating operational steps for finding uninitialized variables which are not local to the scope where they are used, in accordance with an embodiment of the present invention. 
         [0023]    In step  210 , UVF program  128  determines the checked scope for at least one variable. For every variable selected, to determine if the variable is initialized or uninitialized, the scope of the variable must be defined. This is known as a ‘checked scope’. 
         [0024]    In an embodiment of the present invention, UVF program  128  automatically decides which variables to test in order to determine if the variable is initialized or uninitialized. Alternatively, UVF program  128  may allow a user to select various variables to test, in order to determine if the variable is initialized or uninitialized. 
         [0025]    It is noted that the checked scope is source language dependent. Scope is a sub program of the original program, where the variable does not change its behavior. For example, scope can be found automatically, by slicing from the variable use nodes. In another example, scope can be found by automatically defining the scope for variables of the certain kind, to the entire scope, where they can be used. For instance, C static variable ‘checked scope’ may be the file scope. It is noted that the ‘checked scope’ can just be defined by the user. 
         [0026]    For example, in JavaScript® the checked scope may be the top checked function. In another example, for Java®, C++ is a class scope, when class fields are accessed. In such a case, there can be a method that uses fields which are not initialized by the constrictor. This list is not intended to be limiting rather it is used for exemplary purposes only. 
         [0027]    For example, if a programmer of a large program wants to check if variable ‘X’ is initialized or not, the programmer must define the scope of the variable. The programmer may statically review the entire program&#39;s code for any and all locations that variable ‘X’ is present. If, for example, variable ‘X’ is located in two functions, Function H and Function G, then the programmer will locate the entry of the program, MAIN. Therefore, the scope of the variable ‘X’ contains three functions: Function G, Function H and MAIN. 
         [0028]    In step  220 , UVF program  128 , utilizing dominator graph builder  124 , builds inter-procedural dominance relationships, based on a variable(s) checked scope of step  210 . The building of a dominator graph may use any algorithm known in the art. Generally, building a dominator graph includes analyzing the source program file(s)  126 , (the program code at issue) for dominance relationships between the functions of the software program. 
         [0029]    As used herein, the term “dominance”, as used in an inter-procedural case considers only paths that preserve the semantics of function calls, which cannot be nested improperly, for example, if a function F calls (directly or indirectly) a function G, then F cannot return before G has returned. Function calls and returns may therefore be paired on a valid inter-procedural path so that they nest properly. In addition, any valid path from the initial node may have unmatched calls, but may not have unmatched returns. 
         [0030]    Similarly, the phrase “inter-procedural dominance candidates” means: a node v is an inter-procedural dominance candidate if: v is a function entry with more than one predecessor in the inter-procedural control-flow graph, and v is in the dominance frontier of a resume node or v is in the dominance frontier of another inter-procedural dominance candidate. 
         [0031]    Therefore, utilizing dominator graph builder  124 , a dominator graph is built covering the checked scope. Those skilled in the art will appreciate the numerous methods in the art which may be used to build the dominator graph for the checked scope, 
         [0032]    In step  230 , UVF program  128 , utilizing an additional algorithm, determines the uninitialized variables via the inter-procedural data flow. The algorithm (as further described in  FIG. 3A  and  FIG. 3B ) is run on top of the dominator graph. 
         [0033]    For example, the dominator graph is used as an input to the algorithm of UVF program  128 . Thus, for each node that uses a specific variable, the algorithm backward traverses the edges and checks if the graph contains the specific variables definition. If, for example, the algorithm finds a node that redefines the variables value (i.e., X=Y+1), then it continues with the new variable (i.e., Y) instead of the previous variable (i.e., X). 
         [0034]    Therefore, using the inter-procedural data flow information and the inter-procedural dominator information, UVF program  128  checks every use of the selected variable to determine if the selected variable contains an ancestor node which defines the variable. If the selected variable contains an ancestor node, then the variable is defined. Alternatively, if the selected variable does not contain an ancestor node, then the variable is not defined, as it is known as an uninitialized variable. Generally, inter-procedural dependencies are dependencies that exist in source code due to interactions of prior procedures and functions. 
         [0035]    The aforementioned process may be repeated for every variable selected, to determine if the variable is initialized or uninitialized. 
         [0036]    In step  240 , any and all uninitialized variables found are presented to the user. Alternatively, any and all uninitialized variables found are saved in a file for the user. 
         [0037]    An illustration of the operational steps of UVF program  128  is presented in  FIGS. 3A and 3B .  FIGS. 3A and 3B  are flowcharts illustrating detailed exemplary operational steps for finding uninitialized variables outside the local scope, in accordance with an embodiment of the present invention. Flowchart  300 A, as illustrated in  FIG. 3A  and flowchart  300 B, as illustrated in  FIG. 3B , depict an exemplary process to find uninitialized variables which are not local to the scope where they are used. Specifically, flowchart  300 A and  300 B, together, illustrate an algorithm as utilized in step  230  of  FIG. 2 . 
         [0038]    In step  302 , UVF program  128 , is activated. Exemplary function variables, ‘var’, ‘graph’, and ‘node’, are brought into the program to check for uninitialized variables of source program files  126 . In this example, ‘var’ is the variable being examined; ‘graph’ is the dominator graph (sometimes also called a function tree); and ‘node’ is the function being examined. 
         [0039]    In step  304 , it is determined whether ‘var’ is defined in ‘node’. If the function variable, ‘var’ is defined in the node, then in step  320 , false is returned, thereby ending the operations of UVF program  128  as the node is initialized. However, if the node does not define ‘var’, then in decision  306  it is determined whether the node has a predecessor found in the ‘graph’. 
         [0040]    If the node does not have a predecessor, then in step  308  it is determined whether ‘var’ is a function parameter. If ‘var’ is a function parameter then false is returned, as the variable is initialized, thereby ending the operations of UVF program  128 . Alternatively, if ‘var’ is not a function parameter then, in step  330 , true is returned, also ending the operations of UVF program  128 , as ‘var’ is uninitialized. 
         [0041]    Alternatively, if the node does have a predecessor in the graph, then in step  310 , ‘pred’ is created listing all of the predecessors of the ‘node’. In decision  312 , it is determined whether the ‘node’ has a parent or predecessor. If the ‘node’ has at least one predecessor, then for each ‘p’ (p represents each parent of the node) in ‘pred’ (the list of predecessors in the ‘node’) the process goes to flowchart  300 B. 
         [0042]    If the ‘node’ does not have a predecessor, i.e., it is the top most parent, then in step  314 , it is determined whether the operations of flowchart  300 B returned a true at any point. If the operations of flowchart  300 B returned a true, then in step  330  a true is returned as ‘var’ is not defined and uninitialized. Similarly, if flowchart  300 B returned a false, then in step  320  a false is returned as ‘var’ is defined and initialized. It is noted that, that after a return false or true in steps  320  or  330 , respectively, the operations may end or return to decision  360 , of flowchart  300 B depending on the iteration. 
         [0043]    Returning to decision  312 , if it is determined that the ‘node’ has a parent or predecessor, then in step  350 , of flowchart  300 B, exemplary function variables, ‘var’, ‘graph’, ‘node’ and ‘p’ are brought into the program. 
         [0044]    In decision  352 , it is determined if the variable, ‘var’, receives its value from a calculation that involves additional variables. For example, decision  352  determines whether ‘var’ is copied to other variables in the ‘node’. If, for instance, ‘var’ is given a set value, then in step  354 , ‘NextVars’ is the list of one element ‘var’. Alternatively, if the variable, ‘var’ receives its value from a calculation that involves additional variables then, in step  356 , the process continues by analyzing the additional variables. Therefore, in step  356 , ‘NextVars’ is the list of the variables to which ‘var’ is copied. 
         [0045]    In decision  358 , it is determined whether there is a ‘v’ (‘v’ represents the next variable) in ‘NextVars’ . For each ‘v’ in ‘NextVars’ the process is iterated and returns to step  302 , where ‘var’ is now set to ‘v’ and ‘node’ is set to ‘p’. The iteration is conducted constantly, moving to predecessors of ‘var’ in order to determine if the original variable is uninitialized. Stated differently, the process is iterated until a true or a false is returned. If, there is no ‘v’ in ‘NextVars’ then in decision  360 , it is determined whether the operations of flowchart  300 A returned a true at any point. If the operations of flowchart  300 A returned a true, then in step  380  a true is returned, as ‘var’ is not defined and uninitialized. If the operations of flowchart  300 A returned a false, then in step  370  a false is returned as ‘var’ is defined and initialized. 
         [0046]      FIG. 4  depicts a block diagram of internal and external components of a device  400 , in accordance with an illustrative embodiment of the present invention. It should be appreciated that  FIG. 4  provides only an illustration of one implementation, and does not imply any limitations with regard to the environments in which different embodiments may be implemented. Many modifications to the depicted environment may be made. 
         [0047]    Device  400  includes communications fabric  402 , which provides communications between computer processor(s)  404 , memory  406 , persistent storage  408 , communications unit  412 , and input/output (I/O) interface(s)  414 . Communications fabric  402  can be implemented with any architecture designed for passing data and/or control information between processors (such as microprocessors, communications and network processors, etc.), system memory, peripheral devices, and any other hardware components within a system. For example, communications fabric  402  can be implemented with one or more buses. 
         [0048]    Memory  406  and persistent storage  408  are computer readable storage media. In this embodiment, memory  406  includes random access memory (RAM)  416  and cache memory  418 . In general, memory  406  can include any suitable volatile or non-volatile computer readable storage media. Software is stored in persistent storage  408  for execution and/or access by one or more of the respective processors  404  via one or more memories of memory  406 . 
         [0049]    Persistent storage  408  may include, for example, a plurality of magnetic hard disk drives. Alternatively, or in addition to a magnetic hard disk drive, persistent storage  408  can include a solid state hard drive, a semiconductor storage device, read-only memory (ROM), erasable programmable read-only memory (EPROM), flash memory, or any other computer readable storage media that is capable of storing program instructions or digital information. 
         [0050]    The media used by persistent storage  408  may also be removable. For example, a removable hard drive may be used for persistent storage  408 . Other examples include optical and magnetic disks, thumb drives, and smart cards that are inserted into a drive for transfer onto another computer readable storage medium that is also part of persistent storage  408 . 
         [0051]    Communications unit  412  provides for communications with other computer systems or devices via a network. In this exemplary embodiment, communications unit  412  includes network adapters or interfaces such as a TCP/IP adapter cards, wireless Wi-Fi interface cards, or 3G or 4G wireless interface cards or other wired or wireless communication links. The network can comprise, for example, copper wires, optical fibers, wireless transmission, routers, firewalls, switches, gateway computers and/or edge servers. Software and data used to practice embodiments of the present invention can be downloaded to through communications unit  412  (e.g., via the Internet, a local area network or other wide area network). From communications unit  412 , the software and data can be loaded onto persistent storage  408 . 
         [0052]    I/O interface(s)  414  allows for input and output of data with other devices that may be connected to device  400 . For example, I/O interface  414  may provide a connection to external devices  420 , such as a keyboard, keypad, a touch screen, and/or some other suitable input device. External devices  420  can also include portable computer readable storage media such as, for example, thumb drives, portable optical or magnetic disks, and memory cards. Software and data used to practice embodiments of the present invention, can be stored on such portable computer readable storage media and can be loaded onto persistent storage  408  via I/O interface(s)  414 . I/O interface(s)  414  also connect to a display  422 . 
         [0053]    Display  422  provides a mechanism to display data to a user and may be, for example, a computer monitor. 
         [0054]    The present invention may be a system, a method, and/or a computer program product. The computer program product may include a computer readable storage medium (or media) having computer readable program instructions thereon for causing a processor to carry out aspects of the present invention. 
         [0055]    The computer readable storage medium can be a tangible device that can retain and store instructions for use by an instruction execution device. The computer readable storage medium may be, for example, but is not limited to, an electronic storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination of the foregoing. A non-exhaustive list of more specific examples of the computer readable storage medium includes the following: a portable computer diskette, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or Flash memory), a static random access memory (SRAM), a portable compact disc read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanically encoded device such as punch-cards or raised structures in a groove having instructions recorded thereon, and any suitable combination of the foregoing. A computer readable storage medium, as used herein, is not to be construed as being transitory signals per se, such as radio waves or other freely propagating electromagnetic waves, electromagnetic waves propagating through a waveguide or other transmission media (e.g., light pulses passing through a fiber-optic cable), or electrical signals transmitted through a wire. 
         [0056]    Computer readable program instructions described herein can be downloaded to respective computing/processing devices from a computer readable storage medium or to an external computer or external storage device via a network, for example, the Internet, a local area network, a wide area network and/or a wireless network. The network may comprise copper transmission cables, optical transmission fibers, wireless transmission, routers, firewalls, switches, gateway computers and/or edge servers. A network adapter card or network interface in each computing/processing device receives computer readable program instructions from the network and forwards the computer readable program instructions for storage in a computer readable storage medium within the respective computing/processing device. 
         [0057]    Computer readable program instructions for carrying out operations of the present invention may be assembler instructions, instruction-set-architecture (ISA) instructions, machine instructions, machine dependent instructions, microcode, firmware instructions, state-setting data, or either source code or object code written in any combination of one or more programming languages, including an object oriented programming language such as Smalltalk, C++ or the like, and conventional procedural programming languages, such as the “C” programming language or similar programming languages. The computer readable program instructions may execute entirely on the user&#39;s computer, partly on the user&#39;s computer, as a stand-alone software package, partly on the user&#39;s computer and partly on a remote computer or entirely on the remote computer or server. In the latter scenario, the remote computer may be connected to the user&#39;s computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet Service Provider). In some embodiments, electronic circuitry including, for example, programmable logic circuitry, field-programmable gate arrays (FPGA), or programmable logic arrays (PLA) may execute the computer readable program instructions by utilizing state information of the computer readable program instructions to personalize the electronic circuitry, in order to perform aspects of the present invention. 
         [0058]    Aspects of the present invention are described herein with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and/or block diagrams, and combinations of blocks in the flowchart illustrations and/or block diagrams, can be implemented by computer readable program instructions. 
         [0059]    These computer readable program instructions may be provided to a processor of a general purpose computer, special purpose computer, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions/acts specified in the flowchart and/or block diagram block or blocks. These computer readable program instructions may also be stored in a computer readable storage medium that can direct a computer, a programmable data processing apparatus, and/or other devices to function in a particular manner, such that the computer readable storage medium having instructions stored therein comprises an article of manufacture including instructions which implement aspects of the function/act specified in the flowchart and/or block diagram block or blocks. 
         [0060]    The computer readable program instructions may also be loaded onto a computer, other programmable data processing apparatus, or other device to cause a series of operational steps to be performed on the computer, other programmable apparatus or other device to produce a computer implemented process, such that the instructions which execute on the computer, other programmable apparatus, or other device implement the functions/acts specified in the flowchart and/or block diagram block or blocks. 
         [0061]    The flowchart and block diagrams in the Figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of instructions, which comprises one or more executable instructions for implementing the specified logical function(s). In some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems that perform the specified functions or acts or carry out combinations of special purpose hardware and computer instructions. 
         [0062]    The descriptions of the various embodiments of the present invention have been presented for purposes of illustration, but are not intended to be exhaustive or limited to the embodiments disclosed. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the invention. The terminology used herein was chosen to best explain the principles of the embodiment, the practical application or technical improvement over technologies found in the marketplace, or to enable others of ordinary skill in the art to understand the embodiments disclosed herein.