Yahoo Groups archive

Milter-greylist

Index last updated: 2026-04-28 23:32 UTC

Thread

[milter-greylist] missing strndup

[milter-greylist] missing strndup

2013-09-04 by Bruncsak, Attila

Hello,

On my system there is no strndup function.
I got compilation warning (missing function prototype)
and linking error.

On an other system I found the following info (Linux man page of strndup):

CONFORMING TO
       strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001.  strndup(), strdupa(), and strndupa() are GNU extensions.

Is there a way that the configure script checks for it  (inject HAVE_STRNDUP macro),
and if there is no such function a small additional function in milter-greylist.c?

Best,
Attila

Re: [milter-greylist] missing strndup

2013-09-04 by Jim Klimov

On 2013-09-04 09:04, Bruncsak, Attila wrote:
> On my system there is no strndup function.

No offense intended (I too needed to do a lot of minor massaging
for programs under different revisions of Solaris), but rather
out of curiosity: what system do you use that things are missing
also here and there? ;)

//Jim

Re: [milter-greylist] missing strndup

2013-09-04 by Johann Klasek

On Wed, Sep 04, 2013 at 11:48:47AM +0200, Jim Klimov wrote:
> On 2013-09-04 09:04, Bruncsak, Attila wrote:
> > On my system there is no strndup function.
> 
> No offense intended (I too needed to do a lot of minor massaging
> for programs under different revisions of Solaris), but rather
> out of curiosity: what system do you use that things are missing
> also here and there? ;)

Especially (at least) up to Solaris 10 strndup() is not provided.
strndup() can be found in POSIX base starting with POSIX.1-2008.
AFAIK Solaris conforms only to POSIX.1-2001.
However, autoconf should handle it as suggested by Attila ...

(probably we'll hear "submit a patch" from Manu soon ;) )


Johann

Re: [milter-greylist] missing strndup

2013-09-04 by manu@...

Bruncsak, Attila <attila.bruncsak@...> wrote:

>        strdup() conforms to SVr4, 4.3BSD, POSIX.1-2001.  strndup(),
> strdupa(), and strndupa() are GNU extensions.

Right, I will replace it by strlen/malloc/strncpy.

-- 
Emmanuel Dreyfus
http://hcpnet.free.fr/pubz
manu@...

Re: [milter-greylist] missing strndup

2013-09-05 by Jim Klimov

On 2013-09-04 13:14, Johann Klasek wrote:
> On Wed, Sep 04, 2013 at 11:48:47AM +0200, Jim Klimov wrote:
>  > On 2013-09-04 09:04, Bruncsak, Attila wrote:
>  > > On my system there is no strndup function.
>  >
>  > No offense intended (I too needed to do a lot of minor massaging
>  > for programs under different revisions of Solaris), but rather
>  > out of curiosity: what system do you use that things are missing
>  > also here and there? ;)
>
> Especially (at least) up to Solaris 10 strndup() is not provided.

Confirmed on OpenSolaris SXCE (yes, it is pretty old now) also -
the current CVS version does not compile due to this.

> strndup() can be found in POSIX base starting with POSIX.1-2008.
> AFAIK Solaris conforms only to POSIX.1-2001.
> However, autoconf should handle it as suggested by Attila ...
>
> (probably we'll hear "submit a patch" from Manu soon ;) )

For once, no :)

//Jim

Re: [milter-greylist] missing strndup

2013-09-05 by manu@...

Johann Klasek <johann@...> wrote:

> (probably we'll hear "submit a patch" from Manu soon ;) )

It will rather be "get it from CVS or wait next release"

Index: milter-greylist.c
===================================================================
RCS file: /cvsroot/milter-greylist/milter-greylist.c,v
retrieving revision 1.269
retrieving revision 1.270
diff -U 4 -r1.269 -r1.270
--- milter-greylist.c   1 Sep 2013 04:59:42 -0000       1.269
+++ milter-greylist.c   4 Sep 2013 23:58:30 -0000       1.270
@@ -3800,14 +3800,16 @@
                }
 
                src = priv->priv_sr.sr_msg_x;
                for (i = 0; i < lcount; i++) {
-                       if ((lbufs[i] = strndup(src, MAXREPLYLEN)) == NULL) {
+                       if ((lbufs[i] = malloc(MAXREPLYLEN + 1)) == NULL) {
                                mg_log(LOG_ERR, "strndup failed: %s",
                                       strerror(errno));
                                exit(EX_OSERR);
                        }
 
+                       (void)strncpy(lbufs[i], src, MAXREPLYLEN);
+
                        src += MAXREPLYLEN;             
                }
 
                lbufs[i] = NULL;


-- 
Emmanuel Dreyfus
http://hcpnet.free.fr/pubz
manu@...

Re: [milter-greylist] missing strndup

2013-09-05 by Johann Klasek

On Thu, Sep 05, 2013 at 02:05:56AM +0200, manu@... wrote:
[..]
> Index: milter-greylist.c
> ===================================================================
> RCS file: /cvsroot/milter-greylist/milter-greylist.c,v
> retrieving revision 1.269
> retrieving revision 1.270
> diff -U 4 -r1.269 -r1.270
> --- milter-greylist.c   1 Sep 2013 04:59:42 -0000       1.269
> +++ milter-greylist.c   4 Sep 2013 23:58:30 -0000       1.270
> @@ -3800,14 +3800,16 @@
>                 }
>  
>                 src = priv->priv_sr.sr_msg_x;
>                 for (i = 0; i < lcount; i++) {
> -                       if ((lbufs[i] = strndup(src, MAXREPLYLEN)) == NULL) {
> +                       if ((lbufs[i] = malloc(MAXREPLYLEN + 1)) == NULL) {
>                                 mg_log(LOG_ERR, "strndup failed: %s",
>                                        strerror(errno));
>                                 exit(EX_OSERR);
>                         }
>  
> +                       (void)strncpy(lbufs[i], src, MAXREPLYLEN);
> +
>                         src += MAXREPLYLEN;             
>                 }
>  
>                 lbufs[i] = NULL;

These changes seem to be not equivalent.
strndup() has other properties:

 * the patch code always creates in lbuf[i] a MAXREPLYLEN + 1 sized buffer - strndup()
   doesn't if the length of src is less than MAXREPLYLEN.
	if (strlen(src) <= MAXREPLYLEN) {
		lbufs[i] = strdup(src);
	}
	else {
		if ((lbufs[i] = malloc(MAXREPLYLEN + 1)) == NULL) {
		[..]

 * strndup() always null-terminates the destination. strncpy doesn't if
	length of src is greater or equal MAXREPLYLEN.
	Above after strncpy()
		lbufs[i][MAXREPLYLEN] = 0;
	is missing.
	malloc() does not guarantee that the allocate memory is zeroed.


Johann

Re: [milter-greylist] missing strndup

2013-09-05 by Emmanuel Dreyfus

On Thu, Sep 05, 2013 at 09:47:57AM +0200, Johann Klasek wrote:
> 	Above after strncpy()
> 		lbufs[i][MAXREPLYLEN] = 0;
> 	is missing.

Thank you for catching that one.

-- 
Emmanuel Dreyfus
manu@...

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.