2

Following are my code :

#include "stdio.h"
#include "time.h"
#include "string.h"
#include "cJSON.h"
#include "stdlib.h"
int checkUserRole(char *cmd);
int main(){

    char *cmd = "zwr ^A(\"A\")"; //string: zwr ^A("A")
    int v = checkUserRole(cmd);
    printf("%d",v);
    return 0;
}
int checkUserRole(char *cmd)
{
    const char *u ;
    u = getenv("USER");
    char *token;
    char *cmd_w_g;
    char *limiter ;
    if(strstr(cmd,"(") != NULL){
        limiter = "(";
        token = strtok(cmd,limiter);
        cmd_w_g = token;
    }
    char *cmd_qualifier;
    limiter = " ^";
    cmd_qualifier = strtok(cmd_w_g,limiter);
    char *cmd_q;
    cmd_q = cmd_qualifier;
    char *gbl ;
    gbl = strtok(NULL,limiter);


    char *fileName = "roles.cfg";
    char buff[512];
    FILE *file = fopen(fileName, "rt");
    char fileContent[1000000];

    while(fgets(buff, sizeof buff, file) != NULL){
         sprintf(fileContent,"%s%s",fileContent,buff);
    }   
    cJSON *root = cJSON_Parse(fileContent);
    cJSON *gbl_json = cJSON_GetObjectItem(root,gbl);
    cJSON *user_json =  cJSON_GetObjectItem(gbl_json,u);
    cJSON *cmd_json =  cJSON_GetObjectItem(user_json,cmd_q);
    char *role = cmd_json->valuestring;
    if(strstr(role,"true") != NULL){
        return 1;
    } else {
        return 0;
    }
}

and json file 'roles.cfg' using in code:

{
  "A": {
    "root": {
      "set": "true",
      "kill": "true",
      "zwrite": "true"
    },
    "abc": {
      "set": "true",
      "kill": "false",
      "zwrite": "true"
    }
  },
  "B": {
    "root": {
      "set": "false",
      "kill": "false",
      "zwrite": "true"
    },
    "abc": {
      "set": "true",
      "kill": "true",
      "zwrite": "true"
    }
  }
}

I used strok to split some text , but I get Segmentation fault. error when execute

Following are debug output:

Reading symbols from ./test2...done.
(gdb) b 1
Breakpoint 1 at 0x400b55: file test2.c, line 1.
(gdb) r
Starting program: /home/insight/test2 

Breakpoint 1, main () at test2.c:9
9       char *cmd = "zwr ^A(\"A\")";
(gdb) n
10      int v = checkUserRole(cmd);
(gdb) s
checkUserRole (cmd=0x4046c4 "zwr ^A(\"A\")") at test2.c:15
15  {
(gdb) n
17      u = getenv("USER");
(gdb) n
21      if(strstr(cmd,"(") != NULL){
(gdb) n
22          limiter = "(";
(gdb) n
23          token = strtok(cmd,limiter);
(gdb) n

Program received signal SIGSEGV, Segmentation fault.
strtok () at ../sysdeps/x86_64/strtok.S:186
186 ../sysdeps/x86_64/strtok.S: No such file or directory.
(gdb) 

I have no idea why it get error at that line ,I check all paramter to strtok , seem it have no problem //Update : have no warning when compiling:

insight@insight-ubuntu64:~$ gcc -g -o test2 test2.c cJSON.c -lm
insight@insight-ubuntu64:~$ 
Ryo
  • 995
  • 2
  • 25
  • 41
  • 1
    You're trying to modify a string literal. Your code can be reduced to `"zwr ^A(\"A\")"[6] = '\0';`. – melpomene Oct 09 '15 at 11:02
  • this has been asked sooo many times. the hint is that strtok takes `char*` as argument, not `const char*` – AndersK Oct 09 '15 at 11:09
  • Try `gcc -Wall -Wextra -pedantic -Wwrite-strings`. – melpomene Oct 09 '15 at 11:10
  • 1
    @CyberSpock That seems to be a different problem. Dud you mean something like http://stackoverflow.com/questions/8957829/strtok-segmentation-fault?rq=1? – melpomene Oct 09 '15 at 11:12
  • @melpomene guess I took the wrong one of the zillion of questions regarding strtok :) – AndersK Oct 09 '15 at 11:13
  • 1
    On SO we ask people to post a [minimal, complete, verifyable example](http://stackoverflow.com/help/mcve). In your case, that would have been four lines of code (not counting the include and the `int main() {}`, because all that is really relevant to your question is the `strtok()`: `char * cmd = "zwr ^A(\"A\")"; char * limiter = "("; char * token; token = strtok( cmd, limiter );` -- This has the added advantage of not requiring cJSON headers to compile. Trying to *get* a minimal example is an *important* debugging technique you should adopt. – DevSolar Oct 09 '15 at 11:15
  • thanks , I solved my problem ,but I have learned more than I ask on this topic :) – Ryo Oct 09 '15 at 11:23

1 Answers1

5

strtok modifies the original string. And you pass it a string literal which you can't modify. You should declare and initialize cmd like this-

 char cmd[] = "zwr ^A(\"A\")"; //string: zwr ^A("A")

Also in function int checkUserRole(char *cmd) -

char fileContent[1000000];   // maybe use a pointer instead and allocate memory on heap
ameyCU
  • 16,489
  • 2
  • 26
  • 41