Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

cJSON_Print trapped in an infinite loop when elements are added a certain way

Aperta
#922 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 12 giorni

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
c
Ambito
backend

Direzione di ricerca

Inizia compilando ed eseguendo lo snippet C minimo per riprodurre il blocco in cJSON_Print. Segui l’attraversamento dell’array messages e dei relativi nodi system e message riutilizzati, quindi verifica che la stampa termini con JSON valido e che la sequenza di pulizia rimanga sicura.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Here is a strange behavior of cJSON_Print. When the subtree it is transversing contains a node referenced by an ancestor node, it ends up in an infinite loop. The only way this can happen is if leaf nodes link back to their parents and cJSON_Print tries to transverse this path, resulting in a circular dep. Even this assumption doesn't hold up considering that when the order of placing objects message and system into the messages array is swapped (i.e., adding system before message), printing works perfectly.

Here is a minimal snippet to reproduce this behavior.

#include <stdio.h>
#include <stdlib.h>
#include "../cJSON/cJSON.h"
int main() {
    cJSON *bin = cJSON_CreateArray();
    cJSON *root = cJSON_CreateObject();
    cJSON *system = cJSON_CreateObject();
    cJSON *message = cJSON_CreateObject();
    cJSON *messages = cJSON_CreateArray();

    cJSON_AddItemToArray(bin, root);
    cJSON_AddItemToArray(bin, system);
    cJSON_AddItemToArray(bin, message);
    cJSON_AddItemToArray(messages, message);
    cJSON_AddItemToArray(messages, system);
    cJSON_AddItemToObject(root, "messages", messages);

    char *json_string = cJSON_Print(root);
    printf("%s\n", json_string);
    free(json_string);

cleanup:     
    cJSON_DetachItemFromArray(bin, 0);
    cJSON_DetachItemFromArray(bin, 0);
    cJSON_DetachItemFromArray(bin, 0);    
    cJSON_Delete(root);
    cJSON_Delete(bin);

    return 0;
}

Lingua principale
C
Stelle
13k
Fork
3.5k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di DaveGamble/cJSON

Tutte le issue di DaveGamble/cJSON

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.