-3

I'm trying to write a wchar array to a file in C, however there is some sort of corruption and unrelevant data like variables and paths like this

c.:.\.p.r.o.g.r.a.m. .f.i.l.e.s.\.m.i.c.r.o.s.o.f.t. .v.i.s.u.a.l. .s.t.u.d.i.o. 1.0...0.\.v.c.\.i.n.c.l.u.d.e.\.x.s.t.r.i.n.g..l.i.s.t...i.n.s.e.r.t 

are written on to the file along with the correct data (example) I have confirmed that the buffer is null-terminated and contains proper data.

Heres my code:

        myfile = fopen("logs.txt","ab+");
        fseek(myfile,0,SEEK_END); 
        long int size = ftell(myfile);
        fseek(myfile,0,SEEK_SET);
        if (size == 0)
        {
            wchar_t bom_mark = 0xFFFE; 
            size_t written = fwrite(&bom_mark,sizeof(wchar_t),1,myfile); 
        }
// in another func
            while (true)
            {
            [..]
                unsigned char Temp[512];
                iBytesRcvd = recv(sclient_socket,(char*)&Temp,iSize,NULL);
                if(iBytesRcvd > 0 )
                {
                    WCHAR* unicode_recv = (WCHAR*)&Temp;
                    fwrite(unicode_recv,sizeof(WCHAR),wcslen(unicode_recv),myfile);
                    fflush(myfile);
                }
            [..]
            }

What could be causing this?

ᴘᴀɴᴀʏɪᴏᴛɪs
  • 7,169
  • 9
  • 50
  • 81

1 Answers1

1

recv() will not null-terminate &Temp, so wcslen() runs over the bytes actually written by recv(). You will get correct results if you just use iBytesReceived as byte count for fwrite() instead of using wcslen() and hoping the data received is correctly null-terminated (wide-NULL-terminated, that is):

fwrite(unicode_recv, 1, iBytesReceived, myfile);
Daniel Roethlisberger
  • 6,958
  • 2
  • 41
  • 59
  • I've tried what you suggested, but no luck still, also to wide-NULL-terminate it I used the last wchar (2 bytes) and set them equal to 0 (00 00) maybe I should have done something else? – ᴘᴀɴᴀʏɪᴏᴛɪs Apr 15 '12 at 17:37
  • Are you sure that `sizeof(wchar_t)` is `2` and not `4` on your system? – Daniel Roethlisberger Apr 15 '12 at 17:40
  • well int size2 = sizeof(wchar_t); debugged sais its 2 :/ – ᴘᴀɴᴀʏɪᴏᴛɪs Apr 15 '12 at 17:41
  • 1
    Impossible to tell what's the problem if you are not showing us the code you are using. In any case, `recv()` is not guaranteed to read the same amount of bytes in one go as you send with `send()` on the other end, so using `wcslen()` as you do is asking for trouble even if the sender would correctly terminate the data it sends and assuming you'd never have malicious senders. – Daniel Roethlisberger Apr 15 '12 at 17:46
  • I found the mistake and it was in the sender after all, due to incorrect array boundaries (sorry for that :( ). I'll adopt your suggestions for this one for safety, thanks for your support – ᴘᴀɴᴀʏɪᴏᴛɪs Apr 15 '12 at 17:50
  • You probably want your receiver to be resilient against sender mistakes. Or in other words, programming errors in your sender should not be able to crash your receiver. – Daniel Roethlisberger Apr 15 '12 at 17:52