coverity Scan the Code Results

Open Beginner friendly
#1,067 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
68/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c
Domain
testing

Research direction

Locate the print(...) function shown in the issue and inspect its failure path around Coverity CID 5543771. Confirm why printed is checked after the allocation and cleanup branches, then run the repository's available tests or static analysis to verify the dead-code finding is resolved without changing normal cleanup behavior.

Written by the indexing model from the issue text.

Description


34static unsigned char *print(const cJSON * const item, cJSON_bool format, const internal_hooks * const hooks)
1235{
1236    static const size_t default_buffer_size = 256;
1237    printbuffer buffer[1];
      	assignment: 赋值:printed = NULL。
1238    unsigned char *printed = NULL;
1239
1240    memset(buffer, 0, sizeof(buffer));
1241
1242    /* create buffer */
1243    buffer->buffer = (unsigned char*) hooks->allocate(default_buffer_size);
1244    buffer->length = default_buffer_size;
1245    buffer->format = format;
1246    buffer->hooks = *hooks;
1247    if (buffer->buffer == NULL)
1248    {
1249        goto fail;
1250    }
1251
1252    /* print the value */
1253    if (!print_value(item, buffer))
1254    {
1255        goto fail;
1256    }
1257    update_offset(buffer);
1258
1259    /* check if reallocate is available */
1260    if (hooks->reallocate != NULL)
1261    {
1262        printed = (unsigned char*) hooks->reallocate(buffer->buffer, buffer->offset + 1);
1263        if (printed == NULL) {
1264            goto fail;
1265        }
1266        buffer->buffer = NULL;
1267    }
1268    else /* otherwise copy the JSON over to a new buffer */
1269    {
1270        printed = (unsigned char*) hooks->allocate(buffer->offset + 1);
1271        if (printed == NULL)
1272        {
1273            goto fail;
1274        }
1275        memcpy(printed, buffer->buffer, cjson_min(buffer->length, buffer->offset + 1));
1276        printed[buffer->offset] = '\0'; /* just to be sure */
1277
1278        /* free the buffer */
1279        hooks->deallocate(buffer->buffer);
1280        buffer->buffer = NULL;
1281    }
1282
1283    return printed;
1284
1285fail:
1286    if (buffer->buffer != NULL)
1287    {
1288        hooks->deallocate(buffer->buffer);
1289        buffer->buffer = NULL;
1290    }
1291
      	null: 在条件 printed != NULL 下,printed 的值必须为 NULL。
      	dead_error_condition: 条件 printed != NULL 不可能为 true。
1292    if (printed != NULL)
1293    {
      	
CID 5543771: (#1 of 1): 逻辑死代码 (DEADCODE)
dead_error_begin: 执行无法到达此语句:hooks->deallocate(printed);。
1294        hooks->deallocate(printed);
1295        printed = NULL;
1296    }
1297
1298    return NULL;
1299}

Dominant language
C
Stars
13k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from DaveGamble/cJSON

All issues in DaveGamble/cJSON

Similar issues

More C issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.