Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | James Kuyper <jameskuyper@verizon.net> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: uninitialised value & valgrind |
| Date | 2012-06-26 13:53 -0400 |
| Organization | A noiseless patient Spider |
| Message-ID | <4FE9F706.6060301@verizon.net> (permalink) |
| References | <jscobj$amb$1@speranza.aioe.org> |
On 06/26/2012 12:35 PM, Mark wrote:
> Hello
>
> I'm working with a legacy code that I link to my application, and run it
> under 'valgrind' tool to catch memory leaks, corrupted memory etc. So here
> is what it looks like:
>
> #define VLOG_MSG_MAX_DATA_SIZE 1024
> struct vlog_msg_header
> {
> u_int32_t vmh_vr_id;
> u_int16_t vmh_msg_type;
> module_id_t vmh_mod_id;
> u_int32_t vmh_proc_id;
> s_int8_t vmh_priority;
> u_int32_t vmh_data_len;
> };
>
> struct vlog_msg
> {
> struct vlog_msg_header vms_msg_hdr;
> char vms_msg_data[VLOG_MSG_MAX_DATA_SIZE+1];
> };
>
> int vlog_client_send_message (struct vlog_msg *msg)
> {
> ...
> write (sock, (void *) msg, msg_len); /* line 103. */
> }
>
> int vlog_client_send_ctrl_msg ()
> {
> struct vlog_msg_header header;
>
> /* initialise 'header' .. */
>
> vlog_client_send_message ((struct vlog_msg *)&header); /* line 41*/
>
> return RES_OK;
> }
>
> Valgrind reports:
>
> ==1634== Syscall param write(buf) points to uninitialised byte(s)
> ==1634== at 0x6D2D22E: __write_nocancel (syscall-template.S:82)
> ==1634== by 0x8116E68: vlog_client_send_message (vlog_client.c:103)
> ==1634== by 0x8116EBF: vlog_client_send_ctrl_msg (vlog_client.c:41)
> ==1634== by 0x8116EE6: vlog_client_connect_cb (vlog_client.c:211)
> ==1634== by 0x80C9A3F: message_client_start (message.c:597)
> ==1634== by 0x8116BA8: vlog_client_start (vlog_client.c:294)
> ==1634== by 0x8116DA1: vlog_client_create (vlog_client.c:255)
> ==1634== by 0x80C8FF1: openzlog (log.c:116)
> ==1634== by 0x805AF95: ldp_start (ldp_main.c:85)
> ==1634== by 0x8049F39: main (ldp.c:167)
> ==1634== Address 0xfe541aee is on thread 1's stack
> ==1634== Uninitialised value was created by a stack allocation
> ==1634== at 0x8116E7F: vlog_client_send_ctrl_msg (vlog_client.c:32)
>
> I assume valgrind complains because vlog_client_send_message() expects to
> get object of type 'struct vlog_msg', but instead we pass 'struct
> vlog_msg_header' and everything beyond it (what is supposed to be occupied
> by 'char vms_msg_data[VLOG_MSG_MAX_DATA_SIZE+1]' is undefined. Does it sound
> correct ?
>
> My question is -- is it valid to typecast this way or it involves undefined
> behaviours?
Your code has undefined behavior. vlog_client_send_msg() expects to
receive a pointer to an actual vlog_msg struct, and is entitled to
access msg->vms_msg_data, which in your case doesn't exist. This is
called "lying to the compiler", and behavior such as you describe is a
normal consequence of lying about such things.
The messages you're getting from valgrind suggest that something is
attempting to access the non-existent message data inside of __write().
You don't show us how msg_len is calculated. However, if it's greater
than sizeof(struct vlog_msg_header), that would explain the messages
from valgrind.
I'd recommend figuring out why msg_len Is greater than that limit - it
suggests a serious disconnect between what you think you're doing and
what you're actually doing. The most plausible kind of mistake would be
failing to set header.vmh_data_len to 0.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
uninitialised value & valgrind "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2012-06-26 12:35 -0400
Re: uninitialised value & valgrind James Kuyper <jameskuyper@verizon.net> - 2012-06-26 13:53 -0400
Re: uninitialised value & valgrind "Mark" <mark_cruzNOTFORSPAM@hotmail.com> - 2012-06-26 14:07 -0400
Re: uninitialised value & valgrind James Kuyper <jameskuyper@verizon.net> - 2012-06-26 15:42 -0400
Re: uninitialised value & valgrind Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-06-27 02:09 +0200
csiph-web