[PATCH] Win32 errno a little bit safer

Started by Magnus Naeslund(f)over 24 years ago3 messages
1 attachment(s)

Hello, i just reviewed the win32 errno patch and i saw that maybe i didn't
really played it totally safe in my last suggestion, the system table might
pick up the msg but not the netmsg.dll, so better try both.
I also added a hex printout of the "errno" appended to all messages, that's
nicer.

If anyone hate my coding style, or that i'm using goto constructs, just tell
me, and i'll rework it into a nested if () thing.

Patch attached.

Magnus

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Programmer/Networker [|] Magnus Naeslund
PGP Key: http://www.genline.nu/mag_pgp.txt
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Attachments:

win32_strerror_patch1.txttext/plain; name=win32_strerror_patch1.txtDownload
Index: fe-misc.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/interfaces/libpq/fe-misc.c,v
retrieving revision 1.54
diff -u -r1.54 fe-misc.c
--- fe-misc.c	2001/08/21 20:39:54	1.54
+++ fe-misc.c	2001/08/23 21:12:07
@@ -855,23 +855,46 @@
 #ifdef WIN32
 /*
  * strerror replacement for windows:
+ *
+ * We dont't know a fix for win9x yet, but this whould work for nt4 and win2k.
+ * If you can verify this working on win9x or have a solution, let us know, ok?
+ *
  */
 const char*
 winsock_strerror(DWORD eno)
 {
-	if (!FormatMessage( 
-		FORMAT_MESSAGE_IGNORE_INSERTS |
-        FORMAT_MESSAGE_FROM_SYSTEM | /* always consider system table */
-        ((netmsgModule != NULL) ? FORMAT_MESSAGE_FROM_HMODULE : 0),
-        netmsgModule, /* module to get message from (NULL == system) */
-		eno,
-        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-        winsock_strerror_buf,sizeof(winsock_strerror_buf)-1,
-        NULL
-       )){
-      sprintf(winsock_strerror_buf,"Unknown socket error(%u)",eno);
-    }
-    winsock_strerror_buf[sizeof(winsock_strerror_buf)-1]='\0';
-    return winsock_strerror_buf;
+  #define WSSE_MAXLEN (sizeof(winsock_strerror_buf)-1-12) /* 12 == "(0x00000000)" */
+  int length;
+
+  /* First try the "system table", this works on Win2k pro */
+
+  if (FormatMessage(
+	   FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_SYSTEM,
+	   0,eno,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+	   winsock_strerror_buf,WSSE_MAXLEN,NULL
+	 ))
+    goto WSSE_GOODEXIT;
+
+  /* That didn't work, let's try the netmsg.dll */
+
+  if (netmsgModule && 
+      FormatMessage(
+	   FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_FROM_HMODULE,
+	   0,eno,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+	   winsock_strerror_buf,WSSE_MAXLEN,NULL 
+	   ))
+    goto WSSE_GOODEXIT;
+
+  /* Everything failed, just tell the user that we don't know the desc */
+  
+  strcat(winsock_strerror_buf,"Socket error, no description available.");
+
+WSSE_GOODEXIT:
+
+  length = strlen(winsock_strerror_buf);
+  sprintf(winsock_strerror_buf + length<WSSE_MAXLEN?length:WSSE_MAXLEN,
+	  "(0x%08X)",eno);
+
+  return winsock_strerror_buf;
 }
 #endif
#2Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Magnus Naeslund(f) (#1)
Re: [PATCH] Win32 errno a little bit safer

Your patch has been added to the PostgreSQL unapplied patches list at:

http://candle.pha.pa.us/cgi-bin/pgpatches

I will try to apply it within the next 48 hours.

Hello, i just reviewed the win32 errno patch and i saw that maybe i didn't
really played it totally safe in my last suggestion, the system table might
pick up the msg but not the netmsg.dll, so better try both.
I also added a hex printout of the "errno" appended to all messages, that's
nicer.

If anyone hate my coding style, or that i'm using goto constructs, just tell
me, and i'll rework it into a nested if () thing.

Patch attached.

Magnus

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Programmer/Networker [|] Magnus Naeslund
PGP Key: http://www.genline.nu/mag_pgp.txt
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

[ Attachment, skipping... ]

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#3Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Magnus Naeslund(f) (#1)
Re: [PATCH] Win32 errno a little bit safer

Thanks. Patch applied.

Hello, i just reviewed the win32 errno patch and i saw that maybe i didn't
really played it totally safe in my last suggestion, the system table might
pick up the msg but not the netmsg.dll, so better try both.
I also added a hex printout of the "errno" appended to all messages, that's
nicer.

If anyone hate my coding style, or that i'm using goto constructs, just tell
me, and i'll rework it into a nested if () thing.

Patch attached.

Magnus

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Programmer/Networker [|] Magnus Naeslund
PGP Key: http://www.genline.nu/mag_pgp.txt
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

[ Attachment, skipping... ]

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/users-lounge/docs/faq.html

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026