Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

strcpy causing segfaults from source

Name: Anonymous 2010-08-12 14:42

A snippet in my code goes something like

puts(string1);
strcpy(string2, string1);
puts(string1);

but that 2nd call of puts results in a segfault. I thought strcpy only copies. wtf is going on? I couldn't recreate it though on its own so I think it must be this project.

If you guys have nothing to do, maybe you can have a look.
http://pastebin.ca/1915922
It's part of a larger project but this one here reads two lines of text from a given file and separates the tokens that are separated by commas and spaces.

Name: Anonymous 2010-08-12 14:44

Start with The Structure and Interpretation of Computer Programs. Then move to C.

Name: Anonymous 2010-08-12 14:47

Why don't you just use argv[1]?

Name: Anonymous 2010-08-12 14:50

>>3
lol ok. That shaved off a line or two.

btw, my larger problem actually happens a little bit later where I get another segfault at the next puts(b);

As if the 2nd line that was read, b, just disappears after that last strtok.

Name: Anonymous 2010-08-12 14:53

>>4
Wait, I mean that last strcpy.

puts(b);
strcpy(string1, someotherstring2);
puts(b);

that second puts(b) fails for some reason.

Name: Anonymous 2010-08-12 14:58

>>4
As if the 2nd line that was read, b, just disappears after that last strtok.
Because it does? strtok changes strings. But I don't think that that's the reason for the segfault, because it only happens at the second puts.
Or the bug is in the middle line, where string1 becomes some sort of an invalid pointer due to the use of strtok, and string2 gets overwritten with some random garbage. Or maybe you should have ensured that all your strings are 0-terminated? Like reading only 95 characters and manually setting the 96th to 0. Also, you should say char a[96]; since you already know how much memory you want to allocate.

Name: Anonymous 2010-08-12 14:58

>>5
Are you absolutely sure it's not strcpy that is causing the segfault?

Name: >>7 2010-08-12 15:03

Oh my bad. It's not strcpy, something more subtle is going on. It's a bit hilarious. HAVE FUN WITH THAT!!

PS. use a debugger.

Name: Anonymous 2010-08-12 15:13

Watching the common man try to learn C is hilarious!

Name: Anonymous 2010-08-12 15:25

Can you give a sample file.

Name: Anonymous 2010-08-12 15:52

man, I'm stumped. I remember though that it used to work. My old code made use of a loop but then I was having trouble and I realized that since there will always be 6 items per line, I might as well just call strtok 6 times manually.

It would actually make it all the way to parsing the 2nd line that was read but with last string of the 2nd line replacing 1st string of the 1st line for some reason.

I also had variables initializations left over from the loop and when I removed those (since they weren't being used) that's when shit stopped working.

Name: Anonymous 2010-08-12 15:53

>>11
I mean 5 items.

Name: Anonymous 2010-08-12 15:54

If copying takes place between objects that overlap, the behavior is undefined.

Name: Anonymous 2010-08-12 15:57

>>13
DON'T HELP HIM!!!

Name: Anonymous 2010-08-12 16:01

>>13
How do these two overlap?

Name: Anonymous 2010-08-12 16:04

>>15
string1 doesn't have a 0 at the end, so it goes all the way into string2.

Name: Anonymous 2010-08-12 16:08

THIS REMINDS ME OF MY FAVOURITE JOKE

Two strings walk into a bar and sit down. The bartender says, “So what’ll it be?”
The first string says, “I think I’ll have a beer quag fulk boorg jdk^CjfdLk jk3s d#f67howe%^U r89nvy~~owmc63^Dz x.xvcu”
“Please excuse my friend,” the second string says. “He isn’t null-terminated.

Name: Anonymous 2010-08-12 16:15

>>17
It's quite astounding, really, how properly balanced BBcode tags got into that random sequence.

Name: Anonymous 2010-08-12 16:32

>>18
Proof that God exists.

Name: Anonymous 2010-08-12 18:17

Where's Erika?

Name: Anonymous 2010-08-12 18:17

>>20
Fuck off, elliot

Name: Anonymous 2010-08-12 18:22

>>21
you're just mad because she's riding my dick and not yours, xarn.

Name: Anonymous 2010-08-12 18:36

>>20-22

Less of this.

Name: Anonymous 2010-08-12 18:43

>>23
Less of you.

Name: Anonymous 2010-08-12 18:50

Where's Erika?

Name: Anonymous 2010-08-12 20:48

>>25
Xarn raped and killed her. Not necessarily in that order.

Name: Anonymous 2010-08-12 20:48

You're using fixed size buffers, but not making sure that unknown input fits inside them. Don't do that. Use snprintf instead of strcpy, then you at least truncate overly long input instead of overflowing buffers.

Name: Anonymous 2010-08-12 21:02

>>27
Or use "%.25s" or whatever length in the format specifiers.

Name: Anonymous 2010-08-12 22:14

>>28
There's lots of ways to do it, but snprintf is straightforward and doesn't need yet another magic number that's just begging to become a bug later.

Name: Anonymous 2010-08-12 22:50

>>29
You need to provide it with the length of the buffer. That's still a magic number, you're just moving it somewhere else.

Name: Anonymous 2010-08-12 23:04

>>30
You use a macro for that, thus removing its magic number status. You can't do that (easily) inside format strings.

Name: Anonymous 2010-08-12 23:12

>>30
If the buffer is an array, you can simply write sizeof s. If the buffer is a pointer, you should always have its size available via some variable or macro.

Name: Anonymous 2010-08-13 7:08

Okay, I got around my first problem by forgetting about multidimensional arrays for strings and just making multiple arrays. Inelegant but fuck it, it works.

I have a new problem now though. But it seems to be similar to the first one. I have a while loop that parses a string for numbers and it goes


while(i < 4){
 strtok(NULL, ",");
 printf("%d", i);
 stats[4][i] = atoi(counter);
 printf("%d", i);
 i++;
}


I do this multiple times but for one of them, consistently the one for stats[4], the value of i jumps to the value of counter between the two printfs.  What the shit again? And thanks for the help so far.

Name: Anonymous 2010-08-13 7:11

>char x[4][32];
>strcpy(x[4],pch);       //Here's where it fucking happens.

Here you go

Name: Anonymous 2010-08-13 7:14

okay, wtf, I seemed to have fixed it by increasing the size of the stats array even though it really doesn't need it. Well whatever.

Name: Anonymous 2010-08-13 8:54

>>35
Learn to use a debugger already.

Name: Anonymous 2010-08-13 9:13

Okay, now I get it.

x[4] only gives you x[0] to x[3]

wow, why didn't I realize that.

desk(forehead);

Name: Anonymous 2010-08-13 9:17

>>37
No, you're an idiot. x[4] gives you x[4].

Name: Anonymous 2010-08-13 9:41

>>37
Uh-oh, reverse Sepples-style type declarations!

Name: Anonymous 2010-08-13 10:46

>>36
Debuggers are for programmers who can't program

Name: Anonymous 2010-08-13 12:38

>>40
I said learn to use a debugger. I know how to use one very well, which is why I never have to.

Name: Anonymous 2010-08-13 13:05

>>40
<insert Djikstra quote about programming being about putting bugs into programs here>

Name: Anonymous 2010-12-25 8:16

Name: Anonymous 2011-02-03 3:08

Name: Anonymous 2013-09-01 14:10


Assuming the axiom of choice and, given an infinite cardinal π and a non-zero cardinal μ, there exists a cardinal κ such that μ · κ = π if and only if μ ≤ π. It will be unique (and equal to π) if and only if μ < π.

Don't change these.
Name: Email:
Entire Thread Thread List