2

I have written a small program on Mac OS X but I am getting the following error in the following function :

Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000 0x00007fff99b359c4 in strstr ()

/*
 * attrvalue(): parse an attribute value pair.
 *
 * takes a string of the form "ATTRIBUTE = VALUE" and returns the atrribute name
 * and value as seperate strings
 *
 */
  int
 attrvalue(char *entry, char **attr, char **value)
 {
    char           *copy;
    char           *p;

    if (!entry || *entry == '\0')
            return 1;

    copy = strdup(entry);

    *attr = strtok(copy, "=");
    *value = strtok(NULL, "\n");

    /* strip training whitespace from attr and value */
    p = strstr(*attr, "     ");
    if(p)
      *p = '\0';
    p = strstr(*value, "   ");
    if(p)
      *p = '\0';
    return (0);
}

Any idea what is wrong with this function here?

Thanks.

alk
  • 69,737
  • 10
  • 105
  • 255
  • In the debugger, you should be able to examine the value of `*attr` and `*value`. I'm used to the GDB console -- in the console, you can type the `up` command until you are in the right stack frame (the frame for `attrvalue`, instead of `strstr`), then type `p *value` or `p *attr` to see the value passed to `strstr()` and see if that value is `NULL`. I'm sure there's also a way to do this through the GUI. – Dietrich Epp Feb 10 '13 at 20:45
  • Yeah, if you want to learn the most, firing up the debugger is probably a better bet than my answer. :) – David Duncan Feb 10 '13 at 21:01
  • If I do this in gdb I see the following : Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000 0x00007fff99b359c4 in strstr () (gdb) – user2059593 Feb 10 '13 at 21:17
  • I then perform as you suggested : #1 0x000000010000336b in attrvalue () (gdb) p *attr No symbol table is loaded. Use the "file" command. (gdb) – user2059593 Feb 10 '13 at 21:20
  • "No symbol table" indicates you haven't compiled with debug (symbol) information. With GCC this is "-g"; elsewhere I'm not sure. – David Duncan Feb 10 '13 at 21:23
  • @user2059593: It appears as if perhaps you only wanted to remove trailing whitespace--if that's the case, you were really, really close, and I've updated my answer to show that. – David Duncan Feb 10 '13 at 21:31

1 Answers1

0

Revised answer: If you assume that there is no whitespace at the beginning of either attr or value (i.e., there is no space between the start of the line and the attr string, or the equal sign and the value string), then your program can be made to work with only a slight adjustment: change the multiple spaces in the second arguments to strstr() to single spaces. The revised code with a main() to demonstrate follows. If you wish to strip non-trailing whitespace, then, well, it's likely back to the debugger as you expand the problem or borrowing from the link in my original answer below.

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

/*
 * attrvalue(): parse an attribute value pair.
 *
 * takes a string of the form "ATTRIBUTE = VALUE" and returns the atrribute name
 * and value as seperate strings
 *
 */
  int
 attrvalue(char *entry, char **attr, char **value)
 {
    char           *copy;
    char           *p;

    if (!entry || *entry == '\0')
            return 1;

    copy = strdup(entry);

    *attr = strtok(copy, "=");
    *value = strtok(NULL, "\n");

    /* strip trailing whitespace from attr and value */
    p = strstr(*attr, " ");
    if(p)
      *p = '\0';
    p = strstr(*value, " ");
    if(p)
      *p = '\0';
    return (0);
}

int main()
{
    char *input = "asdf    =blah ";  // this works
    //char *input = " asdf = blah";  // this would not
    char *attr;
    char *value;

    attrvalue(input, &attr, &value);

    printf("attr =\"%s\"", attr);
    printf("value=\"%s\"", value);

    return 0;
}

Original answer: I'd use a different method of stripping out whitespace, as it's not even quite clear how strstr() would work as used in this situation. Here's the strstr() documentation, and here's a SO topic discussing the removal of whitespace in C.

Couple other thoughts:

  • if(p) should be if(p != NULL), as a nit.
  • What if second argument to strstr() is longer than the first?
Community
  • 1
  • 1
David Duncan
  • 1,225
  • 8
  • 14