Skip to content
This repository was archived by the owner on Mar 10, 2026. It is now read-only.

Commit 86c184f

Browse files
committed
Clean out some more _cupsStr cruft that might potentially cause an
unaligned memory access (Issue #5474) Don't directly use the string pool in the CGI programs or scheduler.
1 parent e5dfea4 commit 86c184f

4 files changed

Lines changed: 40 additions & 43 deletions

File tree

cgi-bin/admin.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ do_am_class(http_t *http, /* I - HTTP connection */
562562
attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_URI, "member-uris",
563563
num_printers, NULL, NULL);
564564
for (i = 0; i < num_printers; i ++)
565-
attr->values[i].string.text = _cupsStrAlloc(cgiGetArray("MEMBER_URIS", i));
565+
ippSetString(request, &attr, i, cgiGetArray("MEMBER_URIS", i));
566566
}
567567

568568
/*
@@ -2123,7 +2123,7 @@ do_list_printers(http_t *http) /* I - HTTP connection */
21232123
attr;
21242124
attr = ippFindNextAttribute(response, "device-uri", IPP_TAG_URI))
21252125
{
2126-
cupsArrayAdd(printer_devices, _cupsStrAlloc(attr->values[0].string.text));
2126+
cupsArrayAdd(printer_devices, strdup(attr->values[0].string.text));
21272127
}
21282128

21292129
/*
@@ -2261,7 +2261,7 @@ do_list_printers(http_t *http) /* I - HTTP connection */
22612261
for (printer_device = (char *)cupsArrayFirst(printer_devices);
22622262
printer_device;
22632263
printer_device = (char *)cupsArrayNext(printer_devices))
2264-
_cupsStrFree(printer_device);
2264+
free(printer_device);
22652265

22662266
cupsArrayDelete(printer_devices);
22672267
}
@@ -2658,7 +2658,7 @@ do_set_allowed_users(http_t *http) /* I - HTTP connection */
26582658
* Add the name...
26592659
*/
26602660

2661-
attr->values[i].string.text = _cupsStrAlloc(ptr);
2661+
ippSetString(request, &attr, i, ptr);
26622662

26632663
/*
26642664
* Advance to the next name...
@@ -3467,8 +3467,8 @@ do_set_options(http_t *http, /* I - HTTP connection */
34673467

34683468
attr = ippAddStrings(request, IPP_TAG_PRINTER, IPP_TAG_NAME,
34693469
"job-sheets-default", 2, NULL, NULL);
3470-
attr->values[0].string.text = _cupsStrAlloc(cgiGetVariable("job_sheets_start"));
3471-
attr->values[1].string.text = _cupsStrAlloc(cgiGetVariable("job_sheets_end"));
3470+
ippSetString(request, &attr, 0, cgiGetVariable("job_sheets_start"));
3471+
ippSetString(request, &attr, 1, cgiGetVariable("job_sheets_end"));
34723472

34733473
if ((var = cgiGetVariable("printer_error_policy")) != NULL)
34743474
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME,

cgi-bin/var.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
22
* CGI form variable and array functions for CUPS.
33
*
4-
* Copyright 2007-2015 by Apple Inc.
5-
* Copyright 1997-2005 by Easy Software Products.
4+
* Copyright © 2007-2019 by Apple Inc.
5+
* Copyright © 1997-2005 by Easy Software Products.
66
*
77
* Licensed under Apache License v2.0. See the file "LICENSE" for more information.
88
*/
@@ -29,10 +29,10 @@
2929

3030
typedef struct /**** Form variable structure ****/
3131
{
32-
const char *name; /* Name of variable */
32+
char *name; /* Name of variable */
3333
int nvalues, /* Number of values */
3434
avalues; /* Number of values allocated */
35-
const char **values; /* Value(s) of variable */
35+
char **values; /* Value(s) of variable */
3636
} _cgi_var_t;
3737

3838

@@ -135,10 +135,10 @@ cgiClearVariables(void)
135135

136136
for (v = form_vars, i = form_count; i > 0; v ++, i --)
137137
{
138-
_cupsStrFree(v->name);
138+
free(v->name);
139139
for (j = 0; j < v->nvalues; j ++)
140140
if (v->values[j])
141-
_cupsStrFree(v->values[j]);
141+
free(v->values[j]);
142142
}
143143

144144
form_count = 0;
@@ -164,7 +164,7 @@ cgiGetArray(const char *name, /* I - Name of array variable */
164164
if (element < 0 || element >= var->nvalues)
165165
return (NULL);
166166

167-
return (_cupsStrRetain(var->values[element]));
167+
return (strdup(var->values[element]));
168168
}
169169

170170

@@ -222,7 +222,7 @@ cgiGetVariable(const char *name) /* I - Name of variable */
222222

223223
var = cgi_find_variable(name);
224224

225-
return ((var == NULL) ? NULL : _cupsStrRetain(var->values[var->nvalues - 1]));
225+
return ((var == NULL) ? NULL : strdup(var->values[var->nvalues - 1]));
226226
}
227227

228228

@@ -370,10 +370,9 @@ cgiSetArray(const char *name, /* I - Name of variable */
370370
{
371371
if (element >= var->avalues)
372372
{
373-
const char **temp; /* Temporary pointer */
373+
char **temp; /* Temporary pointer */
374374

375-
temp = (const char **)realloc((void *)(var->values),
376-
sizeof(char *) * (size_t)(element + 16));
375+
temp = (char **)realloc((void *)(var->values), sizeof(char *) * (size_t)(element + 16));
377376
if (!temp)
378377
return;
379378

@@ -389,9 +388,9 @@ cgiSetArray(const char *name, /* I - Name of variable */
389388
var->nvalues = element + 1;
390389
}
391390
else if (var->values[element])
392-
_cupsStrFree((char *)var->values[element]);
391+
free((char *)var->values[element]);
393392

394-
var->values[element] = _cupsStrAlloc(value);
393+
var->values[element] = strdup(value);
395394
}
396395
}
397396

@@ -448,10 +447,9 @@ cgiSetSize(const char *name, /* I - Name of variable */
448447

449448
if (size >= var->avalues)
450449
{
451-
const char **temp; /* Temporary pointer */
450+
char **temp; /* Temporary pointer */
452451

453-
temp = (const char **)realloc((void *)(var->values),
454-
sizeof(char *) * (size_t)(size + 16));
452+
temp = (char **)realloc((void *)(var->values), sizeof(char *) * (size_t)(size + 16));
455453
if (!temp)
456454
return;
457455

@@ -468,7 +466,7 @@ cgiSetSize(const char *name, /* I - Name of variable */
468466
{
469467
for (i = size; i < var->nvalues; i ++)
470468
if (var->values[i])
471-
_cupsStrFree((void *)(var->values[i]));
469+
free((void *)(var->values[i]));
472470
}
473471

474472
var->nvalues = size;
@@ -503,9 +501,9 @@ cgiSetVariable(const char *name, /* I - Name of variable */
503501
{
504502
for (i = 0; i < var->nvalues; i ++)
505503
if (var->values[i])
506-
_cupsStrFree((char *)var->values[i]);
504+
free((char *)var->values[i]);
507505

508-
var->values[0] = _cupsStrAlloc(value);
506+
var->values[0] = strdup(value);
509507
var->nvalues = 1;
510508
}
511509
}
@@ -548,10 +546,10 @@ cgi_add_variable(const char *name, /* I - Variable name */
548546
if ((var->values = calloc((size_t)element + 1, sizeof(char *))) == NULL)
549547
return;
550548

551-
var->name = _cupsStrAlloc(name);
549+
var->name = strdup(name);
552550
var->nvalues = element + 1;
553551
var->avalues = element + 1;
554-
var->values[element] = _cupsStrAlloc(value);
552+
var->values[element] = strdup(value);
555553

556554
form_count ++;
557555
}
@@ -583,7 +581,7 @@ cgi_find_variable(const char *name) /* I - Name of variable */
583581
if (form_count < 1 || name == NULL)
584582
return (NULL);
585583

586-
key.name = name;
584+
key.name = (char *)name;
587585

588586
return ((_cgi_var_t *)bsearch(&key, form_vars, (size_t)form_count, sizeof(_cgi_var_t),
589587
(int (*)(const void *, const void *))cgi_compare_variables));

cups/string.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/*
22
* String functions for CUPS.
33
*
4-
* Copyright 2007-2014 by Apple Inc.
5-
* Copyright 1997-2007 by Easy Software Products.
4+
* Copyright © 2007-2019 by Apple Inc.
5+
* Copyright © 1997-2007 by Easy Software Products.
66
*
7-
* Licensed under Apache License v2.0. See the file "LICENSE" for more information.
7+
* Licensed under Apache License v2.0. See the file "LICENSE" for more
8+
* information.
89
*/
910

1011
/*
@@ -311,22 +312,21 @@ _cupsStrFree(const char *s) /* I - String to free */
311312

312313
key = (_cups_sp_item_t *)(s - offsetof(_cups_sp_item_t, str));
313314

314-
#ifdef DEBUG_GUARDS
315-
if (key->guard != _CUPS_STR_GUARD)
316-
{
317-
DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, "
318-
"ref_count=%d", key, key->str, key->guard, key->ref_count));
319-
abort();
320-
}
321-
#endif /* DEBUG_GUARDS */
322-
323315
if ((item = (_cups_sp_item_t *)cupsArrayFind(stringpool, key)) != NULL &&
324316
item == key)
325317
{
326318
/*
327319
* Found it, dereference...
328320
*/
329321

322+
#ifdef DEBUG_GUARDS
323+
if (key->guard != _CUPS_STR_GUARD)
324+
{
325+
DEBUG_printf(("5_cupsStrFree: Freeing string %p(%s), guard=%08x, ref_count=%d", key, key->str, key->guard, key->ref_count));
326+
abort();
327+
}
328+
#endif /* DEBUG_GUARDS */
329+
330330
item->ref_count --;
331331

332332
if (!item->ref_count)

scheduler/ipp.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2601,8 +2601,7 @@ add_printer(cupsd_client_t *con, /* I - Client connection */
26012601
if (!strcmp(attr->values[i].string.text, "none"))
26022602
continue;
26032603

2604-
printer->reasons[printer->num_reasons] =
2605-
_cupsStrRetain(attr->values[i].string.text);
2604+
printer->reasons[printer->num_reasons] = _cupsStrAlloc(attr->values[i].string.text);
26062605
printer->num_reasons ++;
26072606

26082607
if (!strcmp(attr->values[i].string.text, "paused") &&
@@ -4892,7 +4891,7 @@ copy_printer_attrs(
48924891

48934892
if ((p2_uri = ippFindAttribute(p2->attrs, "printer-uri-supported", IPP_TAG_URI)) != NULL)
48944893
{
4895-
member_uris->values[i].string.text = _cupsStrRetain(p2_uri->values[0].string.text);
4894+
member_uris->values[i].string.text = _cupsStrAlloc(p2_uri->values[0].string.text);
48964895
}
48974896
else
48984897
{

0 commit comments

Comments
 (0)