1
void TestSegFunction(void)
{
     int i = 0;
     char *str = "\"{\"loop_number\":1}\""; // line 410
     char *str = "{\"loop_number\":1}"; // line 411
     json_object *pstObj = NULL;
     json_object *sonPstObj = NULL;
     pstObj = json_tokener_parse(str);    // line 414
     if (NULL == pstObj)
     {
         printf("%s : json_tokener_parse failed.\n", __FUNCTION__);
     }
     else
     {
         json_object_object_foreach(pstObj, key1, val1) 
         {
             if (0 == strcmp(key1, LOOP_NUMBER))
             {
                 i = json_object_get_int(val1);
                 printf("i = %d\n", i);
             }
         }
     }
 }

As shown in lines 410 and 411, if 410 lines of code are used, there will be a segment error in 414 lines of function calls. If 411 lines of code are used, there will be no error in 414 lines, because this function is called by others, and they may enter an error string. I don't want to see the segment error to stop the program. Is there any way to avoid this kind of paragraph error?

Hitokiri
  • 3,607
  • 1
  • 9
  • 29
yanzhang.guo
  • 107
  • 2
  • 17
  • Is your question not about the invalid JSON object represented by the first string, but rather how to validate it first so you don't get the crash? – Some programmer dude May 19 '20 at 07:29
  • And while it often is unlikely, have you considered that there is a bug in the JSON library you use? Have you thought to create a proper [mcve] (which can be easily copy-pasted in full to replicate the crash) and then report it as a bug to the library maintainers? Which version of the library are you using? Are there later versions available where this might be fixed (if it's a bug in the library)? – Some programmer dude May 19 '20 at 07:30
  • yes, I think i can know in advance that it is an invalid JSON string, so I can avoid this crash. The library is from https://github.com/json-c/json-c. – yanzhang.guo May 19 '20 at 07:38
  • @yanzhang.guo Any idea how to go about this one? https://stackoverflow.com/questions/65890286/modifying-c-json-string – ajfbiw.s Jan 26 '21 at 18:49

2 Answers2

2

The problem is that I'm trying to iterate over something that isn't of type json_type_object. I need to add a check like

if (json_object_get_type(pstObj) != json_type_object) {
   ...handle error...
}

before the json_object_object_foreach.

This answer comes from https://github.com/json-c/json-c/issues/623.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
yanzhang.guo
  • 107
  • 2
  • 17
  • Sorry, I quoted the author's answer directly from GitHub. – yanzhang.guo Jun 28 '20 at 15:18
  • 1
    Then you should quote it accordingly if you copy someone else's text. And some proper formatting would improve the answer as well. – Gerhardh Jun 29 '20 at 07:05
  • Hello, I still do not have the right to ask questions, I have deleted the unclear questions, and I have got the answers I want for other questions. I have checked my question many times, but I still don't know what kind of modification I should make? Can you tell me, thank you! – yanzhang.guo Jun 29 '20 at 13:25
  • Also, why are you posting your answer as community wiki? – 10 Rep Jul 01 '20 at 02:16
  • @ 10 Rep Sorry, is there a problem – yanzhang.guo Jul 01 '20 at 02:33
  • I give up. I have no way to solve the problem that I'm forbidden to ask. It's impossible for me to reproduce the original code. The previous code bug has been solved. – yanzhang.guo Jul 01 '20 at 02:40
0

I makes sense that code line 410 does not work, while 411 does.

char *str = "\"{\"loop_number\":1}\"";

This is not valid JSON, since a JSON can not start with ", what this code line is in fact doing. It gives you the string "{"loop_number": 1}}". Just use c's print function to verify. But a JSON can start with a {.

char *str = "{\"loop_number\":1}";

which gives you the string {"loop_number": 1} (See, no " around the { }).

I hope this helps.

EDIT

As the code breaks on line 414, you may use json_tokener_parse_ex directly. In fact, json_tokener_parse seems to simply wrap json_tokener_parse_ex. If you therefore directly use it as described in the documentation and as in here you maybe solve this issue. It is however weired, as this answer advises to solve your problem the way you did it. Maybe it is outdated?

MacOS
  • 1,149
  • 1
  • 7
  • 14
  • yes, i know it is an invalid JSON string, i just want to avoid this invalid JSON string. – yanzhang.guo May 19 '20 at 07:47
  • What do you mean with avoid? Is it a user input? Or returned form an API? I'm asking because I thought you have full control over what will be in the input to your program based on the example. Of course, this might be my missinterpretation of the code. – MacOS May 19 '20 at 07:51
  • The above code is just a test. I intend to encapsulate it as a function for others, but others may not input a correct JSON string. If so, my program will crash. – yanzhang.guo May 19 '20 at 07:56
  • I see. But if it accepts input (from whatever source), you have no choice but live with it. You already checked if the input is a valid json in [code lines 414-418](https://stackoverflow.com/questions/37773273/valid-json-check-in-c-language). I don't see what else you can do? If it is invalid, it is invalid. You may implement strategies for fixing the most commong problems, but thats about it. Maybe you can add an explanation into `json_tokener_parse failed` in the print statement to something along the lines `json_tokener_parse failed. This might indicate an invalid JSON.`. – MacOS May 19 '20 at 08:06
  • 2
    When the program runs to line 414, it will crash and no longer execute. – yanzhang.guo May 19 '20 at 08:12
  • Ah, I see. Can you please add this information explicetly to your question? I would suggest as the first sentence before the code listening. – MacOS May 19 '20 at 08:20
  • Sorry, I don't understand the meaning of 'before the code listening', what should I do? – yanzhang.guo May 19 '20 at 08:25
  • Before your code in the question. That way, everyone knows exactly what is going on. :) – MacOS May 19 '20 at 08:30
  • Maybe as Hitokiri said, this is a bug of library function. – yanzhang.guo May 19 '20 at 09:17
  • Of course. I assumed that you have tried different version of the library? As we are talking about it, can you please not only add where it breaks but also the library version you are using (and the library versions you have tried)? Thank you. – MacOS May 19 '20 at 09:41
  • The latest version I downloaded from GitHub, I asked the author, but I haven't replied yet. – yanzhang.guo May 19 '20 at 09:52