Title: Using splint for bounds when the array size is to be found during run time.

Hi all,
   I using splint for the first time.  My basic purpose of using splint is to capture bounds errors and memory leaks.

   In my sample code, I have two variables namely num_of_interfaces & interface_names.  interface_names is an pointer to interface name whose size is known at run time once, detect_num_of_interfaces() is run.

    As a result,

interface_names[i] = Defined Result (if i >=0 && i < num_of_interface) else, Undefined.

    I would therefore like to incorprorate the same into the interface.h so that I need not put the same check  as part of the "REQUIRES" clause in each function, that will possibly be using this variable.   Has someone done anything like this before.  If so, please let me know how to go about it.

File: interface.h
==================
[EMAIL PROTECTED] tmp]# cat interfaces.h
int num_of_interfaces;
typedef char * string;
string *interface_names;


File Interface.c
=================
[EMAIL PROTECTED] tmp]# cat interfaces.c
#include "./interfaces.h"

char sample_device_name[]="Sample Device";

int detect_num_of_interfaces(void)
{
   /* Some Hardware specific function.
           It could for example be an pci_probe
           that'll probe for each device attached to
       the pci bus
   */
   return 10; /* some value */
}

string detect_interface_name(int dev_id)
{
    /* Will return the device name. We can just say that the
       device_id + Vendor id will be the interface name for
           now, let us say that all device names are "Sample Device" */
    return sample_device_name;
}

void configure_interfaces()
{
        int i;
        num_of_interfaces=detect_num_of_interfaces();
        interface_names = (string *)malloc(sizeof(string) * num_of_interfaces);
    for (i = 0; i < num_of_interfaces; i++)
                interface_names[i] = detect_interface_name(i);
}

int print_interface_names()
{
    int i;
    for(i=0; i<num_of_interfaces; i++)
        printf("[%d]st interface is %s\n", i, interface_names[i]);
}
[EMAIL PROTECTED] tmp]#



Splint Warnings
===============
On running splint, I get the following warning
[EMAIL PROTECTED] tmp]# splint -weak +bounds interfaces.c
Splint 3.0.1.7 --- 24 Jan 2003

interfaces.c: (in function print_interface_names)
interfaces.c:36:47: Possible out-of-bounds read:
    interface_names[i]
    Unable to resolve constraint:
    requires maxRead(interface_names @ interfaces.c:36:47) >= i @
    interfaces.c:36:63
     needed to satisfy precondition:
    requires maxRead(interface_names @ interfaces.c:36:47) >= i @
    interfaces.c:36:63
  A memory read references memory beyond the allocated storage. (Use
  -boundsread to inhibit warning)

Finished checking --- 1 code warning
[EMAIL PROTECTED] tmp]#



Confidentiality Notice

The information contained in this electronic message and any attachments to this message are intended
for the exclusive use of the addressee(s) and may contain confidential or privileged information. If
you are not the intended recipient, please notify the sender at Wipro or [EMAIL PROTECTED] immediately
and destroy all copies of this message and any attachments.
_______________________________________________
splint-discuss mailing list
[email protected]
http://www.splint.org/mailman/listinfo/splint-discuss

Reply via email to