OpenPrinting / cups

OpenPrinting CUPS Sources
https://openprinting.github.io/cups
Apache License 2.0
1.05k stars 187 forks source link

the first call of cupsGetDestMediaByName returns a wrong size #993

Closed xiaowen581 closed 1 month ago

xiaowen581 commented 3 months ago

Describe the bug the first call of cupsGetDestMediaByName returns a wrong size.

To Reproduce Steps to reproduce the behavior:

  1. install local printer using everywhere.
  2. run the following function ` void bug_report() { cups_dest_t dest = cupsGetNamedDest(NULL, "ricoh_bug_report", NULL); cups_dinfo_t dinfo = cupsCopyDestInfo(CUPS_HTTP_DEFAULT, dest); http_t* http = cupsConnectDest(dest, CUPS_DEST_FLAGS_NONE, 30000, NULL, NULL, 0, NULL, NULL); cups_size_t size; if (cupsGetDestMediaByName(http, dest, dinfo, "om_a-4-borderless_210x297mm", CUPS_MEDIA_FLAGS_BORDERLESS, &size) != 0) { cout << "first time: " << endl; cout << " media:" << size.media << endl; cout << " width: " << size.width << endl; cout << " length: " << size.length << endl; cout << " bottom: " << size.bottom << endl; cout << " left: " << size.left << endl; cout << " right: " << size.right << endl; cout << " top: " << size.top << endl; }

    if (cupsGetDestMediaByName(http, dest, dinfo, "om_a-4-borderless_210x297mm", CUPS_MEDIA_FLAGS_BORDERLESS, &size) != 0) { cout << "second time: " << endl; cout << " media:" << size.media << endl; cout << " width: " << size.width << endl; cout << " length: " << size.length << endl; cout << " bottom: " << size.bottom << endl; cout << " left: " << size.left << endl; cout << " right: " << size.right << endl; cout << " top: " << size.top << endl; } } `

the output:


first time: media:custom_8.25x14in_8.25x14in_borderless width: 20955 length: 35560 bottom: 0 left: 0 right: 0 top: 0 second time: media:iso_a4_210x297mm_borderless width: 21000 length: 29700 bottom: 0 left: 0 right: 0 top: 0


System Information:

Additional context It seems that the value of "pwg" in cups_get_media_db was changed in "cups_create_media_db", which is possibly not on demand.

cups_debug.zip ipp_tool_result.txt

xiaowen581 commented 3 months ago

First, I made a mistake in the example above. I realized that the "media" parameter must be a PWG media name. When I changed the media to "iso_a4_210x297mm", I was able to get the correct result even on the first call.

However, this API still returns a wrong size when I call a non-standard media name for the first time, such as "custom_8.25x14in_8.25x14in".

This is because pwgMediaForSize in cupsGetDestMediaByName/cupsGetDestMediaBySize returns the thread-local storage (&cg->pwg_media) if finding a non-standard media name, and the value here is overwritten when a non-standard media name is encountered during traversal in cups_create_media_db.

Therefore, I believe it is necessary to re-fetch the value of pwg after cups_create_media_db.

michaelrsweet commented 1 month ago

[master 6c7c5b3e4] Fix cupsGetDestMediaByName and friends (Issue #993)

[2.4.x 7a205e331] Fix cupsGetDestMediaByName and friends (Issue #993)