Lobste.rs

POSIX wordexp(3) is dangerous

Denmark Ends Open Source Office Pilot in Silence | LavX News

wordexp(3) is a POSIX API for performing shell-style expansions from C. It’s often cited/recommended as a way to do tilde expansion (e.g. ~/foo to /home/me/foo): #include <stdio.h> #include <stdlib.h> #include <string.h> #include <wordexp.h> int expand_tilde(const char *input, char **output) { wordexp_t p; int ret = wordexp(input, &p, 0); if (ret == 0) { *output = strdup(p.we_wordv[0]); wordfree(&p); } return ret; } int main(void) { char *expanded = NULL; expand_tilde(“~/foo”, &expanded); printf(“expanded: %sn”, expanded); return 0; } However, wordexp(3) is also dangerous! That last 0 argument is for the flags parameter. By default, all expansion features are enabled, including command expansion: expand_tilde(“$(whoami)/foo”, &expanded); or:…

Continue reading at Lobste.rs yossarian.net
Advertisement