Andrew Nguyen : crypt32: Avoid relying on PATH_MAX in import_certs_from_dir helper.
Alexandre Julliard
julliard at winehq.org
Fri Oct 8 12:38:06 CDT 2010
Module: wine
Branch: master
Commit: 6f2513d39a2247af5acb0144094c387323cfb38c
URL: http://source.winehq.org/git/wine.git/?a=commit;h=6f2513d39a2247af5acb0144094c387323cfb38c
Author: Andrew Nguyen <anguyen at codeweavers.com>
Date: Thu Oct 7 20:56:40 2010 -0500
crypt32: Avoid relying on PATH_MAX in import_certs_from_dir helper.
PATH_MAX is not guaranteed to be available on all platforms, and it is
inadequate as a hardcoded buffer size anyway.
---
dlls/crypt32/rootstore.c | 50 ++++++++++++++++++++++++++++++++++++---------
1 files changed, 40 insertions(+), 10 deletions(-)
diff --git a/dlls/crypt32/rootstore.c b/dlls/crypt32/rootstore.c
index 70b5ef8..506497a 100644
--- a/dlls/crypt32/rootstore.c
+++ b/dlls/crypt32/rootstore.c
@@ -326,6 +326,32 @@ static BOOL import_certs_from_file(int fd, HCERTSTORE store)
static BOOL import_certs_from_path(LPCSTR path, HCERTSTORE store,
BOOL allow_dir);
+static BOOL check_buffer_resize(char **ptr_buf, size_t *buf_size, size_t check_size)
+{
+ if (check_size > *buf_size)
+ {
+ *buf_size = check_size;
+
+ if (*ptr_buf)
+ {
+ char *realloc_buf = CryptMemRealloc(*ptr_buf, *buf_size);
+
+ if (!realloc_buf)
+ return FALSE;
+
+ *ptr_buf = realloc_buf;
+ }
+ else
+ {
+ *ptr_buf = CryptMemAlloc(*buf_size);
+ if (!*ptr_buf)
+ return FALSE;
+ }
+ }
+
+ return TRUE;
+}
+
/* Opens path, which must be a directory, and imports certificates from every
* file in the directory into store.
* Returns TRUE if any certificates were successfully imported.
@@ -341,23 +367,27 @@ static BOOL import_certs_from_dir(LPCSTR path, HCERTSTORE store)
dir = opendir(path);
if (dir)
{
- size_t bufsize = strlen(path) + 1 + PATH_MAX + 1;
- char *filebuf = CryptMemAlloc(bufsize);
+ size_t path_len = strlen(path), bufsize = 0;
+ char *filebuf = NULL;
- if (filebuf)
+ struct dirent *entry;
+ while ((entry = readdir(dir)))
{
- struct dirent *entry;
- while ((entry = readdir(dir)))
+ if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
{
- if (strcmp(entry->d_name, ".") && strcmp(entry->d_name, ".."))
+ size_t name_len = strlen(entry->d_name);
+
+ if (!check_buffer_resize(&filebuf, &bufsize, path_len + 1 + name_len + 1))
{
- snprintf(filebuf, bufsize, "%s/%s", path, entry->d_name);
- if (import_certs_from_path(filebuf, store, FALSE) && !ret)
- ret = TRUE;
+ ERR("Path buffer (re)allocation failed with out of memory condition\n");
+ break;
}
+ snprintf(filebuf, bufsize, "%s/%s", path, entry->d_name);
+ if (import_certs_from_path(filebuf, store, FALSE) && !ret)
+ ret = TRUE;
}
- CryptMemFree(filebuf);
}
+ CryptMemFree(filebuf);
closedir(dir);
}
return ret;
More information about the wine-cvs
mailing list