Skip to content

Commit 2e1f7f5

Browse files
committed
fix: guard float helpers behind float32 option
1 parent 8933f14 commit 2e1f7f5

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ Desktop.ini
4444
# Local env
4545
*.log
4646
git.txt
47+
/prompts

src/mcbor.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ typedef char mcbor_float32_must_be_ieee754_binary32[
194194

195195
/* ── Float bit-cast (avoids strict aliasing) ───────────────────────────── */
196196

197+
#if MCBOR_ENABLE_FLOAT32
197198
static inline uint32_t float_to_bits(float f)
198199
{
199200
uint32_t bits;
@@ -207,6 +208,7 @@ static inline float bits_to_float(uint32_t bits)
207208
memcpy(&f, &bits, sizeof(f));
208209
return f;
209210
}
211+
#endif
210212

211213
/* ═══════════════════════════════════════════════════════════════════════════
212214
* Encoder

tests/test_all.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,14 @@ TEST(test_enc_null) {
259259
TEST(test_enc_float) {
260260
mcbor_enc_t enc;
261261
mcbor_enc_init(&enc, buf, sizeof(buf));
262-
mcbor_enc_float(&enc, 3.14f);
262+
#if MCBOR_ENABLE_FLOAT32
263+
ASSERT_EQ(MCBOR_OK, mcbor_enc_float(&enc, 3.14f));
263264
ASSERT_EQ(5, (int)query_enc_size(&enc));
264265
ASSERT_EQ(0xFA, buf[0]); /* major 7, 4-byte float */
266+
#else
267+
ASSERT_EQ(MCBOR_ERR_UNSUPPORTED, mcbor_enc_float(&enc, 3.14f));
268+
ASSERT_EQ(0, (int)query_enc_size(&enc));
269+
#endif
265270
}
266271

267272
/* ═══════════════════════════════════════════════════════════════════════════
@@ -397,13 +402,18 @@ TEST(test_roundtrip_bool) {
397402
TEST(test_roundtrip_float) {
398403
mcbor_enc_t enc;
399404
mcbor_enc_init(&enc, buf, sizeof(buf));
400-
mcbor_enc_float(&enc, -273.15f);
405+
#if MCBOR_ENABLE_FLOAT32
406+
ASSERT_EQ(MCBOR_OK, mcbor_enc_float(&enc, -273.15f));
401407

402408
mcbor_dec_t dec;
403409
mcbor_dec_init(&dec, buf, query_enc_size(&enc));
404410
float val;
405411
ASSERT_EQ(MCBOR_OK, mcbor_dec_float(&dec, &val));
406412
ASSERT_FLOAT_EQ(-273.15f, val);
413+
#else
414+
ASSERT_EQ(MCBOR_ERR_UNSUPPORTED, mcbor_enc_float(&enc, -273.15f));
415+
ASSERT_EQ(0, (int)query_enc_size(&enc));
416+
#endif
407417
}
408418

409419
TEST(test_roundtrip_str) {
@@ -471,6 +481,7 @@ TEST(test_roundtrip_array) {
471481
TEST(test_roundtrip_map) {
472482
mcbor_enc_t enc;
473483
mcbor_enc_init(&enc, buf, sizeof(buf));
484+
#if MCBOR_ENABLE_FLOAT32
474485
mcbor_enc_map(&enc, 2);
475486
mcbor_enc_str(&enc, "temp");
476487
mcbor_enc_float(&enc, 22.5f);
@@ -498,13 +509,17 @@ TEST(test_roundtrip_map) {
498509
ASSERT_STR_EQ("hum", key);
499510
mcbor_dec_uint(&dec, &uval);
500511
ASSERT_EQ(65, (int)uval);
512+
#else
513+
ASSERT_EQ(MCBOR_ERR_UNSUPPORTED, mcbor_enc_float(&enc, 22.5f));
514+
#endif
501515
}
502516

503517
/* ═══════════════════════════════════════════════════════════════════════════
504518
* Tests: Telemetry payload (real-world pattern)
505519
* ═══════════════════════════════════════════════════════════════════════════ */
506520

507521
TEST(test_telemetry_payload) {
522+
#if MCBOR_ENABLE_FLOAT32
508523
/* Encode: {"device":"sensor-01","temp":23.4,"hum":55,"ok":true} */
509524
mcbor_enc_t enc;
510525
mcbor_enc_init(&enc, buf, sizeof(buf));
@@ -552,6 +567,11 @@ TEST(test_telemetry_payload) {
552567
ASSERT_TRUE(bval);
553568

554569
ASSERT_TRUE(query_dec_done(&dec));
570+
#else
571+
mcbor_enc_t enc;
572+
mcbor_enc_init(&enc, buf, sizeof(buf));
573+
ASSERT_EQ(MCBOR_ERR_UNSUPPORTED, mcbor_enc_float(&enc, 23.4f));
574+
#endif
555575
}
556576

557577
/* ═══════════════════════════════════════════════════════════════════════════

0 commit comments

Comments
 (0)