From ffe828aabe0be033581c5650007e5e7c646ced34 Mon Sep 17 00:00:00 2001 From: Klaas Dannen Date: Fri, 13 Mar 2026 16:07:01 +0100 Subject: [PATCH] Replace assert() with proper exceptions for release-mode safety assert() statements are stripped in Dart release builds, meaning all NDEF flag validation (MB, ME, CF) and TNF/type-length checks silently disappear in production. Replaced with FormatException for malformed NDEF data and RangeError for invalid TNF values, so errors are caught regardless of build mode. --- lib/ndef.dart | 16 ++++++++++++---- lib/record.dart | 20 +++++++++++++------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lib/ndef.dart b/lib/ndef.dart index e39e1be..6c57670 100644 --- a/lib/ndef.dart +++ b/lib/ndef.dart @@ -39,14 +39,22 @@ List decodeRawNdefMessage( while (!stream.isEnd()) { var record = NDEFRecord.decodeStream(stream, typeFactory); if (records.isEmpty) { - assert(record.flags.MB == true, "MB flag is not set in first record"); + if (record.flags.MB != true) { + throw FormatException("MB flag is not set in first record"); + } } else { - assert(record.flags.MB == false, "MB flag is set in middle record"); + if (record.flags.MB != false) { + throw FormatException("MB flag is set in middle record"); + } } records.add(record); } - assert(records.last.flags.ME == true, "ME flag is not set in last record"); - assert(records.last.flags.CF == false, "CF flag is set in last record"); + if (records.last.flags.ME != true) { + throw FormatException("ME flag is not set in last record"); + } + if (records.last.flags.CF != false) { + throw FormatException("CF flag is set in last record"); + } return records; } diff --git a/lib/record.dart b/lib/record.dart index 7fa87a7..8f4bd77 100644 --- a/lib/record.dart +++ b/lib/record.dart @@ -37,7 +37,9 @@ class NDEFRecordFlags { /// Encodes the flags into a single byte. int encode() { - assert(0 <= TNF && TNF <= 7); + if (TNF < 0 || TNF > 7) { + throw RangeError.range(TNF, 0, 7, "TNF"); + } return (MB.toInt() << 7) | (ME.toInt() << 6) | (CF.toInt() << 5) | @@ -336,15 +338,19 @@ class NDEFRecord { idLength = stream.readByte(); } - if ([0, 5, 6].contains(flags.TNF)) { - assert(typeLength == 0, "TYPE_LENGTH must be 0 when TNF is 0,5,6"); + if ([0, 5, 6].contains(flags.TNF) && typeLength != 0) { + throw FormatException("TYPE_LENGTH must be 0 when TNF is 0,5,6"); } if (flags.TNF == 0) { - assert(idLength == 0, "ID_LENGTH must be 0 when TNF is 0"); - assert(payloadLength == 0, "PAYLOAD_LENGTH must be 0 when TNF is 0"); + if (idLength != 0) { + throw FormatException("ID_LENGTH must be 0 when TNF is 0"); + } + if (payloadLength != 0) { + throw FormatException("PAYLOAD_LENGTH must be 0 when TNF is 0"); + } } - if ([1, 2, 3, 4].contains(flags.TNF)) { - assert(typeLength > 0, "TYPE_LENGTH must be > 0 when TNF is 1,2,3,4"); + if ([1, 2, 3, 4].contains(flags.TNF) && typeLength <= 0) { + throw FormatException("TYPE_LENGTH must be > 0 when TNF is 1,2,3,4"); } var type = stream.readBytes(typeLength);