Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit26e9d4d

Browse files
committed
Convert elog.c's useful_strerror() into a globally-used strerror wrapper.
elog.c has long had a private strerror wrapper that handles assortedpossible failures or deficiencies of the platform's strerror. On Windows,it also knows how to translate Winsock error codes, which the nativestrerror does not. Move all this code into src/port/strerror.c anddefine strerror() as a macro that invokes it, so that both our frontendand backend code will have all of this behavior.I believe this constitutes an actual bug fix on Windows, since AFAICSour frontend code did not report Winsock error codes properly before this.However, the main point is to lay the groundwork for implementing %min src/port/snprintf.c: the behavior we want %m to have is this one,not the native strerror's.Note that this throws away the prior use of src/port/strerror.c,which was to implement strerror() on platforms lacking it. That'sbeen dead code for nigh twenty years now, since strerror() wasalready required by C89.We should likewise cause strerror_r to use this behavior, butI'll tackle that separately.Patch by me, reviewed by Michael PaquierDiscussion:https://postgr.es/m/2975.1526862605@sss.pgh.pa.us
1 parenta49ceda commit26e9d4d

File tree

19 files changed

+299
-311
lines changed

19 files changed

+299
-311
lines changed

‎configure

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15644,19 +15644,6 @@ esac
1564415644

1564515645
fi
1564615646

15647-
ac_fn_c_check_func "$LINENO" "strerror" "ac_cv_func_strerror"
15648-
if test "x$ac_cv_func_strerror" = xyes; then :
15649-
$as_echo "#define HAVE_STRERROR 1" >>confdefs.h
15650-
15651-
else
15652-
case " $LIBOBJS " in
15653-
*" strerror.$ac_objext "* ) ;;
15654-
*) LIBOBJS="$LIBOBJS strerror.$ac_objext"
15655-
;;
15656-
esac
15657-
15658-
fi
15659-
1566015647
ac_fn_c_check_func "$LINENO" "strlcat" "ac_cv_func_strlcat"
1566115648
if test "x$ac_cv_func_strlcat" = xyes; then :
1566215649
$as_echo "#define HAVE_STRLCAT 1" >>confdefs.h

‎configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1687,7 +1687,7 @@ else
16871687
AC_CHECK_FUNCS([fpclass fp_class fp_class_d class], [break])
16881688
fi
16891689

1690-
AC_REPLACE_FUNCS([crypt dlopen fls getopt getrusage inet_aton mkdtemp random rint srandomstrerrorstrlcat strlcpy strnlen])
1690+
AC_REPLACE_FUNCS([crypt dlopen fls getopt getrusage inet_aton mkdtemp random rint srandom strlcat strlcpy strnlen])
16911691

16921692
case $host_os in
16931693

‎src/backend/port/win32/socket.c

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -690,39 +690,3 @@ pgwin32_select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, c
690690
memcpy(writefds,&outwritefds,sizeof(fd_set));
691691
returnnummatches;
692692
}
693-
694-
695-
/*
696-
* Return win32 error string, since strerror can't
697-
* handle winsock codes
698-
*/
699-
staticcharwserrbuf[256];
700-
constchar*
701-
pgwin32_socket_strerror(interr)
702-
{
703-
staticHANDLEhandleDLL=INVALID_HANDLE_VALUE;
704-
705-
if (handleDLL==INVALID_HANDLE_VALUE)
706-
{
707-
handleDLL=LoadLibraryEx("netmsg.dll",NULL,DONT_RESOLVE_DLL_REFERENCES |LOAD_LIBRARY_AS_DATAFILE);
708-
if (handleDLL==NULL)
709-
ereport(FATAL,
710-
(errmsg_internal("could not load netmsg.dll: error code %lu",GetLastError())));
711-
}
712-
713-
ZeroMemory(&wserrbuf,sizeof(wserrbuf));
714-
if (FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS |
715-
FORMAT_MESSAGE_FROM_SYSTEM |
716-
FORMAT_MESSAGE_FROM_HMODULE,
717-
handleDLL,
718-
err,
719-
MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT),
720-
wserrbuf,
721-
sizeof(wserrbuf)-1,
722-
NULL)==0)
723-
{
724-
/* Failed to get id */
725-
sprintf(wserrbuf,"unrecognized winsock error %d",err);
726-
}
727-
returnwserrbuf;
728-
}

‎src/backend/utils/error/elog.c

Lines changed: 1 addition & 216 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,6 @@ static void send_message_to_server_log(ErrorData *edata);
178178
staticvoidwrite_pipe_chunks(char*data,intlen,intdest);
179179
staticvoidsend_message_to_frontend(ErrorData*edata);
180180
staticchar*expand_fmt_string(constchar*fmt,ErrorData*edata);
181-
staticconstchar*useful_strerror(interrnum);
182-
staticconstchar*get_errno_symbol(interrnum);
183181
staticconstchar*error_severity(intelevel);
184182
staticvoidappend_with_tabs(StringInfobuf,constchar*str);
185183
staticboolis_log_level_output(intelevel,intlog_min_level);
@@ -3360,7 +3358,7 @@ expand_fmt_string(const char *fmt, ErrorData *edata)
33603358
*/
33613359
constchar*cp2;
33623360

3363-
cp2=useful_strerror(edata->saved_errno);
3361+
cp2=strerror(edata->saved_errno);
33643362
for (;*cp2;cp2++)
33653363
{
33663364
if (*cp2=='%')
@@ -3383,219 +3381,6 @@ expand_fmt_string(const char *fmt, ErrorData *edata)
33833381
}
33843382

33853383

3386-
/*
3387-
* A slightly cleaned-up version of strerror()
3388-
*/
3389-
staticconstchar*
3390-
useful_strerror(interrnum)
3391-
{
3392-
/* this buffer is only used if strerror() and get_errno_symbol() fail */
3393-
staticcharerrorstr_buf[48];
3394-
constchar*str;
3395-
3396-
#ifdefWIN32
3397-
/* Winsock error code range, per WinError.h */
3398-
if (errnum >=10000&&errnum <=11999)
3399-
returnpgwin32_socket_strerror(errnum);
3400-
#endif
3401-
str=strerror(errnum);
3402-
3403-
/*
3404-
* Some strerror()s return an empty string for out-of-range errno. This
3405-
* is ANSI C spec compliant, but not exactly useful. Also, we may get
3406-
* back strings of question marks if libc cannot transcode the message to
3407-
* the codeset specified by LC_CTYPE. If we get nothing useful, first try
3408-
* get_errno_symbol(), and if that fails, print the numeric errno.
3409-
*/
3410-
if (str==NULL||*str=='\0'||*str=='?')
3411-
str=get_errno_symbol(errnum);
3412-
3413-
if (str==NULL)
3414-
{
3415-
snprintf(errorstr_buf,sizeof(errorstr_buf),
3416-
/*------
3417-
translator: This string will be truncated at 47
3418-
characters expanded. */
3419-
_("operating system error %d"),errnum);
3420-
str=errorstr_buf;
3421-
}
3422-
3423-
returnstr;
3424-
}
3425-
3426-
/*
3427-
* Returns a symbol (e.g. "ENOENT") for an errno code.
3428-
* Returns NULL if the code is unrecognized.
3429-
*/
3430-
staticconstchar*
3431-
get_errno_symbol(interrnum)
3432-
{
3433-
switch (errnum)
3434-
{
3435-
caseE2BIG:
3436-
return"E2BIG";
3437-
caseEACCES:
3438-
return"EACCES";
3439-
#ifdefEADDRINUSE
3440-
caseEADDRINUSE:
3441-
return"EADDRINUSE";
3442-
#endif
3443-
#ifdefEADDRNOTAVAIL
3444-
caseEADDRNOTAVAIL:
3445-
return"EADDRNOTAVAIL";
3446-
#endif
3447-
caseEAFNOSUPPORT:
3448-
return"EAFNOSUPPORT";
3449-
#ifdefEAGAIN
3450-
caseEAGAIN:
3451-
return"EAGAIN";
3452-
#endif
3453-
#ifdefEALREADY
3454-
caseEALREADY:
3455-
return"EALREADY";
3456-
#endif
3457-
caseEBADF:
3458-
return"EBADF";
3459-
#ifdefEBADMSG
3460-
caseEBADMSG:
3461-
return"EBADMSG";
3462-
#endif
3463-
caseEBUSY:
3464-
return"EBUSY";
3465-
caseECHILD:
3466-
return"ECHILD";
3467-
#ifdefECONNABORTED
3468-
caseECONNABORTED:
3469-
return"ECONNABORTED";
3470-
#endif
3471-
caseECONNREFUSED:
3472-
return"ECONNREFUSED";
3473-
#ifdefECONNRESET
3474-
caseECONNRESET:
3475-
return"ECONNRESET";
3476-
#endif
3477-
caseEDEADLK:
3478-
return"EDEADLK";
3479-
caseEDOM:
3480-
return"EDOM";
3481-
caseEEXIST:
3482-
return"EEXIST";
3483-
caseEFAULT:
3484-
return"EFAULT";
3485-
caseEFBIG:
3486-
return"EFBIG";
3487-
#ifdefEHOSTUNREACH
3488-
caseEHOSTUNREACH:
3489-
return"EHOSTUNREACH";
3490-
#endif
3491-
caseEIDRM:
3492-
return"EIDRM";
3493-
caseEINPROGRESS:
3494-
return"EINPROGRESS";
3495-
caseEINTR:
3496-
return"EINTR";
3497-
caseEINVAL:
3498-
return"EINVAL";
3499-
caseEIO:
3500-
return"EIO";
3501-
#ifdefEISCONN
3502-
caseEISCONN:
3503-
return"EISCONN";
3504-
#endif
3505-
caseEISDIR:
3506-
return"EISDIR";
3507-
#ifdefELOOP
3508-
caseELOOP:
3509-
return"ELOOP";
3510-
#endif
3511-
caseEMFILE:
3512-
return"EMFILE";
3513-
caseEMLINK:
3514-
return"EMLINK";
3515-
caseEMSGSIZE:
3516-
return"EMSGSIZE";
3517-
caseENAMETOOLONG:
3518-
return"ENAMETOOLONG";
3519-
caseENFILE:
3520-
return"ENFILE";
3521-
caseENOBUFS:
3522-
return"ENOBUFS";
3523-
caseENODEV:
3524-
return"ENODEV";
3525-
caseENOENT:
3526-
return"ENOENT";
3527-
caseENOEXEC:
3528-
return"ENOEXEC";
3529-
caseENOMEM:
3530-
return"ENOMEM";
3531-
caseENOSPC:
3532-
return"ENOSPC";
3533-
caseENOSYS:
3534-
return"ENOSYS";
3535-
#ifdefENOTCONN
3536-
caseENOTCONN:
3537-
return"ENOTCONN";
3538-
#endif
3539-
caseENOTDIR:
3540-
return"ENOTDIR";
3541-
#if defined(ENOTEMPTY)&& (ENOTEMPTY!=EEXIST)/* same code on AIX */
3542-
caseENOTEMPTY:
3543-
return"ENOTEMPTY";
3544-
#endif
3545-
#ifdefENOTSOCK
3546-
caseENOTSOCK:
3547-
return"ENOTSOCK";
3548-
#endif
3549-
#ifdefENOTSUP
3550-
caseENOTSUP:
3551-
return"ENOTSUP";
3552-
#endif
3553-
caseENOTTY:
3554-
return"ENOTTY";
3555-
caseENXIO:
3556-
return"ENXIO";
3557-
#if defined(EOPNOTSUPP)&& (!defined(ENOTSUP)|| (EOPNOTSUPP!=ENOTSUP))
3558-
caseEOPNOTSUPP:
3559-
return"EOPNOTSUPP";
3560-
#endif
3561-
#ifdefEOVERFLOW
3562-
caseEOVERFLOW:
3563-
return"EOVERFLOW";
3564-
#endif
3565-
caseEPERM:
3566-
return"EPERM";
3567-
caseEPIPE:
3568-
return"EPIPE";
3569-
caseEPROTONOSUPPORT:
3570-
return"EPROTONOSUPPORT";
3571-
caseERANGE:
3572-
return"ERANGE";
3573-
#ifdefEROFS
3574-
caseEROFS:
3575-
return"EROFS";
3576-
#endif
3577-
caseESRCH:
3578-
return"ESRCH";
3579-
#ifdefETIMEDOUT
3580-
caseETIMEDOUT:
3581-
return"ETIMEDOUT";
3582-
#endif
3583-
#ifdefETXTBSY
3584-
caseETXTBSY:
3585-
return"ETXTBSY";
3586-
#endif
3587-
#if defined(EWOULDBLOCK)&& (!defined(EAGAIN)|| (EWOULDBLOCK!=EAGAIN))
3588-
caseEWOULDBLOCK:
3589-
return"EWOULDBLOCK";
3590-
#endif
3591-
caseEXDEV:
3592-
return"EXDEV";
3593-
}
3594-
3595-
returnNULL;
3596-
}
3597-
3598-
35993384
/*
36003385
* error_severity --- get string representing elevel
36013386
*

‎src/include/pg_config.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,6 @@
534534
/* Define to 1 if you have the <stdlib.h> header file. */
535535
#undef HAVE_STDLIB_H
536536

537-
/* Define to 1 if you have the `strerror' function. */
538-
#undef HAVE_STRERROR
539-
540537
/* Define to 1 if you have the `strerror_r' function. */
541538
#undef HAVE_STRERROR_R
542539

‎src/include/pg_config.h.win32

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,6 @@
405405
/* Define to 1 if you have the <stdlib.h> header file. */
406406
#define HAVE_STDLIB_H 1
407407

408-
/* Define to 1 if you have the `strerror' function. */
409-
#ifndef HAVE_STRERROR
410-
#define HAVE_STRERROR 1
411-
#endif
412-
413408
/* Define to 1 if you have the `strerror_r' function. */
414409
/* #undef HAVE_STRERROR_R */
415410

‎src/include/port.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ extern intpg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
189189
#endif
190190
#endif/* USE_REPL_SNPRINTF */
191191

192+
/* Replace strerror() with our own, somewhat more robust wrapper */
193+
externchar*pg_strerror(interrnum);
194+
#definestrerror pg_strerror
195+
192196
/* Portable prompt handling */
193197
externvoidsimple_prompt(constchar*prompt,char*destination,size_tdestlen,
194198
boolecho);
@@ -355,7 +359,7 @@ extern intisinf(double x);
355359
#undef isinf
356360
#defineisinf __builtin_isinf
357361
#endif/* __has_builtin(isinf) */
358-
#endif/* __clang__ && !__cplusplus*/
362+
#endif/* __clang__ && !__cplusplus*/
359363
#endif/* !HAVE_ISINF */
360364

361365
#ifndefHAVE_MKDTEMP
@@ -403,7 +407,7 @@ extern void srandom(unsigned int seed);
403407
#ifndefHAVE_DLOPEN
404408
externvoid*dlopen(constchar*file,intmode);
405409
externvoid*dlsym(void*handle,constchar*symbol);
406-
externintdlclose(void*handle);
410+
externintdlclose(void*handle);
407411
externchar*dlerror(void);
408412
#endif
409413

‎src/include/port/win32_port.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,8 +322,8 @@ extern intpgwin32_safestat(const char *path, struct stat *buf);
322322
* Supplement to <errno.h>.
323323
*
324324
* We redefine network-related Berkeley error symbols as the corresponding WSA
325-
* constants.This allowselog.c to recognize them as being in the Winsock
326-
* error code range and pass them off topgwin32_socket_strerror(), since
325+
* constants. This allowsstrerror.c to recognize them as being in the Winsock
326+
* error code range and pass them off towin32_socket_strerror(), since
327327
* Windows' version of plain strerror() won't cope. Note that this will break
328328
* if these names are used for anything else besides Windows Sockets errors.
329329
* See TranslateSocketError() when changing this list.
@@ -456,8 +456,6 @@ intpgwin32_connect(SOCKET s, const struct sockaddr *name, int namelen);
456456
intpgwin32_select(intnfds,fd_set*readfs,fd_set*writefds,fd_set*exceptfds,conststructtimeval*timeout);
457457
intpgwin32_recv(SOCKETs,char*buf,intlen,intflags);
458458
intpgwin32_send(SOCKETs,constvoid*buf,intlen,intflags);
459-
460-
constchar*pgwin32_socket_strerror(interr);
461459
intpgwin32_waitforsinglesocket(SOCKETs,intwhat,inttimeout);
462460

463461
externintpgwin32_noblock;

‎src/interfaces/ecpg/compatlib/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
/blibecpg_compatdll.def
33
/exports.list
44
/snprintf.c
5+
/strerror.c
56
/strnlen.c

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp