@@ -302,6 +302,11 @@ pqsecure_close(PGconn *conn)
302
302
303
303
/*
304
304
* Read data from a secure connection.
305
+ *
306
+ * If SSL is in use, this function is responsible for putting a suitable
307
+ * message into conn->errorMessage upon error; but the caller does that
308
+ * when not using SSL. In either case, caller uses the returned errno
309
+ * to decide whether to continue/retry after error.
305
310
*/
306
311
ssize_t
307
312
pqsecure_read (PGconn * conn , void * ptr , size_t len )
@@ -325,6 +330,13 @@ pqsecure_read(PGconn *conn, void *ptr, size_t len)
325
330
switch (err )
326
331
{
327
332
case SSL_ERROR_NONE :
333
+ if (n < 0 )
334
+ {
335
+ printfPQExpBuffer (& conn -> errorMessage ,
336
+ libpq_gettext ("SSL_read failed but did not provide error information\n" ));
337
+ /* assume the connection is broken */
338
+ SOCK_ERRNO_SET (ECONNRESET );
339
+ }
328
340
break ;
329
341
case SSL_ERROR_WANT_READ :
330
342
n = 0 ;
@@ -342,7 +354,7 @@ pqsecure_read(PGconn *conn, void *ptr, size_t len)
342
354
{
343
355
char sebuf [256 ];
344
356
345
- if (n == -1 )
357
+ if (n < 0 )
346
358
{
347
359
REMEMBER_EPIPE (spinfo , SOCK_ERRNO == EPIPE );
348
360
printfPQExpBuffer (& conn -> errorMessage ,
@@ -353,29 +365,36 @@ pqsecure_read(PGconn *conn, void *ptr, size_t len)
353
365
{
354
366
printfPQExpBuffer (& conn -> errorMessage ,
355
367
libpq_gettext ("SSL SYSCALL error: EOF detected\n" ));
356
-
368
+ /* assume the connection is broken */
357
369
SOCK_ERRNO_SET (ECONNRESET );
358
370
n = -1 ;
359
371
}
360
372
break ;
361
373
}
362
374
case SSL_ERROR_SSL :
363
375
{
364
- char * err = SSLerrmessage ();
376
+ char * errm = SSLerrmessage ();
365
377
366
378
printfPQExpBuffer (& conn -> errorMessage ,
367
- libpq_gettext ("SSL error: %s\n" ), err );
368
- SSLerrfree (err );
379
+ libpq_gettext ("SSL error: %s\n" ), errm );
380
+ SSLerrfree (errm );
381
+ /* assume the connection is broken */
382
+ SOCK_ERRNO_SET (ECONNRESET );
383
+ n = -1 ;
384
+ break ;
369
385
}
370
- /* fall through */
371
386
case SSL_ERROR_ZERO_RETURN :
387
+ printfPQExpBuffer (& conn -> errorMessage ,
388
+ libpq_gettext ("SSL connection has been closed unexpectedly\n" ));
372
389
SOCK_ERRNO_SET (ECONNRESET );
373
390
n = -1 ;
374
391
break ;
375
392
default :
376
393
printfPQExpBuffer (& conn -> errorMessage ,
377
394
libpq_gettext ("unrecognized SSL error code: %d\n" ),
378
395
err );
396
+ /* assume the connection is broken */
397
+ SOCK_ERRNO_SET (ECONNRESET );
379
398
n = -1 ;
380
399
break ;
381
400
}
@@ -391,6 +410,11 @@ pqsecure_read(PGconn *conn, void *ptr, size_t len)
391
410
392
411
/*
393
412
* Write data to a secure connection.
413
+ *
414
+ * If SSL is in use, this function is responsible for putting a suitable
415
+ * message into conn->errorMessage upon error; but the caller does that
416
+ * when not using SSL. In either case, caller uses the returned errno
417
+ * to decide whether to continue/retry after error.
394
418
*/
395
419
ssize_t
396
420
pqsecure_write (PGconn * conn , const void * ptr , size_t len )
@@ -412,6 +436,13 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
412
436
switch (err )
413
437
{
414
438
case SSL_ERROR_NONE :
439
+ if (n < 0 )
440
+ {
441
+ printfPQExpBuffer (& conn -> errorMessage ,
442
+ libpq_gettext ("SSL_write failed but did not provide error information\n" ));
443
+ /* assume the connection is broken */
444
+ SOCK_ERRNO_SET (ECONNRESET );
445
+ }
415
446
break ;
416
447
case SSL_ERROR_WANT_READ :
417
448
@@ -429,7 +460,7 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
429
460
{
430
461
char sebuf [256 ];
431
462
432
- if (n == -1 )
463
+ if (n < 0 )
433
464
{
434
465
REMEMBER_EPIPE (spinfo , SOCK_ERRNO == EPIPE );
435
466
printfPQExpBuffer (& conn -> errorMessage ,
@@ -440,28 +471,36 @@ pqsecure_write(PGconn *conn, const void *ptr, size_t len)
440
471
{
441
472
printfPQExpBuffer (& conn -> errorMessage ,
442
473
libpq_gettext ("SSL SYSCALL error: EOF detected\n" ));
474
+ /* assume the connection is broken */
443
475
SOCK_ERRNO_SET (ECONNRESET );
444
476
n = -1 ;
445
477
}
446
478
break ;
447
479
}
448
480
case SSL_ERROR_SSL :
449
481
{
450
- char * err = SSLerrmessage ();
482
+ char * errm = SSLerrmessage ();
451
483
452
484
printfPQExpBuffer (& conn -> errorMessage ,
453
- libpq_gettext ("SSL error: %s\n" ), err );
454
- SSLerrfree (err );
485
+ libpq_gettext ("SSL error: %s\n" ), errm );
486
+ SSLerrfree (errm );
487
+ /* assume the connection is broken */
488
+ SOCK_ERRNO_SET (ECONNRESET );
489
+ n = -1 ;
490
+ break ;
455
491
}
456
- /* fall through */
457
492
case SSL_ERROR_ZERO_RETURN :
493
+ printfPQExpBuffer (& conn -> errorMessage ,
494
+ libpq_gettext ("SSL connection has been closed unexpectedly\n" ));
458
495
SOCK_ERRNO_SET (ECONNRESET );
459
496
n = -1 ;
460
497
break ;
461
498
default :
462
499
printfPQExpBuffer (& conn -> errorMessage ,
463
500
libpq_gettext ("unrecognized SSL error code: %d\n" ),
464
501
err );
502
+ /* assume the connection is broken */
503
+ SOCK_ERRNO_SET (ECONNRESET );
465
504
n = -1 ;
466
505
break ;
467
506
}
0 commit comments