@@ -1241,7 +1241,11 @@ vips__token_get(const char *p, VipsToken *token, char *string, int size)
1241
1241
const char * q ;
1242
1242
int ch ;
1243
1243
int n ;
1244
- int i ;
1244
+
1245
+ /* string return defaults to "".
1246
+ */
1247
+ if (size > 0 )
1248
+ string [0 ] = '\0' ;
1245
1249
1246
1250
/* Parse this token with p.
1247
1251
*/
@@ -1277,61 +1281,57 @@ vips__token_get(const char *p, VipsToken *token, char *string, int size)
1277
1281
1278
1282
case '"' :
1279
1283
case '\'' :
1280
- /* Parse a quoted string. Copy up to ", interpret any \",
1281
- * error if no closing ".
1284
+ /* Parse a quoted string. Copy up to " or end of string, interpret
1285
+ * any \",
1282
1286
*/
1283
1287
* token = VIPS_TOKEN_STRING ;
1284
1288
1285
1289
do {
1286
- /* Number of characters until the next quote
1287
- * character or end of string.
1290
+ /* Move q to the next matching quote, or the end of the string.
1288
1291
*/
1289
- if ((q = strchr (p + 1 , ch )))
1290
- n = q - p + 1 ;
1291
- else
1292
- n = strlen (p + 1 );
1292
+ if (!(q = strchr (p + 1 , ch )))
1293
+ q = p + strlen (p );
1293
1294
1294
- /* How much can we copy to the buffer?
1295
- */
1296
- i = VIPS_MIN (n , size );
1297
- vips_strncpy (string , p + 1 , i );
1295
+ // number of characters we copy to the output
1296
+ n = VIPS_MIN (q - p - 1 , size - 1 );
1297
+ vips_strncpy (string , p + 1 , n + 1 );
1298
1298
1299
1299
/* We might have stopped at an escaped quote. If the
1300
- * string was not truncated, swap the preceding
1301
- * backslash for a quote.
1300
+ * char before the end is a backslash, swap it for a quote.
1302
1301
*/
1303
- if (p [ n + 1 ] == ch && p [ n ] == '\\' && i == n )
1304
- string [i - 1 ] = ch ;
1302
+ if (q [ - 1 ] == '\\' )
1303
+ string [n - 1 ] = ch ;
1305
1304
1306
- string += i ;
1307
- size -= i ;
1308
- p += n + 1 ;
1305
+ string += n ;
1306
+ size -= n ;
1307
+ p = q ;
1309
1308
} while (p [0 ] && p [-1 ] == '\\' );
1310
1309
1311
- p += 1 ;
1310
+ // step over the terminating quote, if we hit one
1311
+ if (p [0 ] == ch )
1312
+ p += 1 ;
1312
1313
1313
1314
break ;
1314
1315
1315
1316
default :
1316
- /* It's an unquoted string: read up to the next non-string
1317
- * character. We don't allow two strings next to each other,
1318
- * so the next break must be brackets, equals, comma.
1317
+ /* It's an unquoted string: read up to the next non-string character.
1318
+ * We don't allow two strings next to each other, so the next break
1319
+ * must be brackets, equals, comma.
1319
1320
*/
1320
1321
* token = VIPS_TOKEN_STRING ;
1321
1322
q = p + strcspn (p , "[]=," );
1322
1323
1323
- i = VIPS_MIN (q - p , size );
1324
- vips_strncpy (string , p , i + 1 );
1324
+ n = VIPS_MIN (q - p , size );
1325
+ vips_strncpy (string , p , n + 1 );
1325
1326
p = q ;
1326
1327
1327
- /* We remove leading whitespace, so we trim trailing
1328
- * whitespace from unquoted strings too. Only if the string
1329
- * hasn't been truncated.
1328
+ /* We remove leading whitespace, so we trim trailing whitespace from
1329
+ * unquoted strings too. Only if the string hasn't been truncated.
1330
1330
*/
1331
- if (i != size )
1332
- while (i > 0 && isspace (string [i - 1 ])) {
1333
- string [i - 1 ] = '\0' ;
1334
- i -- ;
1331
+ if (n != size )
1332
+ while (n > 0 && isspace (string [n - 1 ])) {
1333
+ string [n - 1 ] = '\0' ;
1334
+ n -- ;
1335
1335
}
1336
1336
1337
1337
break ;
0 commit comments