Skip to content

Commit cdbc627

Browse files
committed
Return false if we have an unterminated quoted string token, and do a better job
validating "$ENV[name]" and "${name}" expansions Also update the documentation comments.
1 parent d7fcb33 commit cdbc627

1 file changed

Lines changed: 39 additions & 32 deletions

File tree

cups/ipp-file.c

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,13 @@ ippFileDelete(ipp_file_t *file) // I - IPP data file
112112
//
113113
// 'ippFileExpandVars()' - Expand IPP data file and environment variables in a string.
114114
//
115-
// This function expands IPP data file variables of the form "$name" and
116-
// environment variables of the form "$ENV[name]" in the source string to the
117-
// destination string. The
115+
// This function expands IPP data file variables in "src" of the form "$name" or
116+
// "${name}" and environment variables of the form "$ENV[name]" in the source
117+
// string to the destination buffer. The destination buffer "dst" is filled up
118+
// to "dstsize - 1" bytes to allow for a trailing NUL character.
119+
//
120+
// The return value is the actual required size for all expansions regardless of
121+
// the value of "dstsize".
118122
//
119123

120124
size_t // O - Required size for expanded variables
@@ -154,49 +158,50 @@ ippFileExpandVars(ipp_file_t *file, // I - IPP data file
154158
}
155159
else if (!strncmp(src, "$ENV[", 5))
156160
{
157-
// Environment variable
158-
cupsCopyString(temp, src + 5, sizeof(temp));
159-
160-
for (tempptr = temp; *tempptr; tempptr ++)
161+
// $ENV[name] - environment variable
162+
for (src += 5, tempptr = temp; *src && *src != ']'; src ++)
161163
{
162-
if (*tempptr == ']')
163-
break;
164+
// Copy the name to "temp"...
165+
if (tempptr < (temp + sizeof(temp) - 1))
166+
*tempptr++ = *src;
164167
}
168+
*tempptr = '\0';
165169

166-
if (*tempptr)
167-
*tempptr++ = '\0';
170+
if (*src) // Skip "]"
171+
src ++;
168172

169173
value = getenv(temp);
170-
src += tempptr - temp + 5;
171174
}
172175
else
173176
{
174-
// $name or ${name}
177+
// $name or ${name} - file variable
175178
if (src[1] == '{')
176179
{
177-
src += 2;
178-
cupsCopyString(temp, src, sizeof(temp));
179-
if ((tempptr = strchr(temp, '}')) != NULL)
180-
*tempptr = '\0';
181-
else
182-
tempptr = temp + strlen(temp);
180+
// ${name}
181+
for (src += 2, tempptr = temp; *src && *src != '}'; src ++)
182+
{
183+
// Copy the name to "temp"...
184+
if (tempptr < (temp + sizeof(temp) - 1))
185+
*tempptr++ = *src;
186+
}
187+
*tempptr = '\0';
188+
189+
if (*src) // Skip "}"
190+
src ++;
183191
}
184192
else
185193
{
186-
cupsCopyString(temp, src + 1, sizeof(temp));
187-
188-
for (tempptr = temp; *tempptr; tempptr ++)
194+
// $name
195+
for (src ++, tempptr = temp; isalnum(*src & 255) || *src == '-' || *src == '_'; src ++)
189196
{
190-
if (!isalnum(*tempptr & 255) && *tempptr != '-' && *tempptr != '_')
191-
break;
197+
// Copy the name to "temp"...
198+
if (tempptr < (temp + sizeof(temp) - 1))
199+
*tempptr++ = *src;
192200
}
193-
194-
if (*tempptr)
195-
*tempptr = '\0';
201+
*tempptr = '\0';
196202
}
197203

198204
value = ippFileGetVar(file, temp);
199-
src += tempptr - temp + 1;
200205
}
201206

202207
if (value)
@@ -207,9 +212,13 @@ ippFileExpandVars(ipp_file_t *file, // I - IPP data file
207212
}
208213
}
209214
else if (dstptr < dstend)
215+
{
210216
*dstptr++ = *src++;
217+
}
211218
else
219+
{
212220
dstptr ++;
221+
}
213222
}
214223

215224
if (dstptr < dstend)
@@ -225,9 +234,7 @@ ippFileExpandVars(ipp_file_t *file, // I - IPP data file
225234
// 'ippFileGetAttribute()' - Get a single named attribute from an IPP data file.
226235
//
227236
// This function finds the first occurence of a named attribute in the current
228-
// IPP attributes in the specified data file. Unlike
229-
// @link ippFileGetAttributes@, this function does not clear the attribute
230-
// state.
237+
// IPP attributes in the specified data file.
231238
//
232239

233240
ipp_attribute_t * // O - Attribute or `NULL` if none
@@ -938,7 +945,7 @@ ippFileReadToken(ipp_file_t *file, // I - IPP data file
938945
*tokptr = '\0';
939946
DEBUG_printf("1ippFileReadToken: Returning \"%s\" at EOF.", token);
940947

941-
return (tokptr > token);
948+
return (tokptr > token && !quote);
942949
}
943950

944951

0 commit comments

Comments
 (0)