|
Home > Archive > Unix Programming > February 2007 > Environment strings in FreeBSD
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Environment strings in FreeBSD
|
|
| Daniel Rudy 2007-02-23, 1:18 pm |
| Hello Group,
What is the "safe" procedure in using environment variables from inside
a program?
I've seen some code like this:
void Listen(int fd, int backlog)
{
char *ptr;
/* can override 2nd arg with environment variable */
if ((ptr = getenv("LISTENQ")) != NULL) backlog = atoi(ptr);
if (listen(fd, backlog) < 0) err_sys("listen error");
}
In looking at the man page, it says that it's not thread safe, it's not
async cancel safe, and it's been depreciated by strtol.
I was thinking of doing something like this:
void Listen(int fd, int backlog)
{
char *ptr;
int i;
/* can override 2nd arg with environment variable */
if ((ptr = getenv("LISTENQ")) != NULL) sscanf(ptr, "%d", &i);
if (i > 5 && i < 15) backlog = i;
if (listen(fd, backlog) < 0) err_sys("listen error");
}
Which to me seems to be more safe and secure from unintentional (or
intentional) garbage in the environment.
What do you guys think?
--
Daniel Rudy
Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
| |
| Jens Thoms Toerring 2007-02-23, 1:18 pm |
| Daniel Rudy <spamthis@spamthis.net> wrote:
> What is the "safe" procedure in using environment variables from inside
> a program?
> I've seen some code like this:
> void Listen(int fd, int backlog)
> {
> char *ptr;
> /* can override 2nd arg with environment variable */
> if ((ptr = getenv("LISTENQ")) != NULL) backlog = atoi(ptr);
> if (listen(fd, backlog) < 0) err_sys("listen error");
> }
> In looking at the man page, it says that it's not thread safe, it's not
> async cancel safe, and it's been depreciated by strtol.
Looks like you're talking about the atoi() function (contrary what
your subject line seems to indicate). Why not use the hint you got
and use strtol() instead? It allows you to test if the string you
pass it did contain a number that strtol() could convert. Moreover,
you can also check if there were some other data following the
number in the string - you decide if you then don't want to use
the number or not. So you could do it like this:
void Listen( int fd, int backlog ) {
long i;
char *end_ptr;
if ( ( ptr = getenv( "LISTENQ" ) ) != NULL ) {
i = strtol( ptr, &end_ptr, 10 )
if ( end_ptr == NULL && i > 5 && i < 15 )
backlog = i;
}
if ( listen(fd, backlog ) < 0 )
err_sys( "listen error" );
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
| |
| Daniel Rudy 2007-02-23, 1:18 pm |
| At about the time of 2/23/2007 7:31 AM, Jens Thoms Toerring stated the
following:
> Daniel Rudy <spamthis@spamthis.net> wrote:
>
>
>
>
>
> Looks like you're talking about the atoi() function (contrary what
> your subject line seems to indicate). Why not use the hint you got
> and use strtol() instead? It allows you to test if the string you
> pass it did contain a number that strtol() could convert. Moreover,
> you can also check if there were some other data following the
> number in the string - you decide if you then don't want to use
> the number or not. So you could do it like this:
>
> void Listen( int fd, int backlog ) {
> long i;
> char *end_ptr;
>
> if ( ( ptr = getenv( "LISTENQ" ) ) != NULL ) {
> i = strtol( ptr, &end_ptr, 10 )
> if ( end_ptr == NULL && i > 5 && i < 15 )
> backlog = i;
> }
> if ( listen(fd, backlog ) < 0 )
> err_sys( "listen error" );
>
> Regards, Jens
> --
> \ Jens Thoms Toerring ___ jt@toerring.de
> \__________________________ http://toerring.de
That looks quite good to me. Thanks.
--
Daniel Rudy
Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m
Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep
|
|
|
|
|