From 242a10fbb12dbdc12d254bd8fc8669a0ac055304 Mon Sep 17 00:00:00 2001
From: Patrick Griffis <pgriffis@igalia.com>
Date: Wed, 5 Feb 2025 14:39:42 -0600
Subject: [PATCH] sniffer: Fix potential overflow

Conflict: Modify file path adaptation: libsoup/content-sniffer/soup-content-sniffer.c->libsoup/soup-content-sniffer.c
Reference: https://gitlab.gnome.org/GNOME/libsoup/-/commit/242a10fbb12dbdc12d254bd8fc8669a0ac055304

---
 libsoup/soup-content-sniffer.c                 |   2 +-
 tests/resources/whitespace.html                | Bin 0 -> 512 bytes
 tests/sniffing-test.c                          |   5 +++++
 tests/soup-tests.gresource.xml                 |   1 +
 4 files changed, 7 insertions(+), 1 deletions(-)
 create mode 100644 tests/resources/whitespace.html

diff --git a/libsoup/soup-content-sniffer.c b/libsoup/soup-content-sniffer.c
index aeee2e25c..da94e60c3 100644
--- a/libsoup/soup-content-sniffer.c
+++ b/libsoup/soup-content-sniffer.c
@@ -669,7 +669,7 @@ sniff_feed_or_html (SoupContentSniffer *sniffer, GBytes *buffer)
 		pos = 3;
 
  look_for_tag:
-	if (pos > resource_length)
+	if (pos >= resource_length)
 		goto text_html;
 
 	if (skip_insignificant_space (resource, &pos, resource_length))
diff --git a/tests/resources/whitespace.html b/tests/resources/whitespace.html
new file mode 100644
index 0000000000000000000000000000000000000000..7f07a0e639a102284d6f7c0c5d5560170f994553
GIT binary patch
literal 512
TcmcCf)YWAe1tT*Abam|ja4Z2(

literal 0
HcmV?d00001

diff --git a/tests/sniffing-test.c b/tests/sniffing-test.c
index 6116719ae..b5428177f 100644
--- a/tests/sniffing-test.c
+++ b/tests/sniffing-test.c
@@ -512,6 +512,11 @@ main (int argc, char **argv)
 			      "type/text_html; charset=UTF-8/test.html => text/html; charset=UTF-8",
 			      do_sniffing_test);
 
+        /* Test hitting skip_insignificant_space() with number of bytes equaling resource_length. */
+	g_test_add_data_func ("/sniffing/whitespace",
+			      "type/text_html/whitespace.html => text/html",
+			      do_sniffing_test);
+
 	/* Test that disabling the sniffer works correctly */
 	g_test_add_data_func ("/sniffing/disabled",
 			      "/text_or_binary/home.gif",
diff --git a/tests/soup-tests.gresource.xml b/tests/soup-tests.gresource.xml
index 9c08d170e..cbef1d402 100644
--- a/tests/soup-tests.gresource.xml
+++ b/tests/soup-tests.gresource.xml
@@ -25,5 +25,6 @@
     <file>resources/text.txt</file>
     <file>resources/text_binary.txt</file>
     <file>resources/tux.webp</file>
+    <file>resources/whitespace.html</file>
   </gresource>
 </gresources>
-- 
GitLab

