From 78e67f2c4d1389c40a184f28ad53b004e0ec10dc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Thu, 22 Feb 2024 09:46:07 -0500
Subject: [PATCH] Avoid pasing NULL to strto*() functions.

Whill this is perfectly legal and is not a problem, Coverity complains.
---
 src/xpdev/xpprintf.c | 49 ++++++++++++++++++++++++++++++++++----------
 1 file changed, 38 insertions(+), 11 deletions(-)

diff --git a/src/xpdev/xpprintf.c b/src/xpdev/xpprintf.c
index f75bab1bd8..22fb7e43a4 100644
--- a/src/xpdev/xpprintf.c
+++ b/src/xpdev/xpprintf.c
@@ -739,7 +739,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 							break;
 #endif
 						case XP_PRINTF_TYPE_CHARP:
-							i=strtol(cp, NULL, 0);
+							if (cp)
+								i=strtol(cp, NULL, 0);
+							else
+								i = 0;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							i=(int)d;
@@ -776,7 +779,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 							break;
 #endif
 						case XP_PRINTF_TYPE_CHARP:
-							ui=strtoul(cp, NULL, 0);
+							if (cp)
+								ui=strtoul(cp, NULL, 0);
+							else
+								ui = 0;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							ui=(unsigned)d;
@@ -813,7 +819,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 							break;
 #endif
 						case XP_PRINTF_TYPE_CHARP:
-							l=strtol(cp, NULL, 0);
+							if (cp)
+								l=strtol(cp, NULL, 0);
+							else
+								l = 0;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							l=(long)d;
@@ -850,7 +859,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 							break;
 #endif
 						case XP_PRINTF_TYPE_CHARP:
-							ul=strtoul(cp, NULL, 0);
+							if (cp)
+								ul=strtoul(cp, NULL, 0);
+							else
+								ul = 0;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							ul=(unsigned long)d;
@@ -886,7 +898,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 							ll=ull;
 							break;
 						case XP_PRINTF_TYPE_CHARP:
-							ll=strtoll(cp, NULL, 0);
+							if (cp)
+								ll=strtoll(cp, NULL, 0);
+							else
+								ll = 0;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							ll=d;
@@ -921,7 +936,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 							ull=ll;
 							break;
 						case XP_PRINTF_TYPE_CHARP:
-							ull=strtoull(cp, NULL, 0);
+							if (cp)
+								ull=strtoull(cp, NULL, 0);
+							else
+								ull = 0;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							ull=d;
@@ -1013,7 +1031,10 @@ char* xp_asprintf_next(char *format, int type, ...)
 							break;
 #endif
 						case XP_PRINTF_TYPE_CHARP:
-							d=strtod(cp, NULL);
+							if (cp)
+								d=strtod(cp, NULL);
+							else
+								d = 0.0;
 							break;
 						case XP_PRINTF_TYPE_LONGDOUBLE:
 							d=ld;
@@ -1050,11 +1071,14 @@ char* xp_asprintf_next(char *format, int type, ...)
 							break;
 #endif
 						case XP_PRINTF_TYPE_CHARP:
+							if (cp)
 #if defined(__BORLANDC__)
-							ld=strtod(cp, NULL);
+								ld=strtod(cp, NULL);
 #else
-							ld=strtold(cp, NULL);
+								ld=strtold(cp, NULL);
 #endif
+							else
+								ld = 0.0L;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							ld=d;
@@ -1129,11 +1153,14 @@ char* xp_asprintf_next(char *format, int type, ...)
 							break;
 #endif
 						case XP_PRINTF_TYPE_CHARP:
+							if (cp)
 #if defined(__BORLANDC__)
-							s=strtoul(cp, NULL, 0);
+								s=strtoul(cp, NULL, 0);
 #else
-							s=strtoull(cp, NULL, 0);
+								s=strtoull(cp, NULL, 0);
 #endif
+							else
+								s = 0;
 							break;
 						case XP_PRINTF_TYPE_DOUBLE:
 							s=(size_t)d;
-- 
GitLab