Unix Technical Forum

Re: distrust snprintf returnvalue

This is a discussion on Re: distrust snprintf returnvalue within the lucky.openbsd.tech forums, part of the OpenBSD category; --> OK, scratch that. Since the manpage is confusinging me I wrote this to get insight in what happens: % ...


Go Back   Unix Technical Forum > Unix Operating Systems > OpenBSD > lucky.openbsd.tech

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-22-2008, 12:27 PM
Han Boetes
 
Posts: n/a
Default Re: distrust snprintf returnvalue

OK, scratch that.

Since the manpage is confusinging me I wrote this to get insight
in what happens:

% cat test.c
#include <stdio.h>
#include <string.h>

int
main(void)
{
int ret;
int max = 6;
char returnstring1[8] = "yadadad";
char returnstring2[6] = "yadad";
char returnstring3[4] = "yad";
char returnstring4[2] = "y";

printf("strlen(returnstring1) = %i \n", strlen(returnstring1));
printf("strlen(returnstring2) = %i \n", strlen(returnstring2));
printf("strlen(returnstring3) = %i \n", strlen(returnstring3));
printf("strlen(returnstring4) = %i \n", strlen(returnstring4));

ret = snprintf(returnstring1, max, "%s%s", "foo", "bar");
printf("ret = %i, returnstring1 = %s, lenght = %i\n", ret, returnstring1, strlen(returnstring1));

ret = snprintf(returnstring2, max, "%s%s", "foo", "bar");
printf("ret = %i, returnstring2 = %s, lenght = %i\n", ret, returnstring2, strlen(returnstring2));

ret = snprintf(returnstring3, max, "%s%s", "foo", "bar");
printf("ret = %i, returnstring3 = %s, lenght = %i\n", ret, returnstring3, strlen(returnstring3));

ret = snprintf(returnstring4, max, "%s%s", "foo", "bar");
printf("ret = %i, returnstring4 = %s, lenght = %i\n", ret, returnstring4, strlen(returnstring4));

return 0;
}

So if you are not so sure what the result will be, please write
down what you think it will be and only then compile and see what
it does.

If you do know what happens, /me bows for you in awe, and please
tell me why it does. Because it doesn't make sense to me. I
expected really something different.



# Han

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 10:28 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com