darksky
darksky

Reputation: 21039

x86 - C's sscanf function

So I have the following code segment in x86 assembly:

mov $0x0, %eax
callq  400ac8 <__isoc99_sscanf@plt>
cmp    $0x5,%eax
jg     40152d <this_function+0x3d>   -----> jumps to add two lines down
callq  4014b a <error_program>
add    $0x18,%rsp
retq

Now in the first line, it sets %eax as a zero. It then calls sscanf and then checks whether %eax is 5. If it is larger than 5, it will continue otherwise terminate. So a simple C code which I made:

eax = 0;
sscanf();
if (eax < 5) error_program();
return;

This is an object dump of a binary file so I am sure that it is correct. However, eax will always be zero and the error will fire up. Am I right on that? Or is it possible that sscanf would manipulate eax?

Thanks

Upvotes: 3

Views: 11458

Answers (2)

Zhong Zhu
Zhong Zhu

Reputation: 73

Short answer is Yes, sscanf will definitely modify %eax. %rax is the default register functions put their return value in, always.

I did some research and it says this:

On success, the function returns the number of items in the argument list successfully filled. This count can match the expected number of items or be less (even zero) in the case of a matching failure. In the case of an input failure before any data could be successfully interpreted, EOF is returned.

see more information here: http://www.cplusplus.com/reference/cstdio/sscanf/

Upvotes: 3

Scott Hunter
Scott Hunter

Reputation: 49883

It is common for functions to pass the return value back in eax; at the very least, it is not guaranteed to be preserved. So perhaps this code is checking to make sure that sscanf is finding at least 5 items?

Upvotes: 4

Related Questions