Open GoogleCodeExporter opened 9 years ago
I've modified the validation process of boolean and unsigned integers
int evcpe_type_validate(enum evcpe_type type, const char *value, unsigned len,
struct evcpe_constraint *cons)
{
int rc;
long val;
unsigned long uval;
char *dup;
struct tm tm;
evcpe_debug(__func__, "validating value of type: %s",
evcpe_type_to_str(type));
switch(type) {
case EVCPE_TYPE_STRING:
break;
case EVCPE_TYPE_BASE64:
// TODO
break;
case EVCPE_TYPE_BOOLEAN:
if (evcpe_strcmp(value, len, "true", 4) && evcpe_strcmp(value, len, "false", 5) &&
evcpe_strcmp(value, len, "0", 1) && evcpe_strcmp(value, len, "1", 1)) {
evcpe_error(__func__, "failed to convert to boolean: %.*s", len, value);
goto finally;
}
break;
case EVCPE_TYPE_INT:
if ((rc = evcpe_atol(value, len, &val))) {
evcpe_error(__func__, "failed to convert to "
"integer: %.*s", len, value);
goto finally;
}
switch(cons->type) {
case EVCPE_CONSTRAINT_NONE:
break;
case EVCPE_CONSTRAINT_MIN:
case EVCPE_CONSTRAINT_MAX:
case EVCPE_CONSTRAINT_RANGE:
if (cons->type != EVCPE_CONSTRAINT_MAX &&
val < cons->value.range.min) {
evcpe_error(__func__, "value out of range: %ld < %ld",
val, cons->value.range.min);
rc = EINVAL;
goto finally;
}
if (cons->type != EVCPE_CONSTRAINT_MIN &&
val > cons->value.range.max) {
evcpe_error(__func__, "value out of range: %ld > %ld",
val, cons->value.range.max);
rc = EINVAL;
goto finally;
}
break;
default:
evcpe_error(__func__, "unexpected constraint type: %d", cons->type);
rc = EINVAL;
goto finally;
}
break;
case EVCPE_TYPE_UNSIGNED_INT:
if ((rc = evcpe_atoul(value, len, &uval))) {
evcpe_error(__func__, "failed to convert to "
"unsigned integer: %.*s", len, value);
goto finally;
}
switch(cons->type) {
case EVCPE_CONSTRAINT_NONE:
break;
case EVCPE_CONSTRAINT_MIN:
case EVCPE_CONSTRAINT_MAX:
case EVCPE_CONSTRAINT_RANGE:
if (cons->type != EVCPE_CONSTRAINT_MAX &&
uval < cons->value.range.min) {
evcpe_error(__func__, "value out of range: %ld < %ld",
uval, cons->value.range.min);
rc = EINVAL;
goto finally;
}
if (cons->type != EVCPE_CONSTRAINT_MIN &&
uval > cons->value.range.max) {
evcpe_error(__func__, "value out of range: %ld > %ld",
val, cons->value.range.max);
rc = EINVAL;
goto finally;
}
break;
default:
evcpe_error(__func__, "unexpected constraint type: %d", cons->type);
rc = EINVAL;
goto finally;
}
break;
case EVCPE_TYPE_DATETIME:
if (!(dup = malloc(len + 1))) {
evcpe_error(__func__, "failed to malloc: %d bytes", len + 1);
rc = ENOMEM;
goto finally;
}
memcpy(dup, value, len);
dup[len] = '\0';
if (!strptime(dup, "%Y-%m-%dT%H:%M:%S", &tm)
&& !strptime(dup, "%Y-%m-%dT%H:%M:%S%z", &tm)) {
evcpe_error(__func__, "failed to parse dateTime: %s", dup);
free(dup);
rc = EINVAL;
goto finally;
}
free(dup);
break;
case EVCPE_TYPE_UNKNOWN:
case EVCPE_TYPE_OBJECT:
case EVCPE_TYPE_MULTIPLE:
default:
evcpe_error(__func__, "value is not applicable to "
"type: %d", type);
rc = EINVAL;
goto finally;
}
rc = 0;
finally:
return rc;
}
Original comment by atef.hal...@gmail.com
on 22 Sep 2011 at 8:36
Original issue reported on code.google.com by
atef.hal...@gmail.com
on 29 Aug 2011 at 1:18