Skip to content

examples: add an example for logging failed transfers - #18668

Closed
jay wants to merge 1 commit into
curl:masterfrom
jay:logfailed
Closed

jay wants to merge 1 commit into
curl:masterfrom
jay:logfailed

Conversation

@jay

@jay jay commented Sep 21, 2025

Copy link
Copy Markdown
Member
  • Add an example that demonstrates per-transfer verbose logging to memory.

The transfer's log is written to disk only if the transfer fails.

Closes #xxxx

Comment thread docs/examples/log_failed_transfers.c Resolved
@jay

jay commented Sep 21, 2025

Copy link
Copy Markdown
Member Author

I've pushed a fix for some of these CI warnings, some seem unnecessary like require a prototype for every function in an example. Some I didn't push a fix for, they seem arguably wrong, for example:

/home/runner/work/curl/curl/docs/examples/log_failed_transfers.c:77:3: error: missing initializer for field ‘log’ of ‘struct transfer’ [-Werror=missing-field-initializers]
   77 |   { "https://httpbin.org/status/400", "400.txt", "400_transfer_log.txt", },

Some of those variables are not yet known so they should initialize to zero, this is common practice, here is the code:

struct transfer {
  const char *url, *bodyfile, *logfile;
  struct mem log;
  FILE *bodyfp;
  CURL *curl;
};

struct transfer transfer[] = {
  { "https://httpbin.org/get", "200.txt", "200_transfer_log.txt", },
  { "https://httpbin.org/status/400", "400.txt", "400_transfer_log.txt", },
};

Here is another one:

/home/runner/work/curl/curl/docs/examples/log_failed_transfers.c: In function ‘mem_addf’:
/home/runner/work/curl/curl/docs/examples/log_failed_transfers.c:150:29: error: comparison is always false due to limited range of data type [-Werror=type-limits]
  150 |     if(x < 0 || (unsigned)x >= SIZE_MAX || mem_need(mem, (size_t)x + 1) < 0)
      |                             ^~

It may be true for that compiler at that time but it may not be false for a different compiler. It is an overflow check, just because it tests always false for that compiler doesn't mean anything. So in this case I would have to get rid of the overflow checks, which seems restrictive.

@bagder

bagder commented Sep 21, 2025

Copy link
Copy Markdown
Member

It may be true for that compiler at that time but it may not be false for a different compiler

It can only ever be true if sizeof(unsigned int) == sizeof(size_t), which probably implies a 32 bit architecture. And (unsigned)x can actually never be larger than SIZE_MAX even then.

@jay

jay commented Sep 22, 2025

Copy link
Copy Markdown
Member Author

AFAICT it's not guaranteed that INT_MAX is going to be less than SIZE_MAX because it's not guaranteed that SIZE_MAX >= UINT_MAX. Therefore positive int x may not be a subset of size_t range and it should be checked for overflow. Practically though it's unlikely. I think it's more correct to check but I will remove it to satisfy CI.

@jay

jay commented Sep 22, 2025

Copy link
Copy Markdown
Member Author

Here's another one that's restrictive:

../../../docs/examples/log_failed_transfers.c:152:67: error: format string is not a string literal [-Werror,-Wformat-nonliteral]
  152 |     x = vsnprintf(mem->buf + mem->len, mem->allocsize - mem->len, format, va);
      |                                                                   ^~~~~~

I'm going to put in a pragma to ignore this specific one, we do that elsewhere

@jay
jay force-pushed the logfailed branch 4 times, most recently from 1217c1b to 65f13b8 Compare September 22, 2025 14:46
Comment thread docs/examples/log_failed_transfers.c Resolved
@jay

jay commented Sep 22, 2025

Copy link
Copy Markdown
Member Author

It looks like ignored "-Wformat-nonliteral" works for every CI compiler except for the VS2022 clang.

Comment thread docs/examples/log_failed_transfers.c Resolved
- Add an example that demonstrates per-transfer verbose logging to
  memory.

The transfer's log is written to disk only if the transfer fails.

Closes curl#18668
@jay

jay commented Oct 8, 2025 •

Copy link
Copy Markdown
Member Author

Why is vsnprintf now banned from examples code?

./docs/examples/log_failed_transfers.c:151:9: warning: use of vsnprintf is banned (BANNEDFUNC)
     x = vsnprintf(mem->buf + mem->len, mem->allocsize - mem->len, format, va);
         ^

@bagder

bagder commented Oct 9, 2025

Copy link
Copy Markdown
Member

Why is vsnprintf now banned from examples code?

@vszakats ? I think we should allow it for examples. It keeps them simple and the fact that it is broken in some older Microsoft compilers is not reason enough to avoid it there I think.

@vszakats

vszakats commented Oct 9, 2025 •

Copy link
Copy Markdown
Member

Why is vsnprintf now banned from examples code?

@vszakats ? I think we should allow it for examples. It keeps them simple and the fact that it is broken in some older Microsoft compilers is not reason enough to avoid it there I think.

It just wasn't used before, and thus I did not add it to the local .checksrc. It can be added there if needed.

edit: done via #18970

vszakats added a commit to vszakats/curl that referenced this pull request Oct 9, 2025
vszakats added a commit that referenced this pull request Oct 9, 2025
@jay jay closed this in 0780de2 Oct 9, 2025
vszakats added a commit to vszakats/curl that referenced this pull request Oct 10, 2025
- include `windows.h` after `winsock2.h` via `curl/curl.h`.
- avoid `errno` for WinCE.
- avoid `_vscprintf` for WinCE.

Ref: 4535532 curl#18843
Follow-up to 0780de2 curl#18668
Closes curl#19016
@jay
jay deleted the logfailed branch October 11, 2025 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants