public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
From: Anton Moryakov <ant.v.moryakov@gmail.com>
To: linux-mtd@lists.infradead.org
Cc: David Oberhollenzer <david.oberhollenzer@sigma-star.at>,
	Anton Moryakov <ant.v.moryakov@gmail.com>
Subject: [PATCH] iniparser: prevent signed integer underflow in line length calculation
Date: Tue, 27 Jan 2026 15:56:57 +0300	[thread overview]
Message-ID: <20260127125657.50784-1-ant.v.moryakov@gmail.com> (raw)

In iniparser_load_file(), the code computed:
    len = (int)strlen(line) - 1;
If the input line was empty (e.g. started with a null byte), strlen()
returned 0, leading to len = -1. This caused:
  - A signed integer underflow (detected by static analyzers)
  - Potential out-of-bounds access when checking line[len]

Fix by:
  - Using size_t for strlen() result
  - Checking for zero length before subtraction
  - Computing len as (int)(line_len - 1) only when safe

Signed-off-by: Anton Moryakov <ant.v.moryakov@gmail.com>
---
 lib/libiniparser.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/lib/libiniparser.c b/lib/libiniparser.c
index 4b21b34..1d6dbac 100644
--- a/lib/libiniparser.c
+++ b/lib/libiniparser.c
@@ -697,7 +697,13 @@ dictionary * iniparser_load_file(FILE * in, const char * ininame)
 
     while (fgets(line+last, ASCIILINESZ-last, in)!=NULL) {
         lineno++ ;
-        len = (int)strlen(line)-1;
+        size_t line_len = strlen(line);
+                if (line_len == 0) {
+            memset(line, 0, ASCIILINESZ);
+            last = 0;
+            continue;
+        }
+        len = (int)(line_len - 1);
         if (len<=0)
             continue;
         /* Safety check against buffer overflows */
-- 
2.39.2


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

                 reply	other threads:[~2026-01-27 12:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260127125657.50784-1-ant.v.moryakov@gmail.com \
    --to=ant.v.moryakov@gmail.com \
    --cc=david.oberhollenzer@sigma-star.at \
    --cc=linux-mtd@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox