strerr.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "../curl_setup.h"
  25. #ifdef HAVE_STRERROR_R
  26. # if (!defined(HAVE_POSIX_STRERROR_R) && \
  27. !defined(HAVE_GLIBC_STRERROR_R)) || \
  28. (defined(HAVE_POSIX_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R))
  29. # error "strerror_r MUST be either POSIX, glibc style"
  30. # endif
  31. #endif
  32. #include <curl/curl.h>
  33. #ifndef WITHOUT_LIBCURL
  34. #include <curl/mprintf.h>
  35. #define SNPRINTF curl_msnprintf
  36. #else
  37. /* when built for the test servers */
  38. /* adjust for old MSVC */
  39. #if defined(_MSC_VER) && (_MSC_VER < 1900)
  40. #define SNPRINTF _snprintf
  41. #else
  42. #define SNPRINTF snprintf
  43. #endif
  44. #endif /* !WITHOUT_LIBCURL */
  45. #include "winapi.h"
  46. #include "strerr.h"
  47. /* The last 2 #include files should be in this order */
  48. #include "../curl_memory.h"
  49. #include "../memdebug.h"
  50. #ifdef USE_WINSOCK
  51. /* This is a helper function for curlx_strerror that converts Winsock error
  52. * codes (WSAGetLastError) to error messages.
  53. * Returns NULL if no error message was found for error code.
  54. */
  55. static const char *
  56. get_winsock_error(int err, char *buf, size_t len)
  57. {
  58. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  59. const char *p;
  60. size_t alen;
  61. #endif
  62. if(!len)
  63. return NULL;
  64. *buf = '\0';
  65. #ifdef CURL_DISABLE_VERBOSE_STRINGS
  66. (void)err;
  67. return NULL;
  68. #else
  69. switch(err) {
  70. case WSAEINTR:
  71. p = "Call interrupted";
  72. break;
  73. case WSAEBADF:
  74. p = "Bad file";
  75. break;
  76. case WSAEACCES:
  77. p = "Bad access";
  78. break;
  79. case WSAEFAULT:
  80. p = "Bad argument";
  81. break;
  82. case WSAEINVAL:
  83. p = "Invalid arguments";
  84. break;
  85. case WSAEMFILE:
  86. p = "Out of file descriptors";
  87. break;
  88. case WSAEWOULDBLOCK:
  89. p = "Call would block";
  90. break;
  91. case WSAEINPROGRESS:
  92. case WSAEALREADY:
  93. p = "Blocking call in progress";
  94. break;
  95. case WSAENOTSOCK:
  96. p = "Descriptor is not a socket";
  97. break;
  98. case WSAEDESTADDRREQ:
  99. p = "Need destination address";
  100. break;
  101. case WSAEMSGSIZE:
  102. p = "Bad message size";
  103. break;
  104. case WSAEPROTOTYPE:
  105. p = "Bad protocol";
  106. break;
  107. case WSAENOPROTOOPT:
  108. p = "Protocol option is unsupported";
  109. break;
  110. case WSAEPROTONOSUPPORT:
  111. p = "Protocol is unsupported";
  112. break;
  113. case WSAESOCKTNOSUPPORT:
  114. p = "Socket is unsupported";
  115. break;
  116. case WSAEOPNOTSUPP:
  117. p = "Operation not supported";
  118. break;
  119. case WSAEAFNOSUPPORT:
  120. p = "Address family not supported";
  121. break;
  122. case WSAEPFNOSUPPORT:
  123. p = "Protocol family not supported";
  124. break;
  125. case WSAEADDRINUSE:
  126. p = "Address already in use";
  127. break;
  128. case WSAEADDRNOTAVAIL:
  129. p = "Address not available";
  130. break;
  131. case WSAENETDOWN:
  132. p = "Network down";
  133. break;
  134. case WSAENETUNREACH:
  135. p = "Network unreachable";
  136. break;
  137. case WSAENETRESET:
  138. p = "Network has been reset";
  139. break;
  140. case WSAECONNABORTED:
  141. p = "Connection was aborted";
  142. break;
  143. case WSAECONNRESET:
  144. p = "Connection was reset";
  145. break;
  146. case WSAENOBUFS:
  147. p = "No buffer space";
  148. break;
  149. case WSAEISCONN:
  150. p = "Socket is already connected";
  151. break;
  152. case WSAENOTCONN:
  153. p = "Socket is not connected";
  154. break;
  155. case WSAESHUTDOWN:
  156. p = "Socket has been shut down";
  157. break;
  158. case WSAETOOMANYREFS:
  159. p = "Too many references";
  160. break;
  161. case WSAETIMEDOUT:
  162. p = "Timed out";
  163. break;
  164. case WSAECONNREFUSED:
  165. p = "Connection refused";
  166. break;
  167. case WSAELOOP:
  168. p = "Loop??";
  169. break;
  170. case WSAENAMETOOLONG:
  171. p = "Name too long";
  172. break;
  173. case WSAEHOSTDOWN:
  174. p = "Host down";
  175. break;
  176. case WSAEHOSTUNREACH:
  177. p = "Host unreachable";
  178. break;
  179. case WSAENOTEMPTY:
  180. p = "Not empty";
  181. break;
  182. case WSAEPROCLIM:
  183. p = "Process limit reached";
  184. break;
  185. case WSAEUSERS:
  186. p = "Too many users";
  187. break;
  188. case WSAEDQUOT:
  189. p = "Bad quota";
  190. break;
  191. case WSAESTALE:
  192. p = "Something is stale";
  193. break;
  194. case WSAEREMOTE:
  195. p = "Remote error";
  196. break;
  197. case WSAEDISCON:
  198. p = "Disconnected";
  199. break;
  200. /* Extended Winsock errors */
  201. case WSASYSNOTREADY:
  202. p = "Winsock library is not ready";
  203. break;
  204. case WSANOTINITIALISED:
  205. p = "Winsock library not initialised";
  206. break;
  207. case WSAVERNOTSUPPORTED:
  208. p = "Winsock version not supported";
  209. break;
  210. /* getXbyY() errors (already handled in herrmsg):
  211. * Authoritative Answer: Host not found */
  212. case WSAHOST_NOT_FOUND:
  213. p = "Host not found";
  214. break;
  215. /* Non-Authoritative: Host not found, or SERVERFAIL */
  216. case WSATRY_AGAIN:
  217. p = "Host not found, try again";
  218. break;
  219. /* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
  220. case WSANO_RECOVERY:
  221. p = "Unrecoverable error in call to nameserver";
  222. break;
  223. /* Valid name, no data record of requested type */
  224. case WSANO_DATA:
  225. p = "No data record of requested type";
  226. break;
  227. default:
  228. return NULL;
  229. }
  230. alen = strlen(p);
  231. if(alen < len)
  232. strcpy(buf, p);
  233. return buf;
  234. #endif
  235. }
  236. #endif /* USE_WINSOCK */
  237. /*
  238. * Our thread-safe and smart strerror() replacement.
  239. *
  240. * The 'err' argument passed in to this function MUST be a true errno number
  241. * as reported on this system. We do no range checking on the number before
  242. * we pass it to the "number-to-message" conversion function and there might
  243. * be systems that do not do proper range checking in there themselves.
  244. *
  245. * We do not do range checking (on systems other than Windows) since there is
  246. * no good reliable and portable way to do it.
  247. *
  248. * On Windows different types of error codes overlap. This function has an
  249. * order of preference when trying to match error codes:
  250. * CRT (errno), Winsock (WSAGetLastError), Windows API (GetLastError).
  251. *
  252. * It may be more correct to call one of the variant functions instead:
  253. * Call Curl_sspi_strerror if the error code is definitely Windows SSPI.
  254. * Call curlx_winapi_strerror if the error code is definitely Windows API.
  255. */
  256. const char *curlx_strerror(int err, char *buf, size_t buflen)
  257. {
  258. #ifdef _WIN32
  259. DWORD old_win_err = GetLastError();
  260. #endif
  261. int old_errno = errno;
  262. char *p;
  263. if(!buflen)
  264. return NULL;
  265. #ifndef _WIN32
  266. DEBUGASSERT(err >= 0);
  267. #endif
  268. *buf = '\0';
  269. #ifdef _WIN32
  270. #ifndef UNDER_CE
  271. /* 'sys_nerr' is the maximum errno number, it is not widely portable */
  272. if(err >= 0 && err < sys_nerr)
  273. SNPRINTF(buf, buflen, "%s", sys_errlist[err]);
  274. else
  275. #endif
  276. {
  277. if(
  278. #ifdef USE_WINSOCK
  279. !get_winsock_error(err, buf, buflen) &&
  280. #endif
  281. !curlx_get_winapi_error((DWORD)err, buf, buflen))
  282. SNPRINTF(buf, buflen, "Unknown error %d (%#x)", err, err);
  283. }
  284. #else /* !_WIN32 */
  285. #if defined(HAVE_STRERROR_R) && defined(HAVE_POSIX_STRERROR_R)
  286. /*
  287. * The POSIX-style strerror_r() may set errno to ERANGE if insufficient
  288. * storage is supplied via 'strerrbuf' and 'buflen' to hold the generated
  289. * message string, or EINVAL if 'errnum' is not a valid error number.
  290. */
  291. if(strerror_r(err, buf, buflen) &&
  292. buflen > sizeof("Unknown error ") + 20) {
  293. if(buf[0] == '\0')
  294. SNPRINTF(buf, buflen, "Unknown error %d", err);
  295. }
  296. #elif defined(HAVE_STRERROR_R) && defined(HAVE_GLIBC_STRERROR_R)
  297. /*
  298. * The glibc-style strerror_r() only *might* use the buffer we pass to
  299. * the function, but it always returns the error message as a pointer,
  300. * so we must copy that string unconditionally (if non-NULL).
  301. */
  302. {
  303. char buffer[256];
  304. char *msg = strerror_r(err, buffer, sizeof(buffer));
  305. if(msg && buflen > 1)
  306. SNPRINTF(buf, buflen, "%s", msg);
  307. else if(buflen > sizeof("Unknown error ") + 20)
  308. SNPRINTF(buf, buflen, "Unknown error %d", err);
  309. }
  310. #else
  311. {
  312. /* !checksrc! disable BANNEDFUNC 1 */
  313. const char *msg = strerror(err);
  314. if(msg && buflen > 1)
  315. SNPRINTF(buf, buflen, "%s", msg);
  316. else if(buflen > sizeof("Unknown error ") + 20)
  317. SNPRINTF(buf, buflen, "Unknown error %d", err);
  318. }
  319. #endif
  320. #endif /* _WIN32 */
  321. /* strip trailing '\r\n' or '\n'. */
  322. p = strrchr(buf, '\n');
  323. if(p && (p - buf) >= 2)
  324. *p = '\0';
  325. p = strrchr(buf, '\r');
  326. if(p && (p - buf) >= 1)
  327. *p = '\0';
  328. if(errno != old_errno)
  329. CURL_SETERRNO(old_errno);
  330. #ifdef _WIN32
  331. if(old_win_err != GetLastError())
  332. SetLastError(old_win_err);
  333. #endif
  334. return buf;
  335. }