Buffers & security question
So it's been a loooooong time since I've properly done any Win32 API stuff. Years ago, samples like these (& my code too) were commonplace: http://allapi.mentalis.org/apilist/EnumPrinters.shtml# or http://word.mvps.org/faqs/macrosvba/...lePrinters.htm.
In essence:
[LIST][1]Try an API call once, passing a dummy value for one of the byref buffer-length related parameters
[2]Check the return value - if it didn't work, use the retrieved buffer length (now known) & just call the same API call again[/ LIST]
Um, so wouldn't this cause one of those nasty buffer overflow security leak errors I've heard so much about in the news, causing potentially full remote control? What's the 2016 preferred way of coding & calling APIs with buffer length parameters please?
Re: Buffers & security question
Quote:
Originally Posted by
alex_read
Um, so wouldn't this cause one of those nasty buffer overflow security leak errors I've heard so much about in the news, causing potentially full remote control?
If you're passing the correct size of your buffer (or have allocated the recommended size), then I don't see how that could cause a buffer overrun. Unless an API has a bug, it won't write more bytes than you've specified (or more than the recommended size).
Quote:
Originally Posted by
alex_read
What's the 2016 preferred way of coding & calling APIs with buffer length parameters please?
It is (usually) same as before. APIs typically do not change their calling behavior because doing so would break programs that depended on those behaviors. MSDN usually documents the recommended steps of filling a buffer in the API's documentation page, so it would be best to follow that.
Re: Buffers & security question
In my opinion, not fully understanding the API call is the biggest reason for buffer overruns. Each API is defined and parameters are explained thru documentation, i.e., MSDN.
Some APIs can return the required buffer length in a passed parameter or as the return value of the function. If via a passed parameter, ensure you are passing that parameter ByRef or passing its VarPtr(); otherwise, the called function can actually trigger the crash trying to write to unallocated/protected memory.
Some APIs may require you to set one or more parameters to null in order to know to return a required buffer size. While others may require you to call a completely different API to get the length, i.e., GetWindowTextLength.
Some APIs will tell you if the buffer size is too small based on a parameter that contains the buffer size. Of course, if you pass a parameter that says a buffer is of a set size, it best be at least that size, else the called function can trigger a crash.
Bottom line, know the API function you are calling. Many freebie examples on the net may be incorrect.
Re: Buffers & security question
Oh ok perfect - thanks both for the helpful advice & answers! :bigyello: