Skip to content

Commit

Permalink
Add malloc checks (Issue #113)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Dec 28, 2020
1 parent d17d87e commit 1e94f8e
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 29 deletions.
38 changes: 33 additions & 5 deletions pappl/device-network.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,27 @@ pappl_dnssd_get_device(
# endif // HAVE_DNSSD

free(device->fullName);
device->fullName = strdup(fullName);

if ((device->fullName = strdup(fullName)) == NULL)
{
cupsArrayRemove(devices, device);
return (NULL);
}
}

return (device);
}

// Yes, add the device...
device = calloc(sizeof(_pappl_dns_sd_dev_t), 1);
device->name = strdup(serviceName);
if ((device = calloc(sizeof(_pappl_dns_sd_dev_t), 1)) == NULL)
return (NULL);

if ((device->name = strdup(serviceName)) == NULL)
{
free(device);
return (NULL);
}

device->domain = strdup(replyDomain);

cupsArrayAdd(devices, device);
Expand All @@ -263,7 +275,11 @@ pappl_dnssd_get_device(
avahi_service_name_join(fullName, sizeof(fullName), serviceName, "_pdl-datastream._tcp.", replyDomain);
# endif /* HAVE_DNSSD */

device->fullName = strdup(fullName);
if ((device->fullName = strdup(fullName)) == NULL)
{
cupsArrayRemove(devices, device);
return (NULL);
}

// Query the TXT record for the device ID and make and model...
#ifdef HAVE_DNSSD
Expand Down Expand Up @@ -1056,11 +1072,23 @@ pappl_snmp_read_response(
}

// Add the device and request the device data
temp = calloc(1, sizeof(_pappl_snmp_dev_t));
if ((temp = calloc(1, sizeof(_pappl_snmp_dev_t))) == NULL)
{
_PAPPL_DEBUG("pappl_snmp_read_response: Unable to allocate memory for device.\n");
return;
}

temp->address = packet.address;
temp->addrname = strdup(addrname);
temp->port = 9100; // Default port to use

if (!temp->addrname)
{
_PAPPL_DEBUG("pappl_snmp_read_response: Unable to allocate memory for device name.\n");
free(temp);
return;
}

cupsArrayAdd(devices, temp);

_papplSNMPWrite(fd, &(packet.address), _PAPPL_SNMP_VERSION_1, packet.community, _PAPPL_ASN1_GET_REQUEST, _PAPPL_SNMP_QUERY_DEVICE_SYSNAME, SysNameOID);
Expand Down
28 changes: 17 additions & 11 deletions pappl/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,23 @@ papplDeviceAddScheme(
// Add the scheme...
if ((ds = (_pappl_devscheme_t *)calloc(1, sizeof(_pappl_devscheme_t))) != NULL)
{
ds->scheme = strdup(scheme);
ds->dtype = dtype;
ds->list_cb = list_cb;
ds->open_cb = open_cb;
ds->close_cb = close_cb;
ds->read_cb = read_cb;
ds->write_cb = write_cb;
ds->status_cb = status_cb;
ds->id_cb = id_cb;

cupsArrayAdd(device_schemes, ds);
if ((ds->scheme = strdup(scheme)) != NULL)
{
ds->dtype = dtype;
ds->list_cb = list_cb;
ds->open_cb = open_cb;
ds->close_cb = close_cb;
ds->read_cb = read_cb;
ds->write_cb = write_cb;
ds->status_cb = status_cb;
ds->id_cb = id_cb;

cupsArrayAdd(device_schemes, ds);
}
else
{
free(ds);
}
}

pthread_rwlock_unlock(&device_rwlock);
Expand Down
33 changes: 24 additions & 9 deletions pappl/dnssd.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,11 +450,17 @@ _papplPrinterRegisterDNSSDNoLock(
}

free(printer->dns_sd_name);
printer->dns_sd_name = strdup(new_dns_sd_name);

papplLogPrinter(printer, PAPPL_LOGLEVEL_INFO, "DNS-SD name collision, trying new DNS-SD service name '%s'.", printer->dns_sd_name);
if ((printer->dns_sd_name = strdup(new_dns_sd_name)) != NULL)
{
papplLogPrinter(printer, PAPPL_LOGLEVEL_INFO, "DNS-SD name collision, trying new DNS-SD service name '%s'.", printer->dns_sd_name);

printer->dns_sd_collision = false;
printer->dns_sd_collision = false;
}
else
{
papplLogPrinter(printer, PAPPL_LOGLEVEL_INFO, "DNS-SD name collision, failed to allocate new DNS-SD service name.");
return (false);
}
}

master = _papplDNSSDInit(printer->system);
Expand Down Expand Up @@ -678,8 +684,9 @@ _papplPrinterRegisterDNSSDNoLock(
if (system->subtypes && *system->subtypes)
{
char *temptypes = strdup(system->subtypes), *start, *end;
// Pointers into sub-types...

for (start = temptypes; *start; start = end)
for (start = temptypes; start && *start; start = end)
{
if ((end = strchr(start, ',')) != NULL)
*end++ = '\0';
Expand All @@ -706,8 +713,9 @@ _papplPrinterRegisterDNSSDNoLock(
if (system->subtypes && *system->subtypes)
{
char *temptypes = strdup(system->subtypes), *start, *end;
// Pointers into sub-types...

for (start = temptypes; *start; start = end)
for (start = temptypes; start && *start; start = end)
{
if ((end = strchr(start, ',')) != NULL)
*end++ = '\0';
Expand Down Expand Up @@ -902,11 +910,18 @@ _papplSystemRegisterDNSSDNoLock(
}

free(system->dns_sd_name);
system->dns_sd_name = strdup(new_dns_sd_name);

papplLog(system, PAPPL_LOGLEVEL_INFO, "DNS-SD name collision, trying new DNS-SD service name '%s'.", system->dns_sd_name);
if ((system->dns_sd_name = strdup(new_dns_sd_name)) != NULL)
{
papplLog(system, PAPPL_LOGLEVEL_INFO, "DNS-SD name collision, trying new DNS-SD service name '%s'.", system->dns_sd_name);

system->dns_sd_collision = false;
system->dns_sd_collision = false;
}
else
{
papplLog(system, PAPPL_LOGLEVEL_ERROR, "DNS-SD name collision, unable to allocate new DNS-SD service name.");
return (false);
}
}

master = _papplDNSSDInit(system);
Expand Down
17 changes: 13 additions & 4 deletions pappl/job.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,21 @@ _papplJobSubmitFile(
}

// Save the print file information...
job->filename = strdup(filename);
if ((job->filename = strdup(filename)) != NULL)
{
// Process the job...
job->state = IPP_JSTATE_PENDING;

// Process the job...
job->state = IPP_JSTATE_PENDING;
_papplPrinterCheckJobs(job->printer);
}
else
{
// Abort the job...
job->state = IPP_JSTATE_ABORTED;

_papplPrinterCheckJobs(job->printer);
papplLogJob(job, PAPPL_LOGLEVEL_ERROR, "Unable to allocate filename.");
unlink(filename);
}
}


Expand Down
6 changes: 6 additions & 0 deletions pappl/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ copy_link(_pappl_link_t *l) // I - Current link
newl->label = strdup(l->label);
newl->path_or_url = strdup(l->path_or_url);
newl->options = l->options;

if (!newl->label || !newl->path_or_url)
{
free_link(newl);
return (NULL);
}
}

return (newl);
Expand Down

0 comments on commit 1e94f8e

Please sign in to comment.