M
Mark
Is it a valid and portable to return strings as in the following code?
#include <stdio.h>
static const char *errno2str(int err)
{
switch (err) {
case 1:
return "Error 1 occured";
case 2:
return "Error 2 occured";
default:
return "Unknown error occured";
}
}
int main(void)
{
printf("%s\n", errno2str(1));
printf("%s\n", errno2str(2));
printf("%s\n", errno2str(5));
return 0;
}
The reason I'm asking is that compilation with
"gcc -ansi -pedantic -W -Wall" goes clean and smooth, but splint spits this
out:
Splint 3.1.1 --- 11 Sep 2006
x.c: (in function errno2str)
x.c:7:11: Observer storage returned without qualification: "Error 1 occured"
Observer storage is transferred to a non-observer reference. (Use
-observertrans to inhibit warning)
x.c:7:11: Storage becomes observer
x.c:9:11: Observer storage returned without qualification: "Error 2 occured"
x.c:9:11: Storage becomes observer
x.c:11:11: Observer storage returned without qualification:
"Unknown error occured"
x.c:11:11: Storage becomes observer
x.c: (in function main)
x.c:17:17: New fresh storage (type char *) passed as implicitly temp (not
released): errno2str(1)
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
x.c:18:17: New fresh storage (type char *) passed as implicitly temp (not
released): errno2str(2)
x.c:19:17: New fresh storage (type char *) passed as implicitly temp (not
released): errno2str(5)
Finished checking --- 6 code warnings
#include <stdio.h>
static const char *errno2str(int err)
{
switch (err) {
case 1:
return "Error 1 occured";
case 2:
return "Error 2 occured";
default:
return "Unknown error occured";
}
}
int main(void)
{
printf("%s\n", errno2str(1));
printf("%s\n", errno2str(2));
printf("%s\n", errno2str(5));
return 0;
}
The reason I'm asking is that compilation with
"gcc -ansi -pedantic -W -Wall" goes clean and smooth, but splint spits this
out:
Splint 3.1.1 --- 11 Sep 2006
x.c: (in function errno2str)
x.c:7:11: Observer storage returned without qualification: "Error 1 occured"
Observer storage is transferred to a non-observer reference. (Use
-observertrans to inhibit warning)
x.c:7:11: Storage becomes observer
x.c:9:11: Observer storage returned without qualification: "Error 2 occured"
x.c:9:11: Storage becomes observer
x.c:11:11: Observer storage returned without qualification:
"Unknown error occured"
x.c:11:11: Storage becomes observer
x.c: (in function main)
x.c:17:17: New fresh storage (type char *) passed as implicitly temp (not
released): errno2str(1)
A memory leak has been detected. Storage allocated locally is not released
before the last reference to it is lost. (Use -mustfreefresh to inhibit
warning)
x.c:18:17: New fresh storage (type char *) passed as implicitly temp (not
released): errno2str(2)
x.c:19:17: New fresh storage (type char *) passed as implicitly temp (not
released): errno2str(5)
Finished checking --- 6 code warnings