Discussion:
lstrcpyn documentation problem : string too long but call still succeedes?
(too old to reply)
R.Wieser
2020-11-27 19:21:02 UTC
Permalink
Hello all,

I need to copy an ANSI string from a source into a buffer with a fixed
length. That looks like a job for "lstrcpynA". Easy-Peasy, right ?

There is only a tiny issue with it : For some reason its able to accept a
string that is too long to place in the target buffer, *but still succeed*.

The problem (to me) is that the MSDN docucumentation to the call [1] do not
even /hint/ at how I'm supposed to detect such a succeeded-but-failed
situation. :-(

[1]
https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-lstrcpyna

Does anyone know whay the succeeded-but-failed result look like ?

Regards,
Rudy Wieser

P.s.
I've seen the page state that I should not use that function, but instead
something like StringCchCopy. The problem with that is that they "forgot"
to mention which DLL its part of ... if its actually a DLL function to begin
with.
R.Wieser
2020-11-27 19:57:12 UTC
Permalink
Post by R.Wieser
There is only a tiny issue with it : For some reason its able to accept a
string that is too long to place in the target buffer, *but still succeed*.
A few tests show that it just copies what it can and terminates that with a
NUL. But as that is not documented I should not depend on it and
therefore still cannot use the function. :-(

Oh well.

Regards,
Rudy Wieser
JJ
2020-11-28 02:13:08 UTC
Permalink
Post by R.Wieser
A few tests show that it just copies what it can and terminates that with a
NUL.
Then what do you need to happen when the buffer doesn't have enough space?
Cause, that result is what a safe string copy process would do.
R.Wieser
2020-11-28 07:14:51 UTC
Permalink
JJ,
Post by JJ
Then what do you need to happen when the buffer doesn't have enough space?
Wrong question. What I /need/ is documented behaviour - whatever it does.
Post by JJ
Cause, that result is what a safe string copy process would do.
I could as easily imagine that it would return an error. What good does a
partial copied string do ? It might even be dangerous (in my case: causing
the wrong file or folder to be accessed) ...

Regards,
Rudy Wieser
JJ
2020-11-29 06:17:44 UTC
Permalink
Post by R.Wieser
I could as easily imagine that it would return an error. What good does a
partial copied string do ? It might even be dangerous (in my case: causing
the wrong file or folder to be accessed) ...
Some strings are copied just for displays where if the source is too long,
the result will be truncated. Showing as much information as possible.
R.Wieser
2020-11-29 09:24:10 UTC
Permalink
JJ,
Post by JJ
Some strings are copied just for displays where if the source is
too long, the result will be truncated. Showing as much information
as possible.
Yeah, that was the only reason I could think of too. It makes me wonder
why this, rather obvious user-related function, is part of kernel32 and not
shell32 (or perhaps user32) though.

And too bad that they removed(?) the explanation to what the function is
actually ment for and does ... It would have cleared up the confusion in a
heartbeat.

Regards,
Rudy Wieser

Loading...