Le 2012-08-12 17:41, songbird a écrit :
> Francis Giraldeau wrote:
> ...
>> Hi,
>>
>> Valgrind reports an invalid read, while I think the program is valid.
>> The program uses stpncpy() instead of strcpy():
>>
>> #include <string.h>
>> #include <stdlib.h>
>> #include <stdio.h>
>>
>> int main(int argc, char **argv) {
>>     char *dst =3D (char *) calloc(4, 1);
>>     char *src =3D (char *) calloc(4, 1);
>>     src[0] =3D 'a';
>>     src[1] =3D 'b';
>>     src[2] =3D 'c';
>>     src[3] =3D '\0';
>>     dst =3D stpncpy(dst, src, 3);
>>     printf("%s %s\n", src, dst);
>> }
>>
>> The error is the following:
> ....
> 
>   from the manpage:
> 
>   "RETURN VALUE
>        stpncpy() returns a pointer to the terminating null byte in  dest,  or,
>        if dest is not null-terminated, dest + n."
> 

Of course you are right! Thought, I just tested without updating dst and
the problem is raised anyway. The error is not caused in the printf(),
but in the stpncpy().

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argv) {
    char *dst = (char *) calloc(4, 1);
    char *src = (char *) calloc(4, 1);
    src[0] = 'a';
    src[1] = 'b';
    src[2] = 'c';
    src[3] = '\0';
    stpncpy(dst, src, 3);
    //printf("%s %s\n", src, dst);
    free(dst);
    free(src);
}

Cheers,

Francis


Attachment: smime.p7s
Description: Signature cryptographique S/MIME

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Valgrind-users mailing list
Valgrind-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-users

Reply via email to