replace strlcpy and strlcat by snprintf to reduce calls

This commit is contained in:
prx 2022-08-20 15:10:05 +02:00
parent 3ca16cf38f
commit 46f2c7238a
4 changed files with 33 additions and 49 deletions

11
main.c
View File

@ -22,20 +22,19 @@ main(int argc, char **argv)
while ((option = getopt(argc, argv, ":d:l:m:u:c:vi")) != -1) { while ((option = getopt(argc, argv, ":d:l:m:u:c:vi")) != -1) {
switch (option) { switch (option) {
case 'd': case 'd':
estrlcpy(chroot_dir, optarg, sizeof(chroot_dir)); esnprintf(chroot_dir, sizeof(chroot_dir), "%s", optarg);
break; break;
case 'l': case 'l':
estrlcpy(lang, "lang=", sizeof(lang)); esnprintf(lang, sizeof(lang), "lang=%s", optarg);
estrlcat(lang, optarg, sizeof(lang));
break; break;
case 'm': case 'm':
estrlcpy(default_mime, optarg, sizeof(default_mime)); esnprintf(default_mime, sizeof(default_mime), "%s", optarg);
break; break;
case 'u': case 'u':
estrlcpy(user, optarg, sizeof(user)); esnprintf(user, sizeof(user), "%s", optarg);
break; break;
case 'c': case 'c':
estrlcpy(cgi_dir, optarg, sizeof(cgi_dir)); esnprintf(cgi_dir, sizeof(cgi_dir), "%s", optarg);
break; break;
case 'v': case 'v':
virtualhost = 1; virtualhost = 1;

30
utils.c
View File

@ -45,29 +45,21 @@ epledge(const char *promises, const char *execpromises)
#endif #endif
size_t size_t
estrlcpy(char *dst, const char *src, size_t dstsize) esnprintf(char *str, size_t size, const char *format, ...)
{ {
size_t n = 0; /* usage : esnprintf(str, sizeof(str), "%s ... %s", arg1, arg2); */
va_list ap;
size_t ret = 0;
n = strlcpy(dst, src, dstsize); va_start(ap, format);
if (n >= dstsize) { ret = vsnprintf(str, size, format, ap);
status(41, "strlcpy failed, see logs"); va_end(ap);
stop(EXIT_FAILURE, "strlcpy() failed for %s = %s", dst, src); if (ret < 0 || ret >= size) {
status(41, "vnsprintf failed: Output trunkated");
stop(EXIT_FAILURE, "vsnprintf: Output trunkated");
} }
return n; return ret;
}
size_t
estrlcat(char *dst, const char *src, size_t dstsize)
{
size_t size;
if ((size = strlcat(dst, src, dstsize)) >= dstsize) {
status(41, "strlcat() failed, see logs");
stop(EXIT_FAILURE, "strlcat on %s + %s", dst, src);
}
return size;
} }
int int

View File

@ -1,9 +1,8 @@
void getsubexp(const char *, regmatch_t, char *); void getsubexp(const char *, regmatch_t, char *);
void echdir (const char *); void echdir(const char *);
void epledge(const char *, const char *); void epledge(const char *, const char *);
void eunveil(const char *, const char *); void eunveil(const char *, const char *);
int esetenv(const char *, const char *, int); int esetenv(const char *, const char *, int);
size_t estrlcat(char *, const char *, size_t); size_t esnprintf(char *, size_t, const char *, ...);
size_t estrlcpy(char *, const char *, size_t);
size_t print_file(FILE *fd); size_t print_file(FILE *fd);
void set_errmsg(const char *, ...); void set_errmsg(const char *, ...);

36
vger.c
View File

@ -222,10 +222,10 @@ do_cgi(const char *chroot_dir, const char *cgi_dir, const char *path, const char
* cgi_dir + strlen(chrootdir) (skip chrootdir) * cgi_dir + strlen(chrootdir) (skip chrootdir)
*/ */
estrlcpy(cgirp, cgi_dir + strlen(chroot_dir), sizeof(cgirp)); esnprintf(cgirp, sizeof(cgirp), "%s", cgi_dir + strlen(chroot_dir));
/* ensure there is no leading / if user didn't end chrootdir with */ /* ensure there is no leading / if user didn't end chrootdir with */
while (*cgirp == '/') while (*cgirp == '/')
estrlcpy(cgirp, cgirp+1, sizeof(cgirp)); memmove(cgirp, cgirp+1, strlen(cgirp+1)+1);
if (strncmp(cgirp, path, strlen(cgirp)) != 0) if (strncmp(cgirp, path, strlen(cgirp)) != 0)
return 1; /* not in cgi_dir, go to display_file */ return 1; /* not in cgi_dir, go to display_file */
@ -252,7 +252,7 @@ do_cgi(const char *chroot_dir, const char *cgi_dir, const char *path, const char
*/ */
/* cgi file to execute */ /* cgi file to execute */
estrlcpy(cgifp, path + strlen(cgirp) + 1, sizeof(cgifp)); esnprintf(cgifp, sizeof(cgifp), "%s", path + strlen(cgirp) + 1);
if (!(*cgifp)) /* problem with cgi file, abort */ if (!(*cgifp)) /* problem with cgi file, abort */
return 1; return 1;
@ -357,7 +357,7 @@ read_request(char *request)
request[strcspn(request, "\r\n")] = '\0'; request[strcspn(request, "\r\n")] = '\0';
/* save request for logs */ /* save request for logs */
estrlcpy(_request, request, sizeof(_request)); esnprintf(_request, sizeof(_request), "%s", request);
/* remove all "/.." for safety reasons */ /* remove all "/.." for safety reasons */
while ((pos = strstr(request, "/..")) != NULL) while ((pos = strstr(request, "/..")) != NULL)
@ -369,19 +369,17 @@ read_request(char *request)
char * char *
set_path(char *path, size_t pathsiz, int virtualhost, const char *hostname) set_path(char *path, size_t pathsiz, int virtualhost, const char *hostname)
{ {
char tmp[GEMINI_REQUEST_MAX] = {'\0'};
if (strlen(path) == 0) /* this is root dir */
estrlcpy(path, "./", pathsiz);
/* path is in a subdir named hostname */ /* path is in a subdir named hostname */
if (virtualhost) { if (virtualhost) {
estrlcpy(tmp, hostname, sizeof(tmp)); char tmp[GEMINI_REQUEST_MAX] = {'\0'};
estrlcat(tmp, "/", sizeof(tmp)); esnprintf(tmp, sizeof(tmp), "%s/%s", hostname, path);
estrlcat(tmp, path, sizeof(tmp)); esnprintf(path, pathsiz, "%s", tmp);
estrlcpy(path, tmp, pathsiz);
} }
if (strlen(path) == 0) /* this is root dir */
esnprintf(path, pathsiz, "./");
return path; return path;
} }
@ -406,21 +404,17 @@ check_path(char *path, size_t pathsiz, int virtualhost, size_t hstnm_o)
/* check if dir path end with "/" */ /* check if dir path end with "/" */
if (path[strlen(path) - 1] != '/') { if (path[strlen(path) - 1] != '/') {
/* redirect to the dir with appropriate ending '/' */ /* redirect to the dir with appropriate ending '/' */
estrlcpy(tmp, "/", sizeof(tmp));
if (virtualhost) /* skip hostname */ if (virtualhost) /* skip hostname */
estrlcat(tmp, path+hstnm_o+1, sizeof(tmp)); esnprintf(tmp, sizeof(tmp), "/%s/", path+hstnm_o+1);
else else
estrlcat(tmp, path, sizeof(tmp)); esnprintf(tmp, sizeof(tmp), "/%s/", path);
estrlcat(tmp, "/", sizeof(tmp));
status(31, "%s", tmp); status(31, "%s", tmp);
stop(EXIT_SUCCESS, NULL); stop(EXIT_SUCCESS, NULL);
} }
/* check if DEFAULT_INDEX exists in directory */ /* check if DEFAULT_INDEX exists in directory */
estrlcpy(tmp, path, sizeof(tmp)); esnprintf(tmp, sizeof(tmp), "%s/%s", path, DEFAULT_INDEX);
estrlcat(tmp, "/", sizeof(tmp));
estrlcat(tmp, DEFAULT_INDEX, sizeof(tmp));
if (stat(tmp, &sb) == 0) if (stat(tmp, &sb) == 0)
estrlcpy(path, tmp, pathsiz); esnprintf(path, pathsiz, "%s", tmp);
} }
} }