ufairiya / mongoose

Automatically exported from code.google.com/p/mongoose
MIT License
0 stars 0 forks source link

Issue in mg_printf with % characters #128

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. I tried to send some unicode characters after encoding the unicode
characters

2. Then I use mg_printf to print the data.

What is the expected output? What do you see instead?

But, the data seems to be cut in vsnprintf. It cut with the % characters.

Sample data I tried to display:

<SampleXml>
<Progress Name="Test1/%ce%a4%ce%b5%cf%84%ce%b1%ce%bc%ce%ad%ce%bd%ce%b7" />
</SampleXml>

When I call the mg_printf to print the above string, vsnprintf only
partially assign data on buf (ie, only before %). I tried with _vsnwprintf
but no luck.

What version of the product are you using? On what operating system?
Windows XP. Mongoose v2.8

If you need any further details, please let me know, Is there any one face
similar issue. or how to fix this issue.

Thanks in advance.

Original issue reported on code.google.com by asharud...@gmail.com on 11 Mar 2010 at 12:11

GoogleCodeExporter commented 9 years ago
Hi,

Further to my previous comment,

I have comment the "n = vsnprintf(buf, buflen, fmt, ap);" in mg_vsnprintf 
function
and replace that line with "n = snprintf(buf, buflen, "%s", fmt);" and the code 
seems
to be work.

Could any one update me the usage of vsnprintf in mongoose. Or shall I continue 
with
this snprintf fix.

Awaiting for reply.

Thanks.

Original comment by asharud...@gmail.com on 11 Mar 2010 at 3:29

GoogleCodeExporter commented 9 years ago
You are mixing printf-family '%' argument replacement coding with URI-encoded
characters which are represented as %XX codes. Feeding the latter to any
printf-derivative as the /format/-string will cause (very) ill effects because 
the
printf() will see those single '%' characters and assume those are introducing 
yet
another argument to be copied into the format string.

Hence either your printf("%s", str) solution is correct as now your URL-encoded
character sequence isn't in the format string any longer but instead part of a 
string
/argument/ which is copied into the result through the '%s' format element, or 
you
could mix URI-encoded characters and printf() format strings by 'escaping' the 
'%'
for those URI-encoded chars in there, e.g.

printf("%s/%%ce%%a4%%ce%%b5%%cf%%84%%ce%%b1%%ce%%bc%%ce%%ad%%ce%%bd%%ce%%b7", 
"Test1");

This is not a mongoose issue but a generic issue with using printf()-family
functions. To see literal '%' in the output, it must be escaped in the format 
string
as "%%".

Original comment by ger.hobbelt on 25 Apr 2010 at 10:00

GoogleCodeExporter commented 9 years ago
Thanks for your reply. I will check it once.

Original comment by asharud...@gmail.com on 26 Apr 2010 at 5:10

GoogleCodeExporter commented 9 years ago

Original comment by valenok on 3 Sep 2010 at 4:00

GoogleCodeExporter commented 9 years ago
I have missed to update to this thread. When I use mg_printf , it seems it 
failed to send all bytes for some reasons. Actually I am trying to send some 
encrypted/compressed content. I've tried it with even mongoose 3.4 (from the 
github). Actually I have changed the code as below, and its working fine. This 
is just for your reference. I have added the following function and use it.

int my_printf(struct mg_connection *, const char *fmt, int size, ...);
int my_printf(struct mg_connection *conn, const char *fmt, int size, ...)
{
    int len, toRet;
    va_list ap;
    long length = size;
    char* buf = NULL;
    buf = (char*)malloc(length);
    if (buf == NULL)
    {
        printf("my_printf failed. no of bytes.. %ld", length);
    }

    memset(buf, '\0', length);
    memcpy(buf, fmt, length);   

    toRet = mg_write(conn, buf, length);
    free(buf);
    return toRet;
}

Thanks,

Original comment by asharud...@gmail.com on 15 Dec 2012 at 10:03