BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

strncpy/wcsncpy query (BCB6/STLPort)
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++)
View previous topic :: View next topic  
Author Message
Andrue Cope [TeamB]
Guest





PostPosted: Thu Jul 06, 2006 5:14 pm    Post subject: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote



I've just found that wcsncpy() calls wcslen() to determine the length
of the source string - surely that's unfair?

In this particular case I'm using wcsncpy() because the source string
is not '\0' terminated. Is this an STLPort bug or expected behaviour?
I'd have thought that wcsncpy() had no business calling wcslen().

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Darko Miletic
Guest





PostPosted: Thu Jul 06, 2006 5:48 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote



Andrue Cope [TeamB] wrote:
Quote:
I've just found that wcsncpy() calls wcslen() to determine the length
of the source string - surely that's unfair?

In this particular case I'm using wcsncpy() because the source string
is not '\0' terminated. Is this an STLPort bug or expected behaviour?
I'd have thought that wcsncpy() had no business calling wcslen().


I do not have bcb6 on this machine but if that is the case it is an error.

You have two options:

1. Roll your own version of desired function.
2. Switch to MS strsafe functions.
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Jul 06, 2006 6:15 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote



Darko Miletic wrote:

Quote:
I do not have bcb6 on this machine but if that is the case it is an
error.

My thinking too. Perhaps STLPort is trying to optimise wcslen into a
memcpy() although that seems pretty pointless.

Quote:
You have two options:

1. Roll your own version of desired function.

Yeah - I used memcpy() :)

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Bob Gonder
Guest





PostPosted: Thu Jul 06, 2006 6:18 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

Andrue Cope [TeamB] wrote:

Quote:
I've just found that wcsncpy() calls wcslen() to determine the length
of the source string - surely that's unfair?

I would think so too.
You could switch to lstrcpynW
(And hope MS doesn't do the same thing)
Back to top
Rudy Velthuis [TeamB]
Guest





PostPosted: Thu Jul 06, 2006 9:10 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

At 15:18:46, 06.07.2006, Bob Gonder wrote:

Quote:
Andrue Cope [TeamB] wrote:

I've just found that wcsncpy() calls wcslen() to determine the length
of the source string - surely that's unfair?

I would think so too.
You could switch to lstrcpynW
(And hope MS doesn't do the same thing)

I'd rather write my own. Then I'd be sure that it did what I expected it
to do.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/

"Well done is better than well said." -- Benjamin Franklin (1706-1790)
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Thu Jul 06, 2006 9:12 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

"Andrue Cope [TeamB]" <no.spam (AT) not (DOT) a.valid.address> writes:

Quote:
I've just found that wcsncpy() calls wcslen() to determine the
length of the source string - surely that's unfair?

I'm not really sure, but I tend to disagree. On my Linux box, I get:

% man wcsncpy

NAME
wcsncpy - copy a fixed-size string of wide characters

SYNOPSIS
#include <wchar.h>

wchar_t *wcsncpy(wchar_t *dest, const wchar_t *src, size_t n);

DESCRIPTION
The wcsncpy function is the wide-character equivalent of the strncpy
function. It copies at most n wide characters from the wide-character
string pointed to by src, including the terminating L'\0' character,
to the array pointed to by dest. Exactly n wide characters are writ-
ten at dest. If the length wcslen(src) is smaller than n, the remain-
ing wide characters in the array pointed to by dest are filled with
L'\0' characters. If the length wcslen(src) is greater or equal to n,
the string pointed to by dest will not be L'\0' terminated.

The strings may not overlap.

The programmer must ensure that there is room for at least n wide
characters at dest.

RETURN VALUE
wcsncpy returns dest.

CONFORMING TO
ISO/ANSI C, UNIX98

[That's not a normative text, but I don't have a copy of the C
standard.]

Note that the behavior of wcsncpy is documented in terms of wcslen()
applied to src. This only makes sense if applying wcslen() to src has
defined behavior.
Back to top
Alan Bellingham
Guest





PostPosted: Thu Jul 06, 2006 9:27 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

maeder (AT) glue (DOT) ch (Thomas Maeder [TeamB]) wrote:

Quote:
[That's not a normative text, but I don't have a copy of the C
standard.]

Note that the behavior of wcsncpy is documented in terms of wcslen()
applied to src. This only makes sense if applying wcslen() to src has
defined behavior.

-------------------------------------------------------------------------
7.24.4.2.2 The wcsncpy function

Synopsis

1 #include <wchar.h>
wchar_t *wcsncpy(wchar_t * restrict s1,
const wchar_t * restrict s2,
size_t n);

Description
2 The wcsncpy function copies not more than n wide characters (those
that follow a null wide character are not copied) from the array
pointed to by s2 to the array pointed to by s1. 288)

3 If the array pointed to by s2 is a wide string that is shorter than n
wide characters, null wide characters are appended to the copy in the
array pointed to by s1, until n wide characters in all have been
written.

Returns
4 The wcsncpy function returns the value of s1.

....

288) Thus, if there is no null wide character in the first n wide
characters of the array pointed to by s2, the result will not be
null-terminated.
-------------------------------------------------------------------------

No mention there whatever of the wcslen() requirement. (This is the C99
version.)

Alan Bellingham
--
Me <url:mailto:alanb (AT) episys (DOT) com> <url:http://www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:http://accu.org/>
The 2006 Discworld Convention <url:http://www.dwcon.org/>
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Thu Jul 06, 2006 9:31 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

Thomas Maeder [TeamB] wrote:

Quote:
Note that the behavior of wcsncpy is documented in terms of wcslen()
applied to src. This only makes sense if applying wcslen() to src has
defined behavior.

Interesting.

So should the following code be fine or asking for trouble?

wchar_t buffer1[ 3 ];

wchar_t buffer2[ 2 ];
buffer2[ 0 ]=L'H';
buffer2[ 1 ]=L'I';

wcsncpy( buffer, buffer2, 2 );

ISTM that it should be fine but if wcsncpy() is using wcslen() it will
obviously be undefined. Using wcslen() inside wcsncpy() seems to be
causing trouble for no good reason :-/
--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Thomas Maeder [TeamB]
Guest





PostPosted: Fri Jul 07, 2006 2:05 am    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

"Andrue Cope [TeamB]" <no.spam (AT) not (DOT) a.valid.address> writes:

Quote:
Note that the behavior of wcsncpy is documented in terms of wcslen()
applied to src. This only makes sense if applying wcslen() to src has
defined behavior.

Interesting.

But wrong, it seems after Alan's post.
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Fri Jul 07, 2006 8:10 am    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

Alan Bellingham wrote:

Quote:
No mention there whatever of the wcslen() requirement. (This is the
C99 version.)

That's certainly what I'd expect. Making use of wcslen() just limits
the usefulness. By its nature the function has to walk the source
string looking for a terminator so the only advantage (if it is one) is
that it can use a memory copy to transfer the data.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Roddy Pratt
Guest





PostPosted: Fri Jul 07, 2006 2:03 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

FWIW, the BDS2006 dinkumware library doesn't use wcslen.

However, the version of wcsncpy in "\source\cpprtl\source\cstrings" still
does!

I don't know exactly which gets used and when - is dinkumware not used for
console applications??

- Roddy
Back to top
Andrue Cope [TeamB]
Guest





PostPosted: Fri Jul 07, 2006 3:27 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

Roddy Pratt wrote:

Quote:
However, the version of wcsncpy in "\source\cpprtl\source\cstrings"
still does!


Yeah :-/

As I suspected it looks like an optimisation.

Quote:
I don't know exactly which gets used and when - is dinkumware not
used for console applications??

I thought it was used all the time with BDS but now I'm not so sure.

--
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Back to top
Bob Gonder
Guest





PostPosted: Sat Jul 08, 2006 9:18 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

Rudy Velthuis [TeamB] wrote:

Quote:
I'd rather write my own. Then I'd be sure that it did what I expected it
to do.

Kinda defeats the purpose of standard libraries, doesn't it?
Back to top
Rudy Velthuis [TeamB]
Guest





PostPosted: Sat Jul 08, 2006 11:45 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

At 18:18:33, 08.07.2006, Bob Gonder wrote:

Quote:
Rudy Velthuis [TeamB] wrote:

I'd rather write my own. Then I'd be sure that it did what I expected
it to do.

Kinda defeats the purpose of standard libraries, doesn't it?

Sure. But if you can't trust a library, what to do? I personally don't
think that wcslen() should have been used.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/

"The Stones, I love the Stones. I watch them whenever I can. Fred,
Barney..." -- Steven Wright.
Back to top
Rudy Velthuis [TeamB]
Guest





PostPosted: Mon Jul 10, 2006 10:08 pm    Post subject: Re: strncpy/wcsncpy query (BCB6/STLPort) Reply with quote

At 14:42:33, 10.07.2006, Hendrik Schober wrote:

Quote:
Sure. But if you can't trust a library, what to do? [...]

Whatever, trading some libraries bugs for your own isn't
an option.

If you don't even trust your own code, then you are in the wrong
business. <g>

Especially if this is only a small routine like wcscpy, it is IMO better
to use your own code which you fully control, than switch an entire 3rd
party library, which may introduce other bugs.
--
Rudy Velthuis [TeamB] http://rvelthuis.de/

"I have often regretted my speech, never my silence."
- Xenocrates (396-314 B.C.)
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Language C++) All times are GMT
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.