Hello,

um, my first question here: How can i join two strings? (E.g. some string user entered and some constant string).

Thank you,
sF
Posted on 2001-12-25 23:27:16 by stealthFIGHTER
lstrcat

Posted on 2001-12-26 00:13:44 by bitRAKE
Have a look at the masmlib ;)

[size=12]


szCatStr proc lpszSource:DWORD, lpszAdd:DWORD

Description
szCatStr appends the second zero terminated string (lpszAdd) to
the end of the first zero tereminated string (lpszSource). The
result is zero terminated.




or in my sh*tty code: (tested)

Posted on 2001-12-26 00:16:05 by JimmyClif
wsprintf is really useful to concatenate several strings, on any position of it.
Posted on 2001-12-26 04:11:57 by JCP
Thank you all. Can you give me an (short) example? Please :). (some string from 'input-box' + some constant string).

sF
Posted on 2001-12-26 06:39:39 by stealthFIGHTER
Theoretically, the documentation is clear enough to use these functions without problems using Invoke.

http://msdn.microsoft.com and search the APIs using the edit box at the left of the page.
Posted on 2001-12-26 07:58:04 by JCP
> JimmyClif
>
> Have a look at the masmlib
>
> code
>
> szCatStr proc lpszSource:DWORD, lpszAdd:DWORD

- No Unicode support ;(
- Why add more code to your EXE when you can use a runtime function to do
the same job? Ok, so its not a lot more code but it does defeat the
purpose of code reuse right?
"standing on other people?s shoulders, not their toes"
- What happends if lpszAdd is longer than lpszSource? If you're going to
re-invent the wheel leave the bumps and corners off. (I'm not blaming
you of course. Damn Hutch.)

> Readiosys
> wsprintf is really useful to concatenate several strings, on any
> position of it.

Flexible, but not so: it has a limit of 1024 bytes for the destination buffer!

> bitRAKE
> lstrcat
> (Oh, damn - I'm getting lazy again. Must be the end of the year.)

And complacent too :) It *must* be the end of the year.

lstrcat is a liability. There is no builtin error
detection, it only returns NULL if the function failed--it could well have
succeeded in overflowing--this can mask some nasty bugs for a weary programmer
(yes that's all of us at 2am). Example:


LOCAL source[3]:byte ;
LOCAL dest[3]:byte ;

STRING overrun, "oh dear!"

invoke lstrcpy, addr source, addr overrun ; overrun 1
invoke lstrcpy, addr dest, addr overrun ; overrun 2
invoke lstrcat, addr dest, addr source ; overrun 3

; windows doesn't even raise a peep
.if (eax == NULL)
invoke MessageBox,hWnd,SADD("lstrcat failed"),
SADD("asdf"),MB_OK or MB_ICONINFORMATION
.endif


btw I did check the returns to lstrcpy above and Windows didn't complain at
all. This was a _blatant_ attempt to overflow the buffer, a subtle mistake like
forgetting to include the source's null character in your caclulations can come
back and bite you in the arse when you least expect it.

What's really needed is a customised strncat() like function for masmlib. It
would have its own length checking logic. Then to concatenation an entire
buffer to another you could just do something like:


invoke ML_strncat, dest, src, sizeof src

.if eax == -1
string too big, deal with it
.endif


And be done with it instead of having to deal with checking the buffers before
hand and other tedious error handling that should be apart of any well
designed function. The function would support wide character strings, have
builtin error checking, and return intelligent error/success codes. (And
it wouldn't be so lpszRidiCulously named :)

Lets learn from other people's mistakes:

http://www.microsoft.com/technet/treeview/default.asp?url=/technet/security/bulletin/MS01-059.asp

I think someone forget to take heed of those lil' notes MS puts at the bottom
of their API's:

Security Remarks
The first argument, psz1, must be large enough to hold psz2 and the closing
'\0', otherwise a buffer overrun may occur. Buffer overruns may lead to a
denial of service attack against the application if an access violation
occurs. In the worst case, a buffer overrun may allow an attacker to inject
executable code into your process, especially if psz1 is a stack-based buffer.

Doh!!

Never mind though, eat, drink, and be merry (it's Christmas after all), for
tommorow we shall worry about patching those overruns!

cheers,
Boggy
Posted on 2001-12-26 08:34:29 by Boggy
SF,

Check out Iczelions Tutorials... A couple of them deal with getting strings from input boxes.

Boggy,

For the buffer overruns: I never had one until now ;-) Maybe because I know what's going on in my own programs? *g*
Imo, if someone uses lstrcat etc.. they know how big the strings are... If you're working with a constant string.. you definitively know how big it is... If you're working with an input box string you have to define how many chars it has to fetch from the input box.. Simple math tells me how big the buffer is supposed to be.
Posted on 2001-12-26 12:36:11 by JimmyClif
Huh, thank all!!!

sF
Posted on 2001-12-26 17:03:01 by stealthFIGHTER