git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] use labs() for variables of type long instead of abs()

From
René Scharfe <l.s.r@web.de>
Date
Nov 15, 2014, 13:27 UTC
Message-ID
<546754B9.2080305@web.de>

Using abs() on long values can cause truncation, so use labs() instead. Reported by Clang 3.5 (-Wabsolute-value, enabled by -Wall).

Signed-off-by: Rene Scharfe <l.s.r@web.de>
---
 builtin/receive-pack.c | 2 +-
 config.c               | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 32fc540..e908d07 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -431,7 +431,7 @@ static const char *check_nonce(const char *buf, size_t len)
 	nonce_stamp_slop = (long)ostamp - (long)stamp;
 
 	if (nonce_stamp_slop_limit &&
-	    abs(nonce_stamp_slop) <= nonce_stamp_slop_limit) {
+	    labs(nonce_stamp_slop) <= nonce_stamp_slop_limit) {
 		/*
 		 * Pretend as if the received nonce (which passes the
 		 * HMAC check, so it is not a forged by third-party)
diff --git a/config.c b/config.c
index 15a2983..ae1398f 100644
--- a/config.c
+++ b/config.c
@@ -506,9 +506,9 @@ static int git_parse_signed(const char *value, intmax_t *ret, intmax_t max)
 			errno = EINVAL;
 			return 0;
 		}
-		uval = abs(val);
+		uval = labs(val);
 		uval *= factor;
-		if (uval > max || abs(val) > uval) {
+		if (uval > max || labs(val) > uval) {
 			errno = ERANGE;
 			return 0;
 		}
-- 
2.1.3
Message 1 of 1 in “use labs() for variables of type long instead of abs()”
  1. use labs() for variables of type long instead of abs()René Scharfe, Nov 15, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.