user1819801
user1819801

Reputation: 3

Invalid read of size 1 Strcpy

I keep getting a valgrind error of Invalid read of size 1 and i can't determine why.

What is causing the error?

==24647== Invalid read of size 1
==24647==    at 0x40258EA: strcpy (mc_replace_strmem.c:437)
==24647==    by 0x8048606: main (source.c:26)
==24647==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==24647==
==24647==
==24647== Process terminating with default action of signal 11 (SIGSEGV)
==24647==  Access not within mapped region at address 0x0
==24647==    at 0x40258EA: strcpy (mc_replace_strmem.c:437)
==24647==    by 0x8048606: main (source.c:26)
==24647==  If you believe this happened as a result of a stack
==24647==  overflow in your program's main thread (unlikely but
==24647==  possible), you can try to increase the size of the
==24647==  main thread stack using the --main-stacksize= flag.
==24647==  The main thread stack size used in this run was 16777216.

And here is my code below, and i commented on the line where the error is detected (source.c:26).

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

int main()
{

    char input[100];

    char name[100];

    char age[100];

    int agee[100];

    fgets(input,sizeof(input),stdin); //i scan in the string and store int char array called input.

    char *charpoint;//declare pointer character

    charpoint=strtok(input,"\"");//run strtoken with quotation marks as second part of argument.

    strcpy(name,charpoint);

    char * charpoint2=strtok(NULL,"\",");

    strcpy(age,charpoint2); //This line is where the error occurs. line 26

    sscanf(age,"%d",&agee[0]);

    printf("%s %d",name, agee[0]);

    system("pause");

    return 0;

}

Upvotes: 0

Views: 3437

Answers (2)

Raam
Raam

Reputation: 10906

Couple of things about your program.

  1. Clear all the arrays before use, this will ensure that the no garbage is read in. You can do this by using memset or just char input[100] = {0};
  2. After reading in the data, ensure that input is null terminated by explicitly setting input[99] = '\0'. This is to ensure that the input can never exceed the size of the array
  3. Do a null check on the pointers returned by strtok, there is no guarantee that you get what you expect. Handle nulls appropriately. My hunch is that charpoint2 is coming back as null for you and hence the error.

Upvotes: 0

Ben Jackson
Ben Jackson

Reputation: 93860

From the manual page (emphasis mine):

The strtok() and strtok_r() functions return a pointer to the beginning of each subsequent token in the string, after replacing the token itself with a NUL character. When no more tokens remain, a null pointer is returned.

From your error

==24647==  Address 0x0 is not stack'd, malloc'd or (recently) free'd

So your pointer charpoint2 is NULL, meaning your previous strtok call did not find what you expected. You should check for that possibility and print an error about the format of the input. And of course you should verify that your strtok call does what you intended.

Upvotes: 7

Related Questions