2012-05-03 18:59:14 +00:00
|
|
|
/*
|
|
|
|
* argvsplit.c
|
|
|
|
* argv_split() routine
|
|
|
|
*
|
2017-02-25 21:04:08 +00:00
|
|
|
* Copyright (c) 2012, 2017 pkgconf authors (see AUTHORS).
|
2012-05-03 18:59:14 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2012-07-20 19:29:58 +00:00
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
2012-05-03 18:59:14 +00:00
|
|
|
*/
|
|
|
|
|
2017-09-18 04:38:25 +00:00
|
|
|
#include <libpkgconf/stdinc.h>
|
2015-09-06 14:35:08 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2012-05-03 18:59:14 +00:00
|
|
|
|
2016-12-11 00:56:09 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* libpkgconf `argvsplit` module
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* This is a lowlevel module which provides parsing of strings into argument vectors,
|
|
|
|
* similar to what a shell would do.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_argv_free(char **argv)
|
|
|
|
*
|
|
|
|
* Frees an argument vector.
|
|
|
|
*
|
|
|
|
* :param char** argv: The argument vector to free.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
2012-05-07 03:21:11 +00:00
|
|
|
void
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_argv_free(char **argv)
|
2012-05-07 02:09:40 +00:00
|
|
|
{
|
|
|
|
free(argv[0]);
|
|
|
|
free(argv);
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:56:09 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: int pkgconf_argv_split(const char *src, int *argc, char ***argv)
|
|
|
|
*
|
|
|
|
* Splits a string into an argument vector.
|
|
|
|
*
|
|
|
|
* :param char* src: The string to split.
|
|
|
|
* :param int* argc: A pointer to an integer to store the argument count.
|
|
|
|
* :param char*** argv: A pointer to a pointer for an argument vector.
|
|
|
|
* :return: 0 on success, -1 on error.
|
|
|
|
* :rtype: int
|
|
|
|
*/
|
2012-05-07 03:21:11 +00:00
|
|
|
int
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_argv_split(const char *src, int *argc, char ***argv)
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
|
|
|
char *buf = malloc(strlen(src) + 1);
|
|
|
|
const char *src_iter;
|
|
|
|
char *dst_iter;
|
|
|
|
int argc_count = 0;
|
|
|
|
int argv_size = 5;
|
|
|
|
char quote = 0;
|
2017-02-25 21:04:08 +00:00
|
|
|
bool escaped = false;
|
2012-05-03 18:59:14 +00:00
|
|
|
|
|
|
|
src_iter = src;
|
|
|
|
dst_iter = buf;
|
|
|
|
|
2012-05-07 08:26:44 +00:00
|
|
|
memset(buf, 0, strlen(src) + 1);
|
2012-05-03 18:59:14 +00:00
|
|
|
|
|
|
|
*argv = calloc(sizeof (void *), argv_size);
|
|
|
|
(*argv)[argc_count] = dst_iter;
|
|
|
|
|
|
|
|
while (*src_iter)
|
|
|
|
{
|
2017-02-25 21:04:08 +00:00
|
|
|
if (escaped)
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
2017-02-25 21:04:08 +00:00
|
|
|
/* POSIX: only \CHAR is special inside a double quote if CHAR is {$, `, ", \, newline}. */
|
2023-01-21 19:51:24 +00:00
|
|
|
if (quote == '"')
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
2017-02-25 21:04:08 +00:00
|
|
|
if (!(*src_iter == '$' || *src_iter == '`' || *src_iter == '"' || *src_iter == '\\'))
|
2012-05-03 18:59:14 +00:00
|
|
|
*dst_iter++ = '\\';
|
2017-02-25 21:04:08 +00:00
|
|
|
|
|
|
|
*dst_iter++ = *src_iter;
|
|
|
|
}
|
|
|
|
else
|
2022-11-22 20:29:09 +00:00
|
|
|
{
|
|
|
|
*dst_iter++ = *src_iter;
|
|
|
|
}
|
2023-01-21 19:51:24 +00:00
|
|
|
|
2017-02-25 21:04:08 +00:00
|
|
|
escaped = false;
|
|
|
|
}
|
|
|
|
else if (quote)
|
|
|
|
{
|
|
|
|
if (*src_iter == quote)
|
|
|
|
quote = 0;
|
2017-12-15 04:41:14 +00:00
|
|
|
else if (*src_iter == '\\' && quote != '\'')
|
2017-02-25 21:04:08 +00:00
|
|
|
escaped = true;
|
|
|
|
else
|
|
|
|
*dst_iter++ = *src_iter;
|
2012-05-03 18:59:14 +00:00
|
|
|
}
|
Avoid undefined behaviour with the ctype(3) functions.
fix https://github.com/pkgconf/pkgconf/issues/291
As defined in the C standard:
In all cases the argument is an int, the value of which shall
be representable as an unsigned char or shall equal the value
of the macro EOF. If the argument has any other value, the
behavior is undefined.
This is because they're designed to work with the int values returned
by getc or fgetc; they need extra work to handle a char value.
If EOF is -1 (as it almost always is), with 8-bit bytes, the allowed
inputs to the ctype(3) functions are:
{-1, 0, 1, 2, 3, ..., 255}.
However, on platforms where char is signed, such as x86 with the
usual ABI, code like
char *ptr = ...;
... isspace(*ptr) ...
may pass in values in the range:
{-128, -127, -126, ..., -2, -1, 0, 1, ..., 127}.
This has two problems:
1. Inputs in the set {-128, -127, -126, ..., -2} are forbidden.
2. The non-EOF byte 0xff is conflated with the value EOF = -1, so
even though the input is not forbidden, it may give the wrong
answer.
Casting char to unsigned int first before passing the result to
ctype(3) doesn't help: inputs like -128 are unchanged by this cast,
because (on a two's-complement machine with 32-bit int and unsigned
int), converting the signed char with integer value -128 to unsigned
int gives integer value 2^32 - 128 = 0xffffff80, which is out of
range, and which is converted in int back to -128, which is also out
of range.
It is necessary to cast char inputs to unsigned char first; you can
then cast to unsigned int if you like but there's no need because the
functions will always convert the argument to int by definition. So
the above fragment needs to be:
char *ptr = ...;
... isspace((unsigned char)*ptr) ...
This patch changes unsigned int casts to unsigned char casts, and
adds unsigned char casts where they are missing.
2023-03-17 19:32:58 +00:00
|
|
|
else if (isspace((unsigned char)*src_iter))
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
2012-07-25 22:49:54 +00:00
|
|
|
if ((*argv)[argc_count] != NULL)
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
|
|
|
argc_count++, dst_iter++;
|
|
|
|
|
|
|
|
if (argc_count == argv_size)
|
|
|
|
{
|
|
|
|
argv_size += 5;
|
|
|
|
*argv = realloc(*argv, sizeof(void *) * argv_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*argv)[argc_count] = dst_iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else switch(*src_iter)
|
|
|
|
{
|
2017-02-25 21:04:08 +00:00
|
|
|
case '\\':
|
|
|
|
escaped = true;
|
2017-02-07 16:24:54 +00:00
|
|
|
break;
|
|
|
|
|
2017-02-25 21:04:08 +00:00
|
|
|
case '\"':
|
2017-02-07 16:24:54 +00:00
|
|
|
case '\'':
|
2017-02-25 21:04:08 +00:00
|
|
|
quote = *src_iter;
|
2017-02-07 16:24:54 +00:00
|
|
|
break;
|
|
|
|
|
2012-05-03 18:59:14 +00:00
|
|
|
default:
|
|
|
|
*dst_iter++ = *src_iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
src_iter++;
|
|
|
|
}
|
|
|
|
|
2017-02-25 21:04:08 +00:00
|
|
|
if (escaped || quote)
|
|
|
|
{
|
|
|
|
free(*argv);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen((*argv)[argc_count]))
|
|
|
|
{
|
2012-05-03 18:59:14 +00:00
|
|
|
argc_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*argc = argc_count;
|
|
|
|
return 0;
|
|
|
|
}
|