Search code examples
cstringstructvalgrind

Set a const char pointer in a struct


I'm attempting to write a program where I can create tokens such that each token has a constant type and value.

token.h:

#ifndef TOKEN_H
#define TOKEN_H
typedef struct{
    const char *type;
    const char *value;
}token;

token *gen_token(const char *r, const char *val);

#endif

token.c:

#include <stdlib.h>
#include <string.h>
#include "lib/token.h"

token *gen_token(const char *type, const char *val){
    token o = {type, val}, *out = &o;
    return out;
}

main.c:

#include <stdio.h>
#include <stdlib.h>
#include "lib/token.h"

int main(int argi, char *argv[]){
    const char th[] = "line", va[] = ":";

    token *t = gen_token(th, va);

    printf("(%s, %s)\n", t->type, t->value);

    return 0;
}

When I run this normally, no errors seem to occur, but when valgrind takes a look at it, it complains alot. Here is my log:

==4086== Memcheck, a memory error detector
==4086== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4086== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==4086== Command: ./a lol
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4E7FD4F: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C2EBE2: strlen (vg_replace_strmem.c:458)
==4086==    by 0x4E80D77: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C2EBF4: strlen (vg_replace_strmem.c:458)
==4086==    by 0x4E80D77: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4EAAEEB: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1307)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4EAAEF1: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1309)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4EAAF03: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1307)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4EAAF0D: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1309)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32EB3: is_overlap (vg_replace_strmem.c:137)
==4086==    by 0x4C32EB3: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32EB9: is_overlap (vg_replace_strmem.c:140)
==4086==    by 0x4C32EB9: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32EC7: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32ECC: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Conditional jump or move depends on uninitialised value(s)
==4086==    at 0x4C32F64: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C32F70: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086== 
==4086== Use of uninitialised value of size 8
==4086==    at 0x4C32F7E: __GI_mempcpy (vg_replace_strmem.c:1525)
==4086==    by 0x4EAAE23: _IO_file_xsputn@@GLIBC_2.2.5 (fileops.c:1327)
==4086==    by 0x4E80BBA: vfprintf (vfprintf.c:1637)
==4086==    by 0x4E871F8: printf (printf.c:33)
==4086==    by 0x1087AB: main (main.c:11)
==4086==  Uninitialised value was created by a stack allocation
==4086==    at 0x108785: main (main.c:10)
==4086==  (line, :)
==4086== 
==4086== HEAP SUMMARY:
==4086==     in use at exit: 0 bytes in 0 blocks
==4086==   total heap usage: 1 allocs, 1 frees, 1,024 bytes allocated
==4086== 
==4086== All heap blocks were freed -- no leaks are possible
==4086== 
==4086== For counts of detected and suppressed errors, rerun with: -v
==4086== ERROR SUMMARY: 36 errors from 14 contexts (suppressed: 0 from 0)

I'd much appreciate any help and/or critique on my code.


Solution

  • In function

    token *gen_token(const char *type, const char *val){
        token o = {type, val}, *out = &o;
        return out;
    }
    

    Variable out point to the local variable o, which is disposed after function returns. This is pure accident you can read those values after you calling a function. Hence a warning about

    Uninitialised value was created by a stack allocation

    Instead you need to allocate memory and copy values into it. NOTE you need to remember to free allocated memory before exiting you program, especially if you'd like to make valgrind happy :)