From 87710c25a670b03c9dce1197b98542b2a3dbad85 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Tue, 2 Feb 2021 19:12:10 +1300 Subject: [PATCH] Fix potential segfault upon overflowing slice bounds. --- src/Services/Prosody/slice.vala | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Services/Prosody/slice.vala b/src/Services/Prosody/slice.vala index f6180b4..0462813 100644 --- a/src/Services/Prosody/slice.vala +++ b/src/Services/Prosody/slice.vala @@ -25,11 +25,15 @@ public class Slice : Gee.Hashable, Object { public Slice.b(Bytes? b) {_ = b == null ? empty : b;} public int length {get {return _.length;}} - public new uint8 get(int i) {return _[i < 0 ? length + i : i];} + public new uint8 get(int i) { + if (i > length) return 0; + return _[i < 0 ? length + i : i]; + } public Slice slice(int start_, int end_) { // Add some Python-style convenience var start = start_; if (start < 0) start += _.length; var end = end_; if (end < 0) end += _.length; + if (end > _.length) end = _.length; if (start >= end) return new Slice(); // Gracefully handle minor errors. // NOTE: Using the slice method of Bytes (which is provided by the