infof: remove newline from format strings, always append it

- the data needs to be "line-based" anyway since it's also passed to the
  debug callback/application

- it makes infof() work like failf() and consistency is good

- there's an assert that triggers on newlines in the format string

- Also removes a few instances of "..."

- Removes the code that would append "..." to the end of the data *iff*
  it was truncated in infof()

Closes #7357
This commit is contained in:
Daniel Stenberg 2021-07-06 17:05:17 +02:00
parent 1026b36ea0
commit e7416cfd2b
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
69 changed files with 936 additions and 947 deletions

View file

@ -79,30 +79,39 @@ unit_stop(void)
curl_global_cleanup();
}
static int verify(const char *info, const char *two)
{
/* the 'info' one has a newline appended */
char *nl = strchr(info, '\n');
if(!nl)
return 1; /* nope */
return strncmp(info, two, nl - info);
}
UNITTEST_START
/* Injecting a simple short string via a format */
msnprintf(input, sizeof(input), "Simple Test");
Curl_infof(data, "%s", input);
fail_unless(strcmp(result, input) == 0, "Simple string test");
fail_unless(verify(result, input) == 0, "Simple string test");
/* Injecting a few different variables with a format */
Curl_infof(data, "%s %u testing %lu\n", input, 42, 43L);
fail_unless(strcmp(result, "Simple Test 42 testing 43\n") == 0,
Curl_infof(data, "%s %u testing %lu", input, 42, 43L);
fail_unless(verify(result, "Simple Test 42 testing 43\n") == 0,
"Format string");
/* Variations of empty strings */
Curl_infof(data, "");
fail_unless(strlen(result) == 0, "Empty string");
fail_unless(strlen(result) == 1, "Empty string");
Curl_infof(data, "%s", NULL);
fail_unless(strcmp(result, "(nil)") == 0, "Passing NULL as string");
fail_unless(verify(result, "(nil)") == 0, "Passing NULL as string");
/* A string just long enough to not be truncated */
memset(input, '\0', sizeof(input));
memset(input, 'A', 2048);
Curl_infof(data, "%s", input);
fail_unless(strlen(result) == 2048, "No truncation of infof input");
fail_unless(strcmp(result, input) == 0, "No truncation of infof input");
fail_unless(verify(result, input) == 0, "No truncation of infof input");
fail_unless(result[sizeof(result) - 1] == '\0',
"No truncation of infof input");
@ -111,25 +120,20 @@ memset(input + 2047, 'A', 4);
Curl_infof(data, "%s", input);
fail_unless(strlen(result) == 2048, "Truncation of infof input 1");
fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 1");
fail_unless(strncmp(result + 2045, "...", 3) == 0,
"Truncation of infof input 1");
/* Just over the limit for truncation with newline */
memset(input + 2047, 'A', 4);
memset(input + 2047 + 4, '\n', 1);
Curl_infof(data, "%s\n", input);
Curl_infof(data, "%s", input);
fail_unless(strlen(result) == 2048, "Truncation of infof input 2");
fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 2");
fail_unless(strncmp(result + 2044, "...", 3) == 0,
"Truncation of infof input 2");
/* Way over the limit for truncation with newline */
memset(input, '\0', sizeof(input));
memset(input, 'A', sizeof(input) - 1);
Curl_infof(data, "%s\n", input);
Curl_infof(data, "%s", input);
fail_unless(strlen(result) == 2048, "Truncation of infof input 3");
fail_unless(result[sizeof(result) - 1] == '\0', "Truncation of infof input 3");
fail_unless(strncmp(result + 2044, "...", 3) == 0,
"Truncation of infof input 3");
UNITTEST_STOP