From 909f569318835d11703c49fba7dbe49996759f38 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Thu, 14 Nov 2024 10:19:31 -0600 Subject: xmpp-vala: StanzaNode.get_attribute_int: Return default value if not parsable as int --- xmpp-vala/src/core/stanza_node.vala | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'xmpp-vala/src/core/stanza_node.vala') diff --git a/xmpp-vala/src/core/stanza_node.vala b/xmpp-vala/src/core/stanza_node.vala index 44a70d8d..54c14956 100644 --- a/xmpp-vala/src/core/stanza_node.vala +++ b/xmpp-vala/src/core/stanza_node.vala @@ -98,15 +98,29 @@ public class StanzaNode : StanzaEntry { } public int get_attribute_int(string name, int def = -1, string? ns_uri = null) { - string? res = get_attribute(name, ns_uri); - if (res == null) return def; - return int.parse((!)res); + string? attribute_str = get_attribute(name, ns_uri); + if (attribute_str == null) return def; + + int res = def; + bool parse_success = int.try_parse(attribute_str, out res); + if (!parse_success) { + info("Could not parse int attribute %s: %s", name, attribute_str); + return def; + } + return res; } public uint get_attribute_uint(string name, uint def = 0, string? ns_uri = null) { - string? res = get_attribute(name, ns_uri); - if (res == null) return def; - return (uint) long.parse((!)res); + string? attribute_str = get_attribute(name, ns_uri); + if (attribute_str == null) return def; + + uint res = def; + bool parse_success = uint.try_parse(attribute_str, out res); + if (!parse_success) { + info("Could not parse uint attribute %s: %s", name, attribute_str); + return def; + } + return res; } public bool get_attribute_bool(string name, bool def = false, string? ns_uri = null) { -- cgit v1.2.3-70-g09d2