public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 8/9] lineread: Use ssize_t for line lengths
Date: Thu,  6 Jun 2024 20:09:48 +1000	[thread overview]
Message-ID: <20240606100949.1250958-9-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20240606100949.1250958-1-david@gibson.dropbear.id.au>

Functions and structures in lineread.c use plain int to record and report
the length of lines we receive.  This means we truncate the result from
read(2) in some circumstances.  Use ssize_t to avoid that.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 conf.c     |  2 +-
 lineread.c | 10 ++++------
 lineread.h |  7 ++++---
 3 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/conf.c b/conf.c
index fdeb9db7..3998bfaa 100644
--- a/conf.c
+++ b/conf.c
@@ -401,9 +401,9 @@ static void get_dns(struct ctx *c)
 	struct fqdn *s = c->dns_search;
 	struct lineread resolvconf;
 	unsigned int added = 0;
+	ssize_t line_len;
 	char *line, *end;
 	const char *p;
-	int line_len;
 
 	dns4_set = !c->ifi4 || !IN4_IS_ADDR_UNSPECIFIED(dns4);
 	dns6_set = !c->ifi6 || !IN6_IS_ADDR_UNSPECIFIED(dns6);
diff --git a/lineread.c b/lineread.c
index d631da44..0387f4a0 100644
--- a/lineread.c
+++ b/lineread.c
@@ -39,13 +39,11 @@ void lineread_init(struct lineread *lr, int fd)
  *
  * Return: length of line in bytes, -1 if no line was found
  */
-static int peek_line(struct lineread *lr, bool eof)
+static ssize_t peek_line(struct lineread *lr, bool eof)
 {
 	char *nl;
 
 	/* Sanity checks (which also document invariants) */
-	ASSERT(lr->count >= 0);
-	ASSERT(lr->next_line >= 0);
 	ASSERT(lr->next_line + lr->count >= lr->next_line);
 	ASSERT(lr->next_line + lr->count <= LINEREAD_BUFFER_SIZE);
 
@@ -74,13 +72,13 @@ static int peek_line(struct lineread *lr, bool eof)
  *
  * Return:	Length of line read on success, 0 on EOF, negative on error
  */
-int lineread_get(struct lineread *lr, char **line)
+ssize_t lineread_get(struct lineread *lr, char **line)
 {
 	bool eof = false;
-	int line_len;
+	ssize_t line_len;
 
 	while ((line_len = peek_line(lr, eof)) < 0) {
-		int rc;
+		ssize_t rc;
 
 		if ((lr->next_line + lr->count) == LINEREAD_BUFFER_SIZE) {
 			/* No space at end */
diff --git a/lineread.h b/lineread.h
index af864186..9203e280 100644
--- a/lineread.h
+++ b/lineread.h
@@ -18,14 +18,15 @@
  * @buf:	Buffer storing data read from file.
  */
 struct lineread {
-	int fd; int next_line;
-	int count;
+	int fd;
+	ssize_t next_line;
+	ssize_t count;
 
 	/* One extra byte for possible trailing \0 */
 	char buf[LINEREAD_BUFFER_SIZE+1];
 };
 
 void lineread_init(struct lineread *lr, int fd);
-int lineread_get(struct lineread *lr, char **line);
+ssize_t lineread_get(struct lineread *lr, char **line);
 
 #endif /* _LINEREAD_H */
-- 
@@ -18,14 +18,15 @@
  * @buf:	Buffer storing data read from file.
  */
 struct lineread {
-	int fd; int next_line;
-	int count;
+	int fd;
+	ssize_t next_line;
+	ssize_t count;
 
 	/* One extra byte for possible trailing \0 */
 	char buf[LINEREAD_BUFFER_SIZE+1];
 };
 
 void lineread_init(struct lineread *lr, int fd);
-int lineread_get(struct lineread *lr, char **line);
+ssize_t lineread_get(struct lineread *lr, char **line);
 
 #endif /* _LINEREAD_H */
-- 
2.45.2


  parent reply	other threads:[~2024-06-06 10:10 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-06 10:09 [PATCH 0/9] Some more static checker fixes David Gibson
2024-06-06 10:09 ` [PATCH 1/9] tcp: Make pointer const in tcp_revert_seq David Gibson
2024-06-06 10:09 ` [PATCH 2/9] udp: Make rport calculation more local David Gibson
2024-06-06 10:09 ` [PATCH 3/9] cppcheck: Suppress constParameterCallback errors David Gibson
2024-06-07 18:49   ` Stefano Brivio
2024-06-08  6:32     ` David Gibson
2024-06-08 11:48       ` Stefano Brivio
2024-06-06 10:09 ` [PATCH 4/9] Remove pointless macro parameters in CALL_PROTO_HANDLER David Gibson
2024-06-06 10:09 ` [PATCH 5/9] clang-tidy: Enable the bugprone-macro-parentheses check David Gibson
2024-06-06 10:09 ` [PATCH 6/9] util: Use unsigned indices for bits in bitmaps David Gibson
2024-06-06 10:09 ` [PATCH 7/9] conf: Safer parsing of MAC addresses David Gibson
2024-06-06 10:09 ` David Gibson [this message]
2024-06-06 10:09 ` [PATCH 9/9] util: Use 'long' to represent millisecond durations David Gibson

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=20240606100949.1250958-9-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).