From 44e7f4f0ebc852514a0463a1a1868529336e0fe1 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Sat, 29 Mar 2025 02:32:24 +0100 Subject: [PATCH] xmlwf: Address clang-tidy warning bugprone-narrowing-conversions The symptom was: > [..]/expat/xmlwf/xmlfile.c:204:13: error: narrowing conversion from 'ssize_t' (aka 'long') to signed type 'int' is implementation-defined [bugprone-narrowing-conversions,-warnings-as-errors] > 204 | nread = read(fd, buf, g_read_size_bytes); > | ^ > [..]/expat/xmlwf/xmlwf.c:314:14: error: narrowing conversion from 'unsigned long' to signed type 'int' is implementation-defined [bugprone-narrowing-conversions,-warnings-as-errors] > 314 | numBytes = count * sizeof(XML_Char); > | ^ The solution to read(3) was copied from file xmlwf/readfilemap.c for now. --- expat/xmlwf/xmlfile.c | 19 +++++++++++++------ expat/xmlwf/xmlwf.c | 2 +- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/expat/xmlwf/xmlfile.c b/expat/xmlwf/xmlfile.c index 9c4f7f8d..78a3a73b 100644 --- a/expat/xmlwf/xmlfile.c +++ b/expat/xmlwf/xmlfile.c @@ -56,12 +56,19 @@ #include "xmltchar.h" #include "filemap.h" +/* Function "read": */ #if defined(_MSC_VER) # include -#endif - -#ifdef HAVE_UNISTD_H +/* https://msdn.microsoft.com/en-us/library/wyssk1bs(v=vs.100).aspx */ +# define EXPAT_read _read +# define EXPAT_read_count_t int +# define EXPAT_read_req_t unsigned int +#else /* POSIX */ # include +/* https://pubs.opengroup.org/onlinepubs/009695399/functions/read.html */ +# define EXPAT_read read +# define EXPAT_read_count_t ssize_t +# define EXPAT_read_req_t size_t #endif #ifndef O_BINARY @@ -192,7 +199,7 @@ processStream(const XML_Char *filename, XML_Parser parser) { } } for (;;) { - int nread; + EXPAT_read_count_t nread; char *buf = (char *)XML_GetBuffer(parser, g_read_size_bytes); if (! buf) { if (filename != NULL) @@ -201,14 +208,14 @@ processStream(const XML_Char *filename, XML_Parser parser) { filename != NULL ? filename : T("xmlwf")); return 0; } - nread = read(fd, buf, g_read_size_bytes); + nread = EXPAT_read(fd, buf, (EXPAT_read_req_t)g_read_size_bytes); if (nread < 0) { tperror(filename != NULL ? filename : T("STDIN")); if (filename != NULL) close(fd); return 0; } - if (XML_ParseBuffer(parser, nread, nread == 0) == XML_STATUS_ERROR) { + if (XML_ParseBuffer(parser, (int)nread, nread == 0) == XML_STATUS_ERROR) { reportError(parser, filename != NULL ? filename : T("STDIN")); if (filename != NULL) close(fd); diff --git a/expat/xmlwf/xmlwf.c b/expat/xmlwf/xmlwf.c index 7c0a8cd4..ec7e51c9 100644 --- a/expat/xmlwf/xmlwf.c +++ b/expat/xmlwf/xmlwf.c @@ -305,7 +305,7 @@ static XML_Char * xcsdup(const XML_Char *s) { XML_Char *result; int count = 0; - int numBytes; + size_t numBytes; /* Get the length of the string, including terminator */ while (s[count++] != 0) {