@@ -421,18 +421,36 @@ static size_t SDLCALL fd_read(void *userdata, void *ptr, size_t size, SDL_IOStat
421
421
ssize_t bytes ;
422
422
do {
423
423
bytes = read (iodata -> fd , ptr , size );
424
- } while (bytes < 0 && errno == EINTR );
424
+ } while ((bytes < 0 ) && (errno == EINTR ));
425
+
426
+ ssize_t result = bytes ;
427
+ if ((bytes > 0 ) && (bytes < size )) { // was it a short read, EOF, or error?
428
+ // try to read the difference, so we can rule out a short read.
429
+ do {
430
+ result = read (iodata -> fd , ((Uint8 * ) ptr ) + bytes , size - bytes );
431
+ } while ((result < 0 ) && (errno == EINTR ));
432
+
433
+ if (result > 0 ) {
434
+ bytes += result ;
435
+ result = bytes ;
436
+ SDL_assert (bytes <= size );
437
+ }
438
+ }
425
439
426
- if (bytes < 0 ) {
440
+ if (result < 0 ) {
427
441
if (errno == EAGAIN ) {
428
442
* status = SDL_IO_STATUS_NOT_READY ;
429
443
} else {
430
444
* status = SDL_IO_STATUS_ERROR ;
431
445
SDL_SetError ("Error reading from datastream: %s" , strerror (errno ));
432
446
}
433
- bytes = 0 ;
434
- } else if (bytes == 0 ) {
447
+ if (bytes < 0 ) {
448
+ bytes = 0 ;
449
+ }
450
+ } else if (result == 0 ) {
435
451
* status = SDL_IO_STATUS_EOF ;
452
+ } else if (result < size ) {
453
+ * status = SDL_IO_STATUS_NOT_READY ;;
436
454
}
437
455
return (size_t )bytes ;
438
456
}
@@ -443,17 +461,36 @@ static size_t SDLCALL fd_write(void *userdata, const void *ptr, size_t size, SDL
443
461
ssize_t bytes ;
444
462
do {
445
463
bytes = write (iodata -> fd , ptr , size );
446
- } while (bytes < 0 && errno == EINTR );
464
+ } while ((bytes < 0 ) && (errno == EINTR ));
465
+
466
+ ssize_t result = bytes ;
467
+ if ((bytes > 0 ) && (bytes < size )) { // was it a short write, or error?
468
+ // try to write the difference, so we can rule out a short read.
469
+ do {
470
+ result = write (iodata -> fd , ((Uint8 * ) ptr ) + bytes , size - bytes );
471
+ } while ((result < 0 ) && (errno == EINTR ));
472
+
473
+ if (result > 0 ) {
474
+ bytes += result ;
475
+ result = bytes ;
476
+ SDL_assert (bytes <= size );
477
+ }
478
+ }
447
479
448
- if (bytes < 0 ) {
480
+ if (result < 0 ) {
449
481
if (errno == EAGAIN ) {
450
482
* status = SDL_IO_STATUS_NOT_READY ;
451
483
} else {
452
484
* status = SDL_IO_STATUS_ERROR ;
453
485
SDL_SetError ("Error writing to datastream: %s" , strerror (errno ));
454
486
}
455
- bytes = 0 ;
487
+ if (bytes < 0 ) {
488
+ bytes = 0 ;
489
+ }
490
+ } else if (result < size ) {
491
+ * status = SDL_IO_STATUS_NOT_READY ;;
456
492
}
493
+
457
494
return (size_t )bytes ;
458
495
}
459
496
0 commit comments