News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

sprintf_s in masm ?

Started by ewok, May 01, 2024, 02:15:05 AM

Previous topic - Next topic

ewok

Quote from: sudoku on May 01, 2024, 03:15:47 AMWhat are the arguments  "q:DWORD,w:DWORD" for?
That was part of desperate coding, at a point i started doing non-sense code/test

Quote from: NoCforMe on May 01, 2024, 02:40:26 AMOne thing wrong: in the original structure definition,
    char* string;
is a pointer to a character string, not a string itself as you coded.

You should have this for that member instead:
     stringPtr  dd ?
I thought of a struct was like an array, so it's just pointers inside a struct? i'll have to fix that.

Quote from: sudoku on May 01, 2024, 04:27:19 AMWhat version of Windows are you running, ewok?
win 10

Quote from: Vortex on May 01, 2024, 05:25:52 AMThe problem is that the import library msvcrt.lib built by the Masm32 package does not export this function
If one day there would be a new release of masm that could be a good addition!
Thanks also for your custom lib. I'm gonna fix my struct and try that lib

NoCforMe

Quote from: ewok on May 02, 2024, 01:31:47 AM
Quote from: NoCforMe on May 01, 2024, 02:40:26 AMOne thing wrong: in the original structure definition,
    char* string;
is a pointer to a character string, not a string itself as you coded.

You should have this for that member instead:
    stringPtr  dd ?
I thought of a struct was like an array, so it's just pointers inside a struct? i'll have to fix that.
A structure (C or assembly language, doesn't matter) is a collection which can contain anything (including other structures).

That field could be an actual string, as you had originally coded it, but in this case it's a pointer to a string instead (because the C definition was char *string). You can have strings--otherwise known as arrays of characters--in a structure; in fact, several Win32 structures, like LOGFONT, have them.

If the C definition had been char string[some_size], then you'd want to put a string (array) there instead.
Assembly language programming should be fun. That's why I do it.

Vortex

Quote from: sudoku on May 01, 2024, 05:28:39 AMUnder the hood, msvcrt calls "vsprintf_s" in case anyone is interested

Hi sudoku,

msvcrt.inc from the Masm32 package defines only this version :

externdef _imp__vsprintf:PTR c_msvcrt
    crt_vsprintf equ <_imp__vsprintf>

sudoku

I have rebuilt my msvcrt.lib here, after adjusting the include file. Thanks for that advice.

From that quote in your post, it should have been
QuoteUnder the hood, msvcrt calls "vsprintf_s" when the user calls "sprintf_s" in case anyone is interested
as illustrated by the image in that post. I thought it was obvious in the context of the thread, so I felt no need to elaborate. :smiley:
:eusa_naughty:

alCoPaUL

sounds like sprintf_s is for sanitizing db/characters/strings..

https://pastebin.com/m54wj4QL

replace the ' in the last line with ; to assemble and link...

alCoPaUL

you don't need to specify the size of the data/string/character to be processed.

and this is good for dynamic data/string/character processing. like you have a fixed buffer of zeroes in your code and you can just use that with the sprintf_s sanitizer and it will cut on the first ,0 that it encounters, whatever size is that provided that it won't exceed the buffer size..

sudoku

Quote from: alCoPaUL on May 16, 2024, 05:45:13 PMyou don't need to specify the size of the data/string/character to be processed.

and this is good for dynamic data/string/character processing. like you have a fixed buffer of zeroes in your code and you can just use that with the sprintf_s sanitizer and it will cut on the first ,0 that it encounters, whatever size is that provided that it won't exceed the buffer size..
Now suppose you have a case where the string is NOT zero terminated??? It would be good to specify the string size in that instance, no? Else as you say 'it will cut on the first ",0" that it encounters' ( the quotes inferred by your post but not explicitly stated there). Where would that be (the first ",0"), in the case of a string that is not zero terminated?? I see a possible buffer overflow (and program crash) in that case.
:eusa_naughty:

alCoPaUL

Quote from: sudoku on May 17, 2024, 02:40:01 AM
Quote from: alCoPaUL on May 16, 2024, 05:45:13 PMyou don't need to specify the size of the data/string/character to be processed.

and this is good for dynamic data/string/character processing. like you have a fixed buffer of zeroes in your code and you can just use that with the sprintf_s sanitizer and it will cut on the first ,0 that it encounters, whatever size is that provided that it won't exceed the buffer size..
Now suppose you have a case where the string is NOT zero terminated??? It would be good to specify the string size in that instance, no? Else as you say 'it will cut on the first ",0" that it encounters' ( the quotes inferred by your post but not explicitly stated there). Where would that be (the first ",0"), in the case of a string that is not zero terminated?? I see a possible buffer overflow (and program crash) in that case.

but my example has a 999426 zeroes as a buffer..

you put 76 bytes on a 999426 zeroes as a buffer, printf will just take the 76 bytes and the first zero (thanks to sprintf_s processing the buffer as "%s")
and say your code still have to process 6675 bytes before printing it, printf will just take 6675 bytes and the first zero, again thanks to sprintf_s.

dynamic data processing will be stifled by doing strlen() everytime you pass a buffer..

and it's allowed, as you can see (if you can assemble and link my source)..

sudoku

Quote from: alCoPaUL on May 17, 2024, 03:32:17 AMand it's allowed, as you can see (if you can assemble and link my source)..

b db 999426 DUP(0)Nah, I'll pass. One megabyte-ish in .data ??
If you have a smaller example, I will consider it.

You can post your source here, did you know? It would make it much easier if anyone is interested in it, rather than having to copy and paste your pastebin link (its not clickable - btw) and go to a third party site to obtain your code.
:eusa_naughty:

alCoPaUL

Quote from: sudoku on May 17, 2024, 03:47:23 AM
Quote from: alCoPaUL on May 17, 2024, 03:32:17 AMand it's allowed, as you can see (if you can assemble and link my source)..

b db 999426 DUP(0)Nah, I'll pass. One megabyte-ish in .data ??
If you have a smaller example, I will consider it.

You can post your source here, did you know? It would make it much easier if anyone is interested in it, rather than having to copy and paste your link (its not clickable - btw) and go to a third party site to obtain your code.

it's the era of datasscience..

i was supposed to post that on this forum the time i finished that but the forum was down..

again, sprintf_s formats that to %s and cuts the rest of the zeroes and finally you pass the buffer (the string data and ,0) to printf, printing that to %s.

so what's happening is double sanitization to %s..

clear from meme buffer overflow..

and when the code is using the 999426 zeroes as a buffer and your code is manipulating data to information and the maxed string that it manipulates is 9999 bytes, it's always underflowing...

but when it assembles the final string by combining 9999 and 9999 and 9999, it still underflowing the buffer coz that's only 29997 bytes inside a 999426 zero buffer...

and sprintf_s only takes the 29997 bytes & ,0 from that humongous buffer to pass to printf....


sudoku

Quote from: alCoPaUL on May 17, 2024, 04:19:03 AMi was supposed to post that on this forum the time i finished that but the forum was down..
Ah, okay.
:eusa_naughty:

alCoPaUL

source code & essential files in the zip..

alCoPaUL

#27
masm32 version

; sprintf_s + printf
; by alCoPaUL [GIMO]
; 5/16/2024 NYC
;
; ml.exe printf_s32.asm /link /subsystem:console /defaultlib:32msvcrt.lib /entry:start [or /entry:main (will assemble and link with warning)]
;
.686
.model flat,stdcall
extern printf:proc
extern sprintf_s:proc
.code
start:
main proc
enter 0,0
push offset x
push offset i
push f
push offset b
call sprintf_s
push eax
push offset b
push offset i
call printf
leave
ret
main endp
.data
f equ 10               ; Scale This Length
x db 'Thereupon',0     ; Manually Or Use StrLen()
b db 999426 DUP(0)     ; As Usual, Humongous Buffer
i db 25h,73h,0
end start

sudoku

Tried to run your executable

I am quite sure that the "32msvcrt.lib" that you are using is not for msvcrt.dll, but rather a dll from the vcruntime dll shown in the message box. A non-starter for me.
:eusa_naughty:

alCoPaUL

Quote from: sudoku on May 17, 2024, 12:19:08 PMTried to run your executable


the library (.lib) is from visual studio 2010, needs the appropriate redistributable runtimes..