Commit c5e87fdb authored by Jay Satiro's avatar Jay Satiro
Browse files

strerror: Preserve Windows error code in some functions

This is a follow-up to af021625 which removed (SET_)ERRNO macros. That
commit was an earlier draft that I committed by mistake, which was then
remedied by a5834e52 and e909de65, and now this commit. With this commit
there is now no difference between the current code and the changes that
were approved in the final draft.

Thanks-to: Max Dymond, Marcel Raad, Daniel Stenberg, Gisle Vanem
Ref: https://github.com/curl/curl/pull/1589
parent e909de65
Loading
Loading
Loading
Loading
+42 −25
Original line number Original line Diff line number Diff line
@@ -49,6 +49,10 @@
#include "curl_memory.h"
#include "curl_memory.h"
#include "memdebug.h"
#include "memdebug.h"


#if defined(WIN32) || defined(_WIN32_WCE)
#define PRESERVE_WINDOWS_ERROR_CODE
#endif

const char *
const char *
curl_easy_strerror(CURLcode error)
curl_easy_strerror(CURLcode error)
{
{
@@ -432,6 +436,10 @@ curl_share_strerror(CURLSHcode error)
static const char *
static const char *
get_winsock_error (int err, char *buf, size_t len)
get_winsock_error (int err, char *buf, size_t len)
{
{
#ifdef PRESERVE_WINDOWS_ERROR_CODE
  DWORD old_win_err = GetLastError();
#endif
  int old_errno = errno;
  const char *p;
  const char *p;


#ifndef CURL_DISABLE_VERBOSE_STRINGS
#ifndef CURL_DISABLE_VERBOSE_STRINGS
@@ -611,6 +619,15 @@ get_winsock_error (int err, char *buf, size_t len)
#endif
#endif
  strncpy(buf, p, len);
  strncpy(buf, p, len);
  buf [len-1] = '\0';
  buf [len-1] = '\0';

  if(errno != old_errno)
    errno = old_errno;

#ifdef PRESERVE_WINDOWS_ERROR_CODE
  if(old_win_err != GetLastError())
    SetLastError(old_win_err);
#endif

  return buf;
  return buf;
}
}
#endif   /* USE_WINSOCK */
#endif   /* USE_WINSOCK */
@@ -628,19 +645,16 @@ get_winsock_error (int err, char *buf, size_t len)
 */
 */
const char *Curl_strerror(struct connectdata *conn, int err)
const char *Curl_strerror(struct connectdata *conn, int err)
{
{
#ifdef PRESERVE_WINDOWS_ERROR_CODE
  DWORD old_win_err = GetLastError();
#endif
  int old_errno = errno;
  char *buf, *p;
  char *buf, *p;
  size_t max;
  size_t max;
  int old_errno;

#ifdef WIN32
  DWORD old_win_err;
#endif
  DEBUGASSERT(conn);
  DEBUGASSERT(conn);
  DEBUGASSERT(err >= 0);
  DEBUGASSERT(err >= 0);


  old_errno = errno;
#ifdef WIN32
  old_win_err = GetLastError();
#endif
  buf = conn->syserr_buf;
  buf = conn->syserr_buf;
  max = sizeof(conn->syserr_buf)-1;
  max = sizeof(conn->syserr_buf)-1;
  *buf = '\0';
  *buf = '\0';
@@ -727,12 +741,14 @@ const char *Curl_strerror(struct connectdata *conn, int err)
  p = strrchr(buf, '\r');
  p = strrchr(buf, '\r');
  if(p && (p - buf) >= 1)
  if(p && (p - buf) >= 1)
    *p = '\0';
    *p = '\0';
#ifdef WIN32

  if(errno != old_errno)
    errno = old_errno;

#ifdef PRESERVE_WINDOWS_ERROR_CODE
  if(old_win_err != GetLastError())
  if(old_win_err != GetLastError())
    SetLastError(old_win_err);
    SetLastError(old_win_err);
#endif
#endif
  if(errno != old_errno)
    errno = old_errno;


  return buf;
  return buf;
}
}
@@ -740,17 +756,19 @@ const char *Curl_strerror(struct connectdata *conn, int err)
#ifdef USE_WINDOWS_SSPI
#ifdef USE_WINDOWS_SSPI
const char *Curl_sspi_strerror (struct connectdata *conn, int err)
const char *Curl_sspi_strerror (struct connectdata *conn, int err)
{
{
#ifdef PRESERVE_WINDOWS_ERROR_CODE
  DWORD old_win_err = GetLastError();
#endif
  int old_errno = errno;
  const char *txt;
  char *outbuf;
  size_t outmax;
#ifndef CURL_DISABLE_VERBOSE_STRINGS
#ifndef CURL_DISABLE_VERBOSE_STRINGS
  char txtbuf[80];
  char txtbuf[80];
  char msgbuf[sizeof(conn->syserr_buf)];
  char msgbuf[sizeof(conn->syserr_buf)];
  char *p, *str, *msg = NULL;
  char *p, *str, *msg = NULL;
  bool msg_formatted = FALSE;
  bool msg_formatted = FALSE;
  int old_errno;
  DWORD old_win_err;
#endif
#endif
  const char *txt;
  char *outbuf;
  size_t outmax;


  DEBUGASSERT(conn);
  DEBUGASSERT(conn);


@@ -760,9 +778,6 @@ const char *Curl_sspi_strerror (struct connectdata *conn, int err)


#ifndef CURL_DISABLE_VERBOSE_STRINGS
#ifndef CURL_DISABLE_VERBOSE_STRINGS


  old_errno = errno;
  old_win_err = GetLastError();

  switch(err) {
  switch(err) {
    case SEC_E_OK:
    case SEC_E_OK:
      txt = "No error";
      txt = "No error";
@@ -1062,12 +1077,6 @@ const char *Curl_sspi_strerror (struct connectdata *conn, int err)
      strncpy(outbuf, str, outmax);
      strncpy(outbuf, str, outmax);
  }
  }


  if(old_win_err != GetLastError())
    SetLastError(old_win_err);

  if(errno != old_errno)
    errno = old_errno;

#else
#else


  if(err == SEC_E_OK)
  if(err == SEC_E_OK)
@@ -1081,6 +1090,14 @@ const char *Curl_sspi_strerror (struct connectdata *conn, int err)


  outbuf[outmax] = '\0';
  outbuf[outmax] = '\0';


  if(errno != old_errno)
    errno = old_errno;

#ifdef PRESERVE_WINDOWS_ERROR_CODE
  if(old_win_err != GetLastError())
    SetLastError(old_win_err);
#endif

  return outbuf;
  return outbuf;
}
}
#endif /* USE_WINDOWS_SSPI */
#endif /* USE_WINDOWS_SSPI */
+2 −5
Original line number Original line Diff line number Diff line
@@ -42,7 +42,6 @@ int test(char *URL)
  int hd;
  int hd;
  struct_stat file_info;
  struct_stat file_info;
  struct curl_slist *hl;
  struct curl_slist *hl;
  int error;


  struct curl_slist *headerlist=NULL;
  struct curl_slist *headerlist=NULL;
  const char *buf_1 = "RNFR 505";
  const char *buf_1 = "RNFR 505";
@@ -55,9 +54,8 @@ int test(char *URL)


  hd_src = fopen(libtest_arg2, "rb");
  hd_src = fopen(libtest_arg2, "rb");
  if(NULL == hd_src) {
  if(NULL == hd_src) {
    error = errno;
    fprintf(stderr, "fopen failed with error: %d %s\n",
    fprintf(stderr, "fopen failed with error: %d %s\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
    return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */
    return TEST_ERR_MAJOR_BAD; /* if this happens things are major weird */
  }
  }
@@ -66,9 +64,8 @@ int test(char *URL)
  hd = fstat(fileno(hd_src), &file_info);
  hd = fstat(fileno(hd_src), &file_info);
  if(hd == -1) {
  if(hd == -1) {
    /* can't open file, bail out */
    /* can't open file, bail out */
    error = errno;
    fprintf(stderr, "fstat() failed with error: %d %s\n",
    fprintf(stderr, "fstat() failed with error: %d %s\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
    fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
    fclose(hd_src);
    fclose(hd_src);
    return TEST_ERR_MAJOR_BAD;
    return TEST_ERR_MAJOR_BAD;
+2 −5
Original line number Original line Diff line number Diff line
@@ -35,7 +35,6 @@ int test(char *URL)
  CURL *curl = NULL;
  CURL *curl = NULL;
  FILE *hd_src = NULL;
  FILE *hd_src = NULL;
  int hd;
  int hd;
  int error;
  struct_stat file_info;
  struct_stat file_info;
  CURLM *m = NULL;
  CURLM *m = NULL;
  int running;
  int running;
@@ -55,9 +54,8 @@ int test(char *URL)


  hd_src = fopen(libtest_arg2, "rb");
  hd_src = fopen(libtest_arg2, "rb");
  if(NULL == hd_src) {
  if(NULL == hd_src) {
    error = errno;
    fprintf(stderr, "fopen failed with error: %d (%s)\n",
    fprintf(stderr, "fopen failed with error: %d (%s)\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
    fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
    return TEST_ERR_FOPEN;
    return TEST_ERR_FOPEN;
  }
  }
@@ -66,9 +64,8 @@ int test(char *URL)
  hd = fstat(fileno(hd_src), &file_info);
  hd = fstat(fileno(hd_src), &file_info);
  if(hd == -1) {
  if(hd == -1) {
    /* can't open file, bail out */
    /* can't open file, bail out */
    error = errno;
    fprintf(stderr, "fstat() failed with error: %d (%s)\n",
    fprintf(stderr, "fstat() failed with error: %d (%s)\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
    fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
    fclose(hd_src);
    fclose(hd_src);
    return TEST_ERR_FSTAT;
    return TEST_ERR_FSTAT;
+2 −5
Original line number Original line Diff line number Diff line
@@ -38,7 +38,6 @@ int test(char *URL)
  FILE *hd_src;
  FILE *hd_src;
  int hd;
  int hd;
  struct_stat file_info;
  struct_stat file_info;
  int error;


  if(!libtest_arg2) {
  if(!libtest_arg2) {
    fprintf(stderr, "Usage: <url> <file-to-upload>\n");
    fprintf(stderr, "Usage: <url> <file-to-upload>\n");
@@ -47,9 +46,8 @@ int test(char *URL)


  hd_src = fopen(libtest_arg2, "rb");
  hd_src = fopen(libtest_arg2, "rb");
  if(NULL == hd_src) {
  if(NULL == hd_src) {
    error = errno;
    fprintf(stderr, "fopen failed with error: %d %s\n",
    fprintf(stderr, "fopen failed with error: %d %s\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
    fprintf(stderr, "Error opening file: %s\n", libtest_arg2);
    return -2; /* if this happens things are major weird */
    return -2; /* if this happens things are major weird */
  }
  }
@@ -58,9 +56,8 @@ int test(char *URL)
  hd = fstat(fileno(hd_src), &file_info);
  hd = fstat(fileno(hd_src), &file_info);
  if(hd == -1) {
  if(hd == -1) {
    /* can't open file, bail out */
    /* can't open file, bail out */
    error = errno;
    fprintf(stderr, "fstat() failed with error: %d %s\n",
    fprintf(stderr, "fstat() failed with error: %d %s\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
    fprintf(stderr, "ERROR: cannot open file %s\n", libtest_arg2);
    fclose(hd_src);
    fclose(hd_src);
    return TEST_ERR_MAJOR_BAD;
    return TEST_ERR_MAJOR_BAD;
+2 −5
Original line number Original line Diff line number Diff line
@@ -228,7 +228,6 @@ int test(char *URL)
  CURL *curl = NULL;
  CURL *curl = NULL;
  FILE *hd_src = NULL;
  FILE *hd_src = NULL;
  int hd;
  int hd;
  int error;
  struct_stat file_info;
  struct_stat file_info;
  CURLM *m = NULL;
  CURLM *m = NULL;
  struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
  struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
@@ -244,9 +243,8 @@ int test(char *URL)


  hd_src = fopen(libtest_arg2, "rb");
  hd_src = fopen(libtest_arg2, "rb");
  if(NULL == hd_src) {
  if(NULL == hd_src) {
    error = errno;
    fprintf(stderr, "fopen() failed with error: %d (%s)\n",
    fprintf(stderr, "fopen() failed with error: %d (%s)\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
    fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
    return TEST_ERR_FOPEN;
    return TEST_ERR_FOPEN;
  }
  }
@@ -255,9 +253,8 @@ int test(char *URL)
  hd = fstat(fileno(hd_src), &file_info);
  hd = fstat(fileno(hd_src), &file_info);
  if(hd == -1) {
  if(hd == -1) {
    /* can't open file, bail out */
    /* can't open file, bail out */
    error = errno;
    fprintf(stderr, "fstat() failed with error: %d (%s)\n",
    fprintf(stderr, "fstat() failed with error: %d (%s)\n",
            error, strerror(error));
            errno, strerror(errno));
    fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
    fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
    fclose(hd_src);
    fclose(hd_src);
    return TEST_ERR_FSTAT;
    return TEST_ERR_FSTAT;
Loading