flat assembler
Message board for the users of flat assembler.

Index > Heap > %p or %.8X ?

Author
Thread Post new topic Reply to topic
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
under linux %p add 0x before, but whats the diffrence under windows?
Post 01 May 2008, 14:09
View user's profile Send private message Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap
%p is for void*, %X is for unsigned int. There is no guarantee that any pointer can be represented in an unsigned int.

_________________
(French only) http://dap.developpez.com
Post 01 May 2008, 17:00
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
void and unsigned int = 32 bits
what do you mean? i dont understand.
Post 01 May 2008, 17:06
View user's profile Send private message Reply with quote
mattst88



Joined: 12 May 2006
Posts: 260
Location: South Carolina
mattst88
asmrox wrote:
void and unsigned int = 32 bits
what do you mean? i dont understand.


And on 64-bit platforms void * is 64-bits, while int is only 32-bit.
Post 01 May 2008, 17:14
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
so on 64bit platform this:
Code:
char buff[9];
sprintf(buff, "%p", 666);    

= buffer overflow?


...oops Embarassed


Last edited by asmrox on 01 May 2008, 18:17; edited 1 time in total
Post 01 May 2008, 17:42
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
= buffer overflow?

this is buffer overflow on 32bit platform too Wink
Post 01 May 2008, 17:56
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
And this is a bug too, it should be
sprintf(buff, "%p", (void*)666);

666 by itself is int, eg. 32 bit
Post 01 May 2008, 17:57
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap
asmrox wrote:
void and unsigned int = 32 bits
what do you mean? i dont understand.

You can only assume that void* can contain any address and that int is at least 16 bits wide.

I ran that code on Windows XP :
Code:
printf ("%p %X\n", (void*)0x100, 0x100);    


Output :
Code:
00000100 100    


asmrox wrote:
so on 64bit platform this:
Code:
char buff[8];
sprintf(buff, "%p", 666);    

= buffer overflow?


...oops Embarassed

This should be quite portable :
Code:
char buff[sizeof(void*)*2 + 1];
sprintf(buff, "%p", (void*)666);    


I'm not sure that every system will print 666 in hexadecimal though. It is safer to use snprintf() but it's C99-only. On Windows you can use StringCbPrintf() instead.

_________________
(French only) http://dap.developpez.com
Post 01 May 2008, 18:10
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
forgot about null wich sprintf add
Quote:

And this is a bug too, it should be
sprintf(buff, "%p", (void*)666);

666 by itself is int, eg. 32 bit

i never use casts, love -w option in gcc

you sure StringCbPrintf() is a function?
Post 01 May 2008, 18:23
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
Quote:
i never use casts, love -w option in gcc

maybe that's why you do such bugs Razz
Post 02 May 2008, 08:05
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
one thing more

whats the output on 64bit platform?
Code:
push -1
push -1
push f ;'%.8X',0
call [printf]
    


and how can i set maximum number of characters to output?
like .8 <-will padd by 0's, but whats on 64bit platform where 1 stack argument = 64 bits?
Post 09 May 2008, 23:58
View user's profile Send private message Reply with quote
Xorpd!



Joined: 21 Dec 2006
Posts: 161
Xorpd!
asmrox wrote:

whats the output on 64bit platform?
Code:
push -1
push -1
push f ;'%.8X',0
call [printf]
    


The output depends on the contents of rcx, rdx, and r8 (Windows) or rdi rsi, rdx (Linux). See Agner Fog's calling conventions manual for details.
Post 10 May 2008, 17:00
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
so is %08X overflow on 64bit?
Post 15 May 2008, 17:10
View user's profile Send private message Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Btw, iirc %p is an extension to printf - so you shouldn't use it if you want to be reeeeeally portable. But it's OK for debug traces, and better than %X (if you want to support both 32- and 64-bit, anyway).
Post 16 May 2008, 00:08
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
ok, but is %08X is an overflow Question Question
Post 17 May 2008, 00:18
View user's profile Send private message Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
bump tell me ffs Exclamation Exclamation
Post 17 May 2008, 22:06
View user's profile Send private message Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap
f0dder wrote:
Btw, iirc %p is an extension to printf - so you shouldn't use it if you want to be reeeeeally portable.

%p is part of C89 but what it displays pointers is implementation-dependant.

asmrox wrote:
ok, but is %08X is an overflow Question Question

No if the argument has the correct type (unsigned int).

_________________
(French only) http://dap.developpez.com
Post 18 May 2008, 11:02
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
grrr unsigned int = 4 bytes?
push on 64 = 4 bytes?
Post 18 May 2008, 21:00
View user's profile Send private message Reply with quote
dap



Joined: 01 Dec 2007
Posts: 61
Location: Belgium
dap
asmrox wrote:
grrr unsigned int = 4 bytes?
push on 64 = 4 bytes?

On x86-64 yes, but the first parameters of a passed by the registers so you don't have to worry about the stack.

_________________
(French only) http://dap.developpez.com
Post 19 May 2008, 10:06
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.