Question

I'm trying to write a function that adds a new struct into a linked list. Valgrind keeps giving me this error no matter what I do. Here is the code:

/* Stores a new address record to the linked list
 * 
 * Parameters:
 * first: pointer to the start of the linked list
 * name: name of the new record to be added (same name can be stored multiple times)
 * ad: IPv4 address to be stored with the name
 * 
 * Returns:
 * Pointer to the new node in linked list (that becomes the first node in list) */
struct addrRecord *storeIPv4(struct addrRecord *first, const char *name, const addr_4 *ad)
{
    if (first==NULL) {
        first=malloc(sizeof(struct addrRecord));
        if (!first)
             return NULL;
        strcpy(first->name,name);
        first->type=IPv4;
        strcpy(first->u.in4.a,ad);
        first->next=NULL;
        return first;
    }
    struct addrRecord *new=malloc(sizeof(struct addrRecord));
    if (!new)
        return NULL;
    strcpy(new->name,name);
    new->type=IPv4;
    strcpy(new->u.in4.a,ad);
    new->next=first;
    return new;
}

Here is the main code:

struct addrRecord *recs = NULL;
addr_4 ad4 = {{128, 214, 4, 64}};
recs = storeIPv4(recs, "www.example.com", &ad4);
if (!recs)
    return EXIT_FAILURE;

and finally the header:

#ifndef AALTO_ADDRESS_H
#define AALTO_ADDRESS_H

/* container for 32-bit IPv4 address */
typedef struct {
    char a[4];
} addr_4;

/* container for 128-bit IPv6 address */
typedef struct {
    char a[16];
} addr_6;

/* container for Unix domain address */
typedef struct {
    char a[108];
} addr_un;

typedef enum {
    NONE, // address not in use
    IPv4,
    IPv6,
    UNIX
} adType;

/* One node in linked list of name / address entries */
struct addrRecord {
    char name[20];
    adType type;
    union {
        addr_4 in4;
        addr_6 in6;
        addr_un un;
    } u;
        struct addrRecord *next;
};

/* Store IPv4 address to addrRecord */
struct addrRecord *storeIPv4(struct addrRecord *first, const char *name, const addr_4 *ad);

/* Store IPv6 address to addrRecord */
struct addrRecord *storeIPv6(struct addrRecord *first, const char *name, const addr_6 *ad);

/* Store Unix domain address to addrRecord */
struct addrRecord *storeUnix(struct addrRecord *first, const char *name, const addr_un *ad);

/* Find address from linked list starting from 'first' */
struct addrRecord *findAddress(struct addrRecord *first, const char *name);

/* Prints the address in address record to standard output */
void printAddress(struct addrRecord *ad);

#endif  /* AALTO_ADDRESS_H */

The error I get:

==358== Conditional jump or move depends on uninitialised value(s)
==358==    at 0x4C25897: strcpy (mc_replace_strmem.c:311)
==358==    by 0x403203: storeIPv4 (address.c:79)
==358==    by 0x401706: initRec (test_source.c:30)
==358==    by 0x401AB0: test_store (test_source.c:97)
==358==    by 0x406350: srunner_run_all (in /tmc/test/test)
==358==    by 0x402C5A: tmc_run_tests (tmc-check.c:121)
==358==    by 0x402915: main (test_source.c:324)

Sorry for the lengthy codes and thanks in advance.

Was it helpful?

Solution

This is just a guess, as your code is not a full example.

Try not to copy the addr_4 structure using strcpy. Instead use memcpy.

So, change this line :

strcpy(first->u.in4.a,ad);

to this :

memcpy(first->u.in4.a,ad,sizeof(*ad));

strcpy expects a null-terminated string, and ad may not be that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top