error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
Maintainer thường phản hồi trong vòng 12 ngày
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 62/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Ít trao đổi
- Công nghệ
- c, cmake
- Lĩnh vực
- build-system
Hướng nghiên cứu
Tái hiện bản build với cảnh báo nghiêm ngặt bằng CMake FetchContent và kiểm tra cJSON.c quanh dòng 607 trong print_number, vị trí mà báo cáo xác định là nơi xảy ra lỗi. So sánh các chẩn đoán của trình biên dịch với các kiểm tra toán học xung quanh và chạy bản build cùng các bài kiểm thử của cJSON; được coi là hoàn tất khi thư viện build thành công với các cờ cảnh báo được báo cáo mà không vô hiệu hóa lỗi.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
I encountered a compilation error when integrating cJSON into my C++ project via CMake's FetchContent. The build fails at line 607 in cjson.c with the following error:
error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
Environment
- cJSON Version:
v1.7.19 - Compiler:
gcc version 15.1.0 (x86_64-win32-seh-rev0, Built by MinGW-Builds project) - Build System:
cmake version 4.0.2 (CMake; JetBrains IDE bundle; build 1) - OS: Windows 11
Steps to Reproduce
- Configure CMake to include cJSON via FetchContent:
include(FetchContent) FetchContent_Declare( cjson # actualy I downloaded form release and use `SOURSE_DIR` to include it GIT_REPOSITORY https://github.com/DaveGamble/cJSON.git GIT_TAG v1.7.19 ) FetchContent_MakeAvailable(cjson) target_link_libraries(my_target cjson) - Enable strict warnings (e.g.,
-Werror=float-conversion). - Build the project.
Error Log
"D:\software\CLion 2024.3.5\bin\cmake\win\x64\bin\cmake.exe" --build D:\projects\prj-terminal\cmake-build-debug --target prj_terminal -j 22
[1/3] Building C object _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj
FAILED: _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj
D:\software\mingw64\bin\cc.exe -DCJSON_API_VISIBILITY -DCJSON_EXPORT_SYMBOLS -DENABLE_LOCALES -Dcjson_EXPORTS -std=c89 -pedantic -Wall -Wextra -Werror -Wstrict-prototypes -Wwrite-strings -Wshadow -Winit-self -Wcast-align -Wformat=2 -Wmissing-prototypes -Wstrict-overflow=2 -Wcast-qual -Wundef -Wswitch-default -Wconversion -Wc++-compat -fstack-protector-strong -Wdouble-promotion -Wparentheses -Wformat-overflow -Wunused-macros -Wmissing-variable-declarations -Wswitch-enum -fvisibility=hidden -g -std=gnu11 -fdiagnostics-color=always -MD -MT _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj -MF _deps\cjson-build\CMakeFiles\cjson.dir\cJSON.c.obj.d -o _deps/cjson-build/CMakeFiles/cjson.dir/cJSON.c.obj -c D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c
In file included from D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c:42:
D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c: In function 'print_number':
D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c:607:15: error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
607 | if (isnan(d) || isinf(d))
| ^
D:/projects/prj-terminal/third_party/cJSON-1.7.19/cJSON.c:607:27: error: conversion from 'double' to 'float' may change value [-Werror=float-conversion]
607 | if (isnan(d) || isinf(d))
| ^
cc1.exe: all warnings being treated as errors
ninja: build stopped: subcommand failed.
Proposed Solution
The error occurs due to an implicit double-to-float conversion.
I asked ai, and it told me that to resolve this, consider:
- Explicitly casting the result to
float(if precision loss is acceptable). - Using
-Wno-float-conversionfor cJSON (not ideal for strict compliance). - Refactoring the calculation to avoid precision loss warnings.
Additional Context
- This issue arises when compiling with strict warning flags (e.g.,
-Werror). - The problematic code is in the
parse_hex4function (line 607 in latest master).
I wander if there is a way to solve this without remove the flag Werror=float-conversion
I'm still learning Cpp and Cmake. So if you would be willing to help me, I would greatly appreciate it. Thank you for reading my issue. Let me know if you need more details!
- 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
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DaveGamble/cJSON
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DaveGamble/cJSON#1106 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Undefined-Behavior in function cJSON_SetNumberHelperCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
DaveGamble/cJSON#1100 · 1 reaction ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 86/100
DaveGamble/cJSON#1094 · 9 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
DaveGamble/cJSON#1093 ·
Maintainer thường phản hồi trong vòng 12 ngày
-
Use-after-free in cJSONUtils_ApplyPatches when a patch removes the patch arrayCó thể đã có người làm @iliasabk đã nhận 32 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
DaveGamble/cJSON#1082 ·
Maintainer thường phản hồi trong vòng 12 ngày
Tất cả issue của DaveGamble/cJSON
Issue tương tự
-
v9.6.0: CMake incorrectly enforces presence of libevdevCó thể đã có người làm Có pull request liên kết đang mở hoặc đã được merge. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 67/100
Maintainer thường phản hồi trong vòng 1 ngày
-
[Bug] Unhandled ValueError terminates watchdog thread when GISRC value contains multiple colonsĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug Component component: net
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
RT-Thread/rt-thread#11913 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
contiki-ng/contiki-ng#3289 ·
Maintainer thường phản hồi trong vòng 3 ngày
-
area:docs help wanted type:enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
Maintainer thường phản hồi trong vòng 1 ngày