[splint-discuss] const tests

Liran Orevi liranorevi at hotmail.com
Tue Aug 14 09:51:29 EDT 2007


Hello,
Does Splint checks 'const' related actions?
Below are some examples.

Many Thanks,
Liran

CONST INT
--------------------

liran at best-computer3:~/Learn/Source/SplintTries$
liran at best-computer3:~/Learn/Source/SplintTries$ cat 4.c
#include 
#include 

        int
main()
{
        const int int1 = 5;
        int1 = 6;
        exit(EXIT_SUCCESS);
}
liran at best-computer3:~/Learn/Source/SplintTries$ splint 4.c
Splint 3.1.2 --- 02 Aug 2007

Finished checking --- no warnings
liran at best-computer3:~/Learn/Source/SplintTries$ gcc 4.c -o 4
4.c: In function ‘main’:
4.c:8: error: assignment of read-only variable ‘int1’


CONST POINTER
---------------------------------

liran at best-computer3:~/Learn/Source/SplintTries$
liran at best-computer3:~/Learn/Source/SplintTries$ cat 3.c
#include 
#include 

        static void
changeString(const char *char1)
{
        *char1='H';
}

        int
main()
{
        char char1[6] = "hello";
        printf("before: %s\n",char1);
        changeString(char1);
        printf("after: %s\n",char1);
        exit(EXIT_SUCCESS);
}
liran at best-computer3:~/Learn/Source/SplintTries$ splint 3.c
Splint 3.1.2 --- 02 Aug 2007

Finished checking --- no warnings
liran at best-computer3:~/Learn/Source/SplintTries$ gcc 3.c -o 3
3.c: In function ‘changeString’:
3.c:7: error: assignment of read-only location

CONST POINTER COPY (warned by Gcc)
-----------------------------------------------------------

liran at best-computer3:~/Learn/Source/SplintTries$
liran at best-computer3:~/Learn/Source/SplintTries$ cat 2.c
#include 
#include 

        static void
changeString(const char *char1)
{
        char *charPointer1=char1;
        *charPointer1='H';
}

        int
main()
{
        char char1[6] = "hello";
        printf("before: %s\n",char1);
        changeString(char1);
        printf("after: %s\n",char1);
        exit(EXIT_SUCCESS);
}
liran at best-computer3:~/Learn/Source/SplintTries$ splint 2.c
Splint 3.1.2 --- 02 Aug 2007

Finished checking --- no warnings
liran at best-computer3:~/Learn/Source/SplintTries$ gcc 2.c -o 2
2.c: In function ‘changeString’:
2.c:7: warning: initialization discards qualifiers from pointer target type
liran at best-computer3:~/Learn/Source/SplintTries$ ./2
before: hello
after: Hello

_________________________________________________________________
With Windows Live Hotmail, you can personalize your inbox with your favorite color.
www.windowslive-hotmail.com/learnmore/personalize.html?locale=en-us&ocid=TXT_TAGLM_HMWL_reten_addcolor_0607


More information about the splint-discuss mailing list