Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065 https://stackoverflow.com/a/8474123/14065

Try this:

static ssize_t read_all(int fdes, void* buffer, ssize_t size)
{
 ssize_t totalRead = 0;
 while(totalRead != size)
 {
 ssize_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065

Try this:

static ssize_t read_all(int fdes, void* buffer, ssize_t size)
{
 ssize_t totalRead = 0;
 while(totalRead != size)
 {
 ssize_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: https://stackoverflow.com/a/8474123/14065

Try this:

static ssize_t read_all(int fdes, void* buffer, ssize_t size)
{
 ssize_t totalRead = 0;
 while(totalRead != size)
 {
 ssize_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}
added 3 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065

Try this:

static size_tssize_t read_all(int fdes, void* buffer, size_tssize_t size)
{
 size_tssize_t totalRead = 0;
 while(totalRead != size)
 {
 size_tssize_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065

Try this:

static size_t read_all(int fdes, void* buffer, size_t size)
{
 size_t totalRead = 0;
 while(totalRead != size)
 {
 size_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065

Try this:

static ssize_t read_all(int fdes, void* buffer, ssize_t size)
{
 ssize_t totalRead = 0;
 while(totalRead != size)
 {
 ssize_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}
Fixed spelling of an errno
Source Link
vnp
  • 58.7k
  • 4
  • 55
  • 144

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065

Try this:

static size_t read_all(int fdes, void* buffer, size_t size)
{
 size_t totalRead = 0;
 while(totalRead != size)
 {
 size_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTEREINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065

Try this:

static size_t read_all(int fdes, void* buffer, size_t size)
{
 size_t totalRead = 0;
 while(totalRead != size)
 {
 size_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTER)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}

My main problem is the recursion. I don't think its major but I personally would use a loop. Given the current layout I can't quite convince myself that it works in all situations (especially since there are two alternative recursive calls).

Declare variables close to the usage point (rather than everything at the top).

 ssize_t ret;
 ssize_t ret2;

C has been updated so that you can declare variables at any point in a function. This helps in readability as you don't need to scroll far to find the variable declaration. Also if you declare in the most restrictive scope possible it helps to prevent data leaking (out of a scope).

see: http://stackoverflow.com/a/8474123/14065

Try this:

static size_t read_all(int fdes, void* buffer, size_t size)
{
 size_t totalRead = 0;
 while(totalRead != size)
 {
 size_t thisRead = read(fdes, buffer + totalRead, size - totalRead);
 if ((thisRead == -1) && (errno == EINTR)) {
 continue;
 }
 // Note: There are other errors that may not be erros.
 // EAGAIN or EWOULDBLOCK spring to mind but may need special handling
 // as immediately calling read may just cause a busy wait you may
 // want to suspend the thread by calling sleep..
 if (thisRead == -1) {
 return -1;
 }
 if (thisRead == 0) {
 break;
 }
 totalRead += thisRead;
 }
 return totalRead;
}
added 296 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
added 49 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
added 56 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
added 73 characters in body
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
Source Link
Loki Astari
  • 97.7k
  • 5
  • 126
  • 341
Loading
lang-c

AltStyle によって変換されたページ (->オリジナル) /