Look at this piece of code:
Code:
1071 {
1072 b = strchr(a, '\0');
1073 finished = 1;
1074 }
1075
1076 strncpy(temp, a, (b-a));
1077 strcat(temp, ""); /* add a null at the end */
1078
1079 a = b + 1;
1080
When I was debugging the program I noticed that lie 1077 was being skipped for no reason at all.
Check out the GDB output:
1076 strncpy(temp, a, (b-a));
4: b = 0x5042b5 "/../test"
3: a = 0x5042b1 "test/../test"
2: simple_path = 0x5042b0 "/test/../test"
1: temp = '\0' <repeats 79 times>
(gdb) next
1079 a = b + 1;
4: b = 0x5042b5 "/../test"
3: a = 0x5042b1 "test/../test"
2: simple_path = 0x5042b0 "/test/../test"
1: temp = "test", '\0' <repeats 75 times>
Yes I did have the correct version of the program compiled...
On a side note:
when I'm dynamically allocating space for a string, which one of these pieces of code is correct?
Code:
new_string = malloc(strlen(old_string) + 1);
or
Code:
new_string = malloc( (strlen(old_string) + 1) * sizeof(char));