From 4050df8178def9df2107bab3d49ae97282f79e53 Mon Sep 17 00:00:00 2001 From: Edoardo La Greca Date: Wed, 30 Jul 2025 18:57:19 +0200 Subject: enforce msize observance --- src/main/kotlin/NinePMessage.kt | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/main/kotlin/NinePMessage.kt') diff --git a/src/main/kotlin/NinePMessage.kt b/src/main/kotlin/NinePMessage.kt index 1d0530a..87d59a5 100644 --- a/src/main/kotlin/NinePMessage.kt +++ b/src/main/kotlin/NinePMessage.kt @@ -13,7 +13,7 @@ import kotlin.math.pow * @param fieldValuesInt A map of each field name into its value. This map only stores integer values. * @param fieldValuesStr A map of each field name into its value. This map only stores string values. */ -class NinePMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: List, val fieldValuesInt: Map, val fieldValuesStr: Map) { +class NinePMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: List, val fieldValuesInt: Map, val fieldValuesStr: Map, val maxSize: UInt) { /** * Intersection between [fieldNames] and [fieldValuesInt]. In other words: the amount of integer fields that are * going to be used when writing the message. @@ -29,13 +29,17 @@ class NinePMessage(val type: NinePMessageType, val tag: UShort, val fieldNames: * Send the message using the given networking API. * * @param npt The networking API. - * @throws IllegalArgumentException if [fieldNames], [fieldValuesInt], and [fieldValuesStr] are incoherent. + * @throws IllegalArgumentException if [fieldNames], [fieldValuesInt], and [fieldValuesStr] are incoherent or the + * final size of the message exceeds the negotiated value. */ fun write(npt: NetworkPacketTransporter) { // check that names in fieldNames exist as keys in either fieldValuesInt or fieldValuesStr but not both require(insecInts.size == fieldNames.size - insecStrs.size) val totalSize = size() + if (totalSize > this.maxSize.toLong()) { + throw IllegalArgumentException("Message size exceeded.") + } writeMessageSizeType(npt, totalSize, type) writeInteger(npt, SizedMessageField(2, tag.toInt().toBigInteger())) for (field in fieldNames) { -- cgit v1.2.3