summaryrefslogtreecommitdiff
path: root/hangul/hangulkeyboard.c
diff options
context:
space:
mode:
authorChoe Hwanjin <choe.hwanjin@gmail.com>2018-06-05 14:00:40 +0900
committerChoe Hwanjin <choe.hwanjin@gmail.com>2018-06-05 14:06:35 +0900
commita4c5f1e052650c7839a9667a5431ae437b217e03 (patch)
tree1e9cf01ff1ee7c91e3b7ab52bf9ec4657102813d /hangul/hangulkeyboard.c
parent4277b88571fed21ea022321934c5b1de7285adf8 (diff)
downloadlibhangul-a4c5f1e052650c7839a9667a5431ae437b217e03.tar.gz
PATH_MAX 사용 코드 제거
hurd에서는 PATH_MAX가 없는 것 같다. https://buildd.debian.org/status/fetch.php?pkg=libhangul&arch=hurd-i386&ver=0.1.0%2Bgit20170815-1&stamp=1511811053&raw=0 관련 코드는 정확한 PATH_MAX값이 필요한 것이 아니므로 PATH_MAX를 사용하지 않도록 수정하는 편이 좋을 것 같다. 파일 이름에 사용할 버퍼에 필요한 스트링 길이를 계산하고 메모리를 할당하여 처리한다. https://github.com/choehwanjin/libhangul/issues/24
Diffstat (limited to 'hangul/hangulkeyboard.c')
-rw-r--r--hangul/hangulkeyboard.c46
1 files changed, 36 insertions, 10 deletions
diff --git a/hangul/hangulkeyboard.c b/hangul/hangulkeyboard.c
index c04a417..fbb1822 100644
--- a/hangul/hangulkeyboard.c
+++ b/hangul/hangulkeyboard.c
@@ -782,13 +782,25 @@ hangul_keyboard_new_from_file(const char* path)
static unsigned
hangul_keyboard_list_load_dir(const char* path)
{
- char pattern[PATH_MAX];
- snprintf(pattern, sizeof(pattern), "%s/*.xml", path);
+ if (path == NULL) {
+ return 0;
+ }
+
+ const char* subpattern = "/*.xml";
+ size_t len = strlen(path) + strlen(subpattern) + 1;
+ char* pattern = (char*)malloc(len);
+ if (pattern == NULL) {
+ return 0;
+ }
+
+ snprintf(pattern, len, "%s%s", path, subpattern);
glob_t result;
int res = glob(pattern, GLOB_ERR, NULL, &result);
- if (res != 0)
+ if (res != 0) {
+ free(pattern);
return 0;
+ }
size_t i;
for (i = 0; i < result.gl_pathc; ++i) {
@@ -799,6 +811,7 @@ hangul_keyboard_list_load_dir(const char* path)
}
globfree(&result);
+ free(pattern);
return hangul_keyboards.n;
}
@@ -839,17 +852,30 @@ hangul_keyboard_list_init()
n += hangul_keyboard_list_load_dir(LIBHANGUL_KEYBOARD_DIR);
/* 유저의 개별 키보드 파일 로딩 */
- char user_data_dir[PATH_MAX];
+ char* user_data_dir = NULL;
char* xdg_data_home = getenv("XDG_DATA_HOME");
if (xdg_data_home == NULL) {
- char* home_dir = getenv("HOME");
- snprintf(user_data_dir, sizeof(user_data_dir),
- "%s/.local/share/libhangul/keyboards", home_dir);
+ char* home_dir = getenv("HOME");
+ if (home_dir != NULL) {
+ const char* subdir = "/.local/share/libhangul/keyboards";
+ size_t len = strlen(home_dir) + strlen(subdir) + 1;
+ user_data_dir = (char*)malloc(len);
+ if (user_data_dir != NULL) {
+ snprintf(user_data_dir, len, "%s%s", home_dir, subdir);
+ }
+ }
} else {
- snprintf(user_data_dir, sizeof(user_data_dir),
- "%s/libhangul/keyboards", xdg_data_home);
+ const char* subdir = "/libhangul/keyboards";
+ size_t len = strlen(xdg_data_home) + strlen(subdir) + 1;
+ user_data_dir = (char*)malloc(len);
+ if (user_data_dir != NULL) {
+ snprintf(user_data_dir, len, "%s%s", xdg_data_home, subdir);
+ }
+ }
+ if (user_data_dir != NULL) {
+ n += hangul_keyboard_list_load_dir(user_data_dir);
+ free(user_data_dir);
}
- n += hangul_keyboard_list_load_dir(user_data_dir);
if (n == 0)
return 1;