Skip to main content
added 95 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

Goal unclear

OP has "I want a 'view' not a copy.", yet uses malloc to make a copy.

Lacking documentation

Lacking documentation

Goal unclear

OP has "I want a 'view' not a copy.", yet uses malloc to make a copy.

Lacking documentation

Post Undeleted by chux
Post Deleted by chux
added 52 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

It appears that the function is to slice an array of char. Yet "%.*s" terminates early if a null character is found. This is a bug for an array function.

A helper function that existsexits!?

Use an auto formatformatter.

Recall size_t is an unsigned type and i - *scan_from can become a large value near SIZE_MAX.

I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *

In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead for an array function.

It appears that the function is to slice an array of char. Yet "%.*s" terminates early if a null character is found.

A helper function that exists!?

Use an auto format.

Recall size_t is an unsigned type and i - *scan_from become a large value near SIZE_MAX.

I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *

In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead.

It appears that the function is to slice an array of char. Yet "%.*s" terminates early if a null character is found. This is a bug for an array function.

A helper function that exits!?

Use an auto formatter.

Recall size_t is an unsigned type and i - *scan_from can become a large value near SIZE_MAX.

I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use.

In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead for an array function.

added 369 characters in body
Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97

Poor for code to have a buried memory allocation yet does not advise that the caller should free it.

"to create a non-generic slice function. I want ....", "The scan_from arg is ... " and "Use is for ..." deserve to be in code as a comment and/or code should be clear enough to explain itself.

Document at a high level as if caller has access to the function declaration and not the definition. (in the .h file)

Array or string?

exit(ENOMEM) is surprising and undocumented in function description. Further @Mr R Further, ENOMEM is not part of the standard C lib and reduces portability.

// assert(substr_length == snprintf(substr, substr_length + 1, // "%.*s", (int)substr_length, s + *scan_from); int len = snprintf(substr, substr_length + 1, "%.*s", (int)substr_length, s + *scan_from); assert(len >= 0 && (unsigned) len == substr_length); 

const return?

I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *

Bug

In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead.

"to create a non-generic slice function. I want ....", "The scan_from arg is ... " and "Use is for ..." deserve to be in code as a comment and/or code should be clear enough to explain itself.

Array or string?

exit(ENOMEM) is surprising and undocumented. Further, ENOMEM is not part of the standard C lib and reduces portability.

// assert(substr_length == snprintf(substr, substr_length + 1, // "%.*s", (int)substr_length, s + *scan_from); int len = snprintf(substr, substr_length + 1, "%.*s", (int)substr_length, s + *scan_from); assert(len == substr_length); 

Poor for code to have a buried memory allocation yet does not advise that the caller should free it.

"to create a non-generic slice function. I want ....", "The scan_from arg is ... " and "Use is for ..." deserve to be in code as a comment and/or code should be clear enough to explain itself.

Document at a high level as if caller has access to the function declaration and not the definition. (in the .h file)

Array or string?

exit(ENOMEM) is surprising and undocumented in function description. @Mr R Further, ENOMEM is not part of the standard C lib and reduces portability.

// assert(substr_length == snprintf(substr, substr_length + 1, // "%.*s", (int)substr_length, s + *scan_from); int len = snprintf(substr, substr_length + 1, "%.*s", (int)substr_length, s + *scan_from); assert(len >= 0 && (unsigned) len == substr_length); 

const return?

I'd expect a return types char * and not const char * as code is providing a allocated buffer for caller's use. const char *

Bug

In extreme use (int)substr_length fails when substr_length > INT_MAX. Function description would mention that. Better code would work for all substr_length. Consider memcpy() instead.

Source Link
chux
  • 36.5k
  • 2
  • 43
  • 97
Loading