From: Michael Sweet Date: Tue, 25 Jul 2017 22:11:01 +0000 (-0400) Subject: Add error checking for all Bonjour browsing in cupsEnumDests. X-Git-Tag: v2.2.5~71 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ced9dda82671ebb67ff85234e3abf1ebd12fd714;p=thirdparty%2Fcups.git Add error checking for all Bonjour browsing in cupsEnumDests. Clean up testcups unit tests. --- diff --git a/cups/dest.c b/cups/dest.c index 4fa54ddcd3..2a0e78546b 100644 --- a/cups/dest.c +++ b/cups/dest.c @@ -61,6 +61,7 @@ #endif /* __APPLE__ */ #if defined(HAVE_DNSSD) || defined(HAVE_AVAHI) +# define _CUPS_DNSSD_GET_DESTS 250 /* Milliseconds for cupsGetDests */ # define _CUPS_DNSSD_MAXTIME 50 /* Milliseconds for maximum quantum of time */ #endif /* HAVE_DNSSD || HAVE_AVAHI */ @@ -1154,27 +1155,37 @@ cupsEnumDests( main_fd = DNSServiceRefSockFD(data.main_ref); ipp_ref = data.main_ref; - DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0, - "_ipp._tcp", NULL, - (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data); + if (DNSServiceBrowse(&ipp_ref, kDNSServiceFlagsShareConnection, 0, "_ipp._tcp", NULL, (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data) != kDNSServiceErr_NoError) + { + DEBUG_puts("1cupsEnumDests: Unable to create IPP browser, returning 0."); + DNSServiceRefDeallocate(data.main_ref); + return (0); + } local_ipp_ref = data.main_ref; - DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection, - kDNSServiceInterfaceIndexLocalOnly, - "_ipp._tcp", NULL, - (DNSServiceBrowseReply)cups_dnssd_local_cb, &data); + if (DNSServiceBrowse(&local_ipp_ref, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexLocalOnly, "_ipp._tcp", NULL, (DNSServiceBrowseReply)cups_dnssd_local_cb, &data) != kDNSServiceErr_NoError) + { + DEBUG_puts("1cupsEnumDests: Unable to create local IPP browser, returning 0."); + DNSServiceRefDeallocate(data.main_ref); + return (0); + } # ifdef HAVE_SSL ipps_ref = data.main_ref; - DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0, - "_ipps._tcp", NULL, - (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data); + if (DNSServiceBrowse(&ipps_ref, kDNSServiceFlagsShareConnection, 0, "_ipps._tcp", NULL, (DNSServiceBrowseReply)cups_dnssd_browse_cb, &data) != kDNSServiceErr_NoError) + { + DEBUG_puts("1cupsEnumDests: Unable to create IPPS browser, returning 0."); + DNSServiceRefDeallocate(data.main_ref); + return (0); + } local_ipps_ref = data.main_ref; - DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection, - kDNSServiceInterfaceIndexLocalOnly, - "_ipps._tcp", NULL, - (DNSServiceBrowseReply)cups_dnssd_local_cb, &data); + if (DNSServiceBrowse(&local_ipps_ref, kDNSServiceFlagsShareConnection, kDNSServiceInterfaceIndexLocalOnly, "_ipps._tcp", NULL, (DNSServiceBrowseReply)cups_dnssd_local_cb, &data) != kDNSServiceErr_NoError) + { + DEBUG_puts("1cupsEnumDests: Unable to create local IPPS browser, returning 0."); + DNSServiceRefDeallocate(data.main_ref); + return (0); + } # endif /* HAVE_SSL */ # else /* HAVE_AVAHI */ @@ -1197,11 +1208,26 @@ cupsEnumDests( } data.browsers = 1; - ipp_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL, 0, cups_dnssd_browse_cb, &data); + if ((ipp_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipp._tcp", NULL, 0, cups_dnssd_browse_cb, &data)) == NULL) + { + DEBUG_puts("1cupsEnumDests: Unable to create Avahi IPP browser, returning 0."); + + avahi_client_free(data.client); + avahi_simple_poll_free(data.simple_poll); + return (0); + } # ifdef HAVE_SSL data.browsers ++; - ipps_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipps._tcp", NULL, 0, cups_dnssd_browse_cb, &data); + if ((ipps_ref = avahi_service_browser_new(data.client, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, "_ipps._tcp", NULL, 0, cups_dnssd_browse_cb, &data)) == NULL) + { + DEBUG_puts("1cupsEnumDests: Unable to create Avahi IPPS browser, returning 0."); + + avahi_service_browser_free(ipp_ref); + avahi_client_free(data.client); + avahi_simple_poll_free(data.simple_poll); + return (0); + } # endif /* HAVE_SSL */ # endif /* HAVE_DNSSD */ @@ -1239,6 +1265,8 @@ cupsEnumDests( if (nfds > 0) DNSServiceProcessResult(data.main_ref); + else if (nfds < 0 && errno != EINTR && errno != EAGAIN) + break; # else /* HAVE_AVAHI */ data.got_data = 0; @@ -2070,7 +2098,7 @@ cupsGetDests2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_ data.num_dests = 0; data.dests = NULL; - cupsEnumDests(0, 250, NULL, 0, 0, (cups_dest_cb_t)cups_get_cb, &data); + cupsEnumDests(0, _CUPS_DNSSD_GET_DESTS, NULL, 0, 0, (cups_dest_cb_t)cups_get_cb, &data); /* * Make a copy of the "real" queues for a later sanity check... diff --git a/cups/testcups.c b/cups/testcups.c index aa58766157..f4931c5002 100644 --- a/cups/testcups.c +++ b/cups/testcups.c @@ -1,7 +1,7 @@ /* * CUPS API test program for CUPS. * - * Copyright 2007-2014 by Apple Inc. + * Copyright 2007-2017 by Apple Inc. * Copyright 2007 by Easy Software Products. * * These coded instructions, statements, and computer programs are the @@ -47,7 +47,9 @@ main(int argc, /* I - Number of command-line arguments */ cups_dest_t *dests, /* Destinations */ *dest, /* Current destination */ *named_dest; /* Current named destination */ - const char *ppdfile; /* PPD file */ + const char *dest_name, /* Destination name */ + *dval, /* Destination value */ + *ppdfile; /* PPD file */ ppd_file_t *ppd; /* PPD file data */ int num_jobs; /* Number of jobs for queue */ cups_job_t *jobs; /* Jobs for queue */ @@ -360,11 +362,19 @@ main(int argc, /* I - Number of command-line arguments */ * cupsGetDest(printer) */ - printf("cupsGetDest(\"%s\"): ", dests[num_dests / 2].name); + for (i = 0, dest_name = NULL; i < num_dests; i ++) + { + if ((dval = cupsGetOption("printer-is-temporary", dests[i].num_options, dest[i].options)) != NULL && !strcmp(dval, "false")) + { + dest_name = dests[i].name; + break; + } + } + + printf("cupsGetDest(\"%s\"): ", dest_name ? dest_name : "(null)"); fflush(stdout); - if ((dest = cupsGetDest(dests[num_dests / 2].name, NULL, num_dests, - dests)) == NULL) + if ((dest = cupsGetDest(dest_name, NULL, num_dests, dests)) == NULL) { puts("FAIL"); return (1); @@ -380,8 +390,7 @@ main(int argc, /* I - Number of command-line arguments */ dest->instance ? dest->instance : "(null)"); fflush(stdout); - if ((named_dest = cupsGetNamedDest(NULL, dest->name, - dest->instance)) == NULL || + if ((named_dest = cupsGetNamedDest(NULL, dest->name, dest->instance)) == NULL || !dests_equal(dest, named_dest)) { if (named_dest) @@ -408,7 +417,7 @@ main(int argc, /* I - Number of command-line arguments */ fputs("cupsPrintFile: ", stdout); fflush(stdout); - if (cupsPrintFile(dest->name, "../data/testprint", "Test Page", + if (cupsPrintFile(dest->name, "../test/testfile.pdf", "Test Page", dest->num_options, dest->options) <= 0) { printf("FAIL (%s)\n", cupsLastErrorString()); @@ -421,7 +430,7 @@ main(int argc, /* I - Number of command-line arguments */ * cupsGetPPD(printer) */ - fputs("cupsGetPPD(): ", stdout); + fputs("cupsGetPPD: ", stdout); fflush(stdout); if ((ppdfile = cupsGetPPD(dest->name)) == NULL) @@ -436,7 +445,7 @@ main(int argc, /* I - Number of command-line arguments */ * ppdOpenFile() */ - fputs("ppdOpenFile(): ", stdout); + fputs("ppdOpenFile: ", stdout); fflush(stdout); if ((ppd = ppdOpenFile(ppdfile)) == NULL) @@ -550,33 +559,39 @@ show_diffs(cups_dest_t *a, /* I - First destination */ { int i; /* Looping var */ cups_option_t *aoption; /* Current option */ + cups_option_t *boption; /* Current option */ const char *bval; /* Option value */ if (!a || !b) return; - puts(" Item cupsGetDest cupsGetNamedDest"); - puts(" -------------------- -------------------- --------------------"); + puts(" Item cupsGetDest cupsGetNamedDest"); + puts(" -------------------- ------------------------ ------------------------"); if (_cups_strcasecmp(a->name, b->name)) - printf(" name %-20.20s %-20.20s\n", a->name, b->name); + printf(" name %-24.24s %-24.24s\n", a->name, b->name); if ((a->instance && !b->instance) || (!a->instance && b->instance) || (a->instance && _cups_strcasecmp(a->instance, b->instance))) - printf(" instance %-20.20s %-20.20s\n", + printf(" instance %-24.24s %-24.24s\n", a->instance ? a->instance : "(null)", b->instance ? b->instance : "(null)"); if (a->num_options != b->num_options) - printf(" num_options %-20d %-20d\n", a->num_options, + printf(" num_options %-24d %-24d\n", a->num_options, b->num_options); for (i = a->num_options, aoption = a->options; i > 0; i --, aoption ++) if ((bval = cupsGetOption(aoption->name, b->num_options, b->options)) == NULL || strcmp(aoption->value, bval)) - printf(" %-20.20s %-20.20s %-20.20s\n", aoption->name, + printf(" %-20.20s %-24.24s %-24.24s\n", aoption->name, aoption->value, bval ? bval : "(null)"); + + for (i = b->num_options, boption = b->options; i > 0; i --, boption ++) + if (!cupsGetOption(boption->name, a->num_options, a->options)) + printf(" %-20.20s %-24.24s %-24.24s\n", boption->name, + boption->value, "(null)"); }