Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

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

Đang mở
#922 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 12 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
c
Lĩnh vực
backend

Hướng nghiên cứu

Bắt đầu bằng cách biên dịch và chạy đoạn mã C tối giản để tái hiện hiện tượng treo trong cJSON_Print. Theo dõi quá trình duyệt qua mảng messages và các node system và message được tái sử dụng của nó, sau đó xác minh rằng việc in kết thúc với JSON hợp lệ và trình tự dọn dẹp vẫn an toàn.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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;
}

Ngôn ngữ chính
C
Star
13k
Fork
3.5k
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của DaveGamble/cJSON

Tất cả issue của DaveGamble/cJSON

Issue tương tự

Thêm issue về C

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.