/
githubmirror
/
sudo
Обзор
Документация
Войти
/
githubmirror
/
sudo
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
plugins/sudoers/rationalize.c
60 строк
2 KB
Todd C. Miller
find_path: collapse extra "/" and "./" components in the command path
18 мар 2026, 15:54
18 мар 2026, 15:54
4b3a5e7
Код
Авторство
О чём код?
/* * SPDX-License-Identifier: ISC * * Copyright (c) 2026 Todd C. Miller <Todd.Miller@sudo.ws> * * Permission to use, copy, modify, and 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. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include <config.h> #include <string.h> #include <sudoers.h> /* * Rationalize a path by removing consecutive '/' and "/./" path elements. */ char * rationalize_path(char *path) { char *cp, *ep, *path_end; debug_decl(rationalize_path, SUDOERS_DEBUG_UTIL); path_end = path + strlen(path); for (cp = path; *cp != '\0';) { if (cp[0] == '/') { /* Collapse consecutive '/' characters. */ if (cp[1] == '/') { for (ep = cp + 1; ep[1] == '/'; ep++) continue; /* The size argument includes the terminating NUL byte. */ memmove(cp, ep, (size_t)(path_end - ep) + 1); continue; } /* Remove "/./" in path or "/." at path_end. */ if (cp[1] == '.' && (cp[2] == '/' || cp[2] == '\0')) { /* /./foo -> /foo OR /foo/. -> /foo */ ep = cp + 2; /* The size argument includes the terminating NUL byte. */ memmove(cp, ep, (size_t)(path_end - ep) + 1); continue; } } cp++; } debug_return_str(path); }