Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
     if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' && *endptr != '0円') { // junk at the end
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    
  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
     if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' && *endptr != '0円') { // junk at the end
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    
  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
     if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' && *endptr != '0円') { // junk at the end
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    
edited body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 96
  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
     if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' ||&& *endptr != '0円') { // junk at the end
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    
  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
     if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' || *endptr != '0円') { // junk at the end
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    
  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
     if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' && *endptr != '0円') { // junk at the end
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    
added 71 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 96

Only a bit to a bit to add beyond @pacmaninbw answer:

  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
      if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
    
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' || *endptr != '0円') { // junk at the end
     *value_int = 0;
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    

Only a bit to a bit to add beyond @pacmaninbw answer:

  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' || *endptr != '0円') { // junk at the end
     *value_int = 0;
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    

Only a bit to add beyond @pacmaninbw answer:

  1. Better to write code that gets to the point and avoid magic numbers. Then the comment becomes unnecessary. A new-line is not specified to be 10.

     // if((*ptr) != 10 && (*ptr) != 0){ /*If it's not a newline or a null then invalid ...
     if(*ptr != '\n' && *ptr != '0円') {
    
  2. Note: Code lacks robust error detection/handling in "string to long to int" conversion. Could use you own strtoi() or this or roll your own conversion like below:

     // return true if error
     bool parse_int(const char *token, int *value_int) {
     char *endptr;
     errno = 0;
     long value_long = strtol(token, &endptr, /* base */ 10);
     if (token == endptr) { // no conversion
     *value_int = 0;
     return true; 
     }
     #if INT_MAX < LONG_MAX
      if (value_long > INT_MAX) {
     *value_int = INT_MAX;
     errno = ERANGE;
     return true;
     }
     #endif
     #if INT_MIN > LONG_MIN
     if (value_long < INT_MIN) {
     *value_int = INT_MIN;
     errno = ERANGE;
     return true;
     }
     #endif 
    
     *value_int = (int) value_long;
     if (errno) {
     return true; // outside long range or other ID error
     }
     if (*endptr != '\n' || *endptr != '0円') { // junk at the end
     return true; 
     }
     return false;
     }
    
  3. By keeping variables local to where they are needed, rather than declaring them far above, it is easier to review.

     // long node_value;
     .... 12 lines
     while(token != NULL){
     long node_value = strtol(token, &ptr, 10); /*Convert each string to integer*/
     ... 
     add_node(node_value);
    
added 549 characters in body
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 96
Loading
Source Link
chux
  • 36.4k
  • 2
  • 43
  • 96
Loading
lang-c

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