it is not clear if + 1 is needed or not here:
int len = strlen(TARGET);
info = malloc( len + 1 );
because few lines above it it already was once appended to it:
TARGET[END - START] = '\0';
if it is needed then perhaps also.. appending the \0 is needed.
int len = strlen(TARGET);
info = malloc( len + 1 );
strcpy(info, TARGET);
info[len] = '\0';
Q: How to determine if a string already has the
null termination
perhaps if it already has it.. appending another one wouldn't be logic.
full function :
char * FUNCTION ( char * v ){
char *TARGET = NULL;
const char *PATTERN1 = "co=";
const char *PATTERN2 = "&";
char *START = strstr(v, PATTERN1);
if (START) {
START = START + strlen(PATTERN1);
char *END = strstr(START, PATTERN2);
if (!END){
END = START + strlen(START);
}
TARGET = malloc(END - START + 1);
memcpy(TARGET, START, END - START);
TARGET[END - START] = '\0';
}
if (!START || TARGET == NULL || TARGET[0] == '\0') {
return 0;
}
int len = strlen(TARGET);
info = malloc( len + 1 );
strcpy(info, TARGET);
info[len] = '\0';
return info;
}
strlenif the string weren't null-terminated.