Skip to content

Commit

Permalink
Merge branch 'fix-variables'
Browse files Browse the repository at this point in the history
* fix-variables:
  simplify code and thread-safety
  fix crash with variable SEQUENCE[n,m]
  • Loading branch information
TurboGit committed Jan 3, 2025
2 parents 09cea4a + cdb6e61 commit e932ddb
Showing 1 changed file with 60 additions and 79 deletions.
139 changes: 60 additions & 79 deletions src/common/variables.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,77 +285,52 @@ static char *_variables_get_latitude(dt_variables_params_t *params)
}
}

static int _get_parameters(char **variable, char **parameters, size_t max_param)
/*
* @param char **variable - The variable to read parameters from
* @param char **parameters - a pointer to where list of parameters are stored in
* @param size_t max_param - The maximum number of parameters to read
* (and parameters has space for pointers)
* @return The number of parameters, -1 if any error
*
* You should free the string in parameters with g_free() after usage unless
* returnvalue is 0 or -1;
/**
* get n,m from a variable with [n,m], i.e. $(SEQUENCE[4,1])
* param_m may be NULL if only n is needed, i.e. $(IMAGE_ID[4])
*/
static void _get_parameters_n_m(char **variable, gchar **param_n, gchar **param_m)
{
*parameters = NULL;
if(*variable[0] == '[')
{
(*variable) ++;
if(*variable[0] == ',')
{
return -1;
}
*parameters = g_strdup(*variable);
char *end = g_strstr_len(*parameters, -1, "]");
if(end)
{
end[0] = '\0';
(*variable) += strlen(*parameters) + 1;

size_t count = 0;
char *token = strtok(*parameters, ",");
while (token != NULL && count < max_param)
{
*parameters = token;
parameters++;
count++;
token = strtok(NULL, ",");
}
return count;
}
}
return -1;
}

static gboolean _is_number(char *str)
{
if(*str == '-' || *str == '+')
str++;
*param_n = NULL;
if(param_m) *param_m = NULL;

if(!g_ascii_isdigit(*str))
return FALSE; // don't take empty strings
if(*variable[0] != '[') return;
(*variable)++;
if(*variable[0] == ',') return;

while(*str)
gchar *parameters = g_strdup(*variable);
char *end = g_strstr_len(parameters, -1, "]");
if(end)
{
if(!g_ascii_isdigit(*str))
return FALSE;
str++;
end[0] = '\0';
(*variable) += strlen(parameters) + 1;

gchar **res = g_strsplit(parameters, ",", 2);
gchar **p = res;
int n = 0;
while(*p && n < 2)
{
gchar **target = (n == 0)? param_n : param_m;
if(target) *target = g_strdup(*p);
p++;
n++;
}
g_strfreev(res);
}
return TRUE;
g_free(parameters);
}

static uint8_t _get_var_parameter(char **variable, const int default_value)
{
uint8_t val = default_value;
if(*variable[0] == '[')
{
char *parameters[1] = { NULL };
const int num = _get_parameters(variable, parameters, 1);
if(num == 1 && _is_number(parameters[0]))
{
val = (uint8_t)strtol(parameters[0], NULL, 10);
}
g_free(parameters[0]);
gchar *val_s;
_get_parameters_n_m(variable, &val_s, NULL);
int n = g_ascii_strtoll(val_s, NULL, 10);
if(n > 0)
val = n;
g_free(val_s);
}
return val;
}
Expand Down Expand Up @@ -702,26 +677,30 @@ static char *_get_base_value(dt_variables_params_t *params, char **variable)
if(g_ascii_isdigit(*variable[0]))
{
nb_digit = (uint8_t)*variable[0] & 0b1111;
(*variable) ++;
(*variable)++;
}
// new $(SEQUENCE[n,m]) syntax
// allows \[[0-9]+,[0-9]+] (PCRE)
// everything else will be ignored
else if(*variable[0] == '[')
{
char *parameters[2] = {NULL};
const int num = _get_parameters(variable, parameters, 2);
if(num >= 1 && _is_number(parameters[0]))
gchar *nb_digit_s, *shift_s;
_get_parameters_n_m(variable, &nb_digit_s, &shift_s);

if(nb_digit_s)
{
nb_digit = (uint8_t) strtol(parameters[0], NULL, 10);
if(num == 2 && _is_number(parameters[1]))
{
shift = (gint) strtol(parameters[1], NULL, 10);
}
int nb = g_ascii_strtoll(nb_digit_s, NULL, 10);
if(nb > 0) nb_digit = nb;
}

if(shift_s)
{
int nb = g_ascii_strtoll(shift_s, NULL, 10);
if(nb > 0) shift = nb;
}
if(num == 2)
g_free(parameters[1]);
g_free(parameters[0]);

g_free(nb_digit_s);
g_free(shift_s);
}
result = g_strdup_printf("%.*u", nb_digit,
params->sequence >= 0
Expand Down Expand Up @@ -923,7 +902,7 @@ static char *_get_base_value(dt_variables_params_t *params, char **variable)
if(g_ascii_isdigit(*variable[0]))
{
const uint8_t level = (uint8_t)*variable[0] & 0b1111;
(*variable) ++;
(*variable)++;
if(*variable[0] == '(')
{
char *category = g_strdup(*variable + 1);
Expand All @@ -947,21 +926,23 @@ static char *_get_base_value(dt_variables_params_t *params, char **variable)
// $(CATEGORY[n,category]) with category as above (new syntax)
else if(*variable[0] == '[')
{
char *parameters[2] = {NULL};
const int num = _get_parameters(variable, parameters, 2);
if(num == 2 && g_ascii_isdigit(*parameters[0]))
gchar *level_s, *category;
_get_parameters_n_m(variable, &level_s, &category);

if(level_s && category && g_ascii_isdigit(*level_s))
{
const uint8_t level = (uint8_t)*parameters[0] & 0b1111;
parameters[1][strlen(parameters[1]) + 1] = '\0';
parameters[1][strlen(parameters[1])] = '|';
char *tag = dt_tag_get_subtags(params->imgid, parameters[1], (int)level);
const uint8_t level = (uint8_t)*level_s & 0b1111;
gchar *cat = g_strdup_printf("%s|", category);
char *tag = dt_tag_get_subtags(params->imgid, cat, (int)level);
g_free(cat);
if(tag)
{
result = g_strdup(tag);
g_free(tag);
}
}
g_free(parameters[0]);
g_free(level_s);
g_free(category);
}
}
else if(_has_prefix(variable, "IMAGE.TAGS.HIERARCHY"))
Expand Down

0 comments on commit e932ddb

Please sign in to comment.