For years I've been doing this to add a backslash to a path, because I didn't
know the Windows API could do it for me. The good thing is that the API won't
add a slash if it's already there, so you don't need to do an If/EndIf test.
You are making the string longer without reallocating it.
It works with such a short example, because windows allocates memory at a certain boundary (16 bytes iirc).
If you happen to have a string that exactly fills the allocated buffer, well... boom
The chance for that is remote, but if it ever happens, good luck on tracing that bug...
> If you happen to have a string that exactly fills the allocated buffer, well... boom
Are you sure? The MSDN docs says the return value will be null if the backslash
could not be appended due to inadequate buffer size, which means no boom?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
PB wrote:> If you happen to have a string that exactly fills the allocated buffer, well... boom
Are you sure? The MSDN docs says the return value will be null if the backslash
could not be appended due to inadequate buffer size, which means no boom?
It also says that the buffer should be MAX_PATH in size, which is not the case.
As the function takes no "size" parameter it cannot know how big the buffer really is,
so my guess is it simply assumes MAX_PATH as the size.
OK, i'm confused here; The MSDN site says: "lpszPath
[in, out] Pointer to a buffer with a string that represents a path. The size of this buffer should be set to MAX_PATH to ensure that it is large enough to hold the returned string."
So this means if a buffer size was MAX_PATH it would always be big enough to hold the returned string? So if PathAddBackslash_(p2$) and
p2$ was a pointer to a buffer that was MAX_PATH in size then there wouldn't be any problem? Are are you saying that a pointer to the buffer that was MAX_PATH wouldn't matter that there would still be problems because the function doesn't take a size parameter?
No if the buffer has length MAX_PATH then there wouldn't be any BOOM. But if the buffer is shorter there will be BOOM since there is no size parameter. Alternatively, turn down your loudspeakers. *Straight face*
Trond wrote:No if the buffer has length MAX_PATH then there wouldn't be any BOOM. But if the buffer is shorter there will be BOOM since there is no size parameter. Alternatively, turn down your loudspeakers. *Straight face*
Right, but there is no garantee for that with a PB string.
a$ = "c:\Kcc"
PathAddBackslash(@a$) ; la soluce qui tue : o) debug a$
calldebugger
a$ = "c:\toto\"
PathAddBackslash(@a$) ; la soluce qui tue : o) debug a$
calldebugger
procedure PathAddBackslash(Path ) ;Solution By Dobro patented copyrighté Supra Hadopi : o) ; ajoute un "\" si pas present a la fin
mem=Path
path$= reversestring ( peeks (mem)) if mid (Path$,1,1) <> "\"
Path$= "\" +Path$
Path$= reversestring (Path$) pokes (mem,path$) endif endprocedure