Permalink
Browse files

refactor sanitize_var

be more strict about what we accept
  • Loading branch information...
1 parent a1ee8f3 commit 4f588b723d1591d431ae805e95e7354515ddd988 @falconindy committed Apr 20, 2011
Showing with 18 additions and 10 deletions.
  1. +18 −10 init.c
View
28 init.c
@@ -90,23 +90,31 @@ static char *concat_path(const char *path, const char *filename) { /* {{{ */
static char *sanitize_var(char *var) { /* {{{ */
char *p;
+ /* special attention to first letter */
p = var;
if (!(isalpha(*p) || *p == '_')) {
/* invalid var name, can't use this */
return NULL;
}
- p++;
- while (*p) {
- switch (*p) {
- case '-': /* fallthrough */
- case '.':
- *p = '_';
- break;
- case '=': /* don't touch anything past this */
- return var;
+ while (*++p) {
+ if (isalnum((unsigned char)*p) || *p == '_') {
+ /* valid character */
+ continue;
+ }
+
+ if (*p == '=') {
+ /* stop here, don't mangle the values */
+ return var;
+ }
+
+ if (*p == '.' || *p == '-') {
+ /* sanitizable */
+ *p = '_';
+ } else {
+ /* gfy */
+ return NULL;
}
- p++;
}
return var;

0 comments on commit 4f588b7

Please sign in to comment.