From: Choe Hwanjin Date: Sat, 1 Sep 2007 03:18:11 +0000 (+0900) Subject: memory 관리 개선: X-Git-Tag: libhangul-0.0.7~18 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=6752d8484871d57e37b87b4e1bb95ba5952855ef;p=platform%2Fcore%2Fuifw%2Flibhangul.git memory 관리 개선: * malloc에서 NULL 리턴하는 경우 처리 추가 * malloc에 전달되는 크기가 ULONG_MAX를 넘는 경우 처리 * 불필요한 int arugment를 unsigned int로 바꿈 git-svn-id: http://kldp.net/svn/hangul/libhangul/trunk@135 8f00fcd2-89fc-0310-932e-b01be5b65e01 --- diff --git a/hangul/hangul.h b/hangul/hangul.h index fa4a9a5..24b96bd 100644 --- a/hangul/hangul.h +++ b/hangul/hangul.h @@ -88,7 +88,7 @@ void hangul_keyboard_set_type(HangulKeyboard *keyboard, int type); HangulCombination* hangul_combination_new(); void hangul_combination_delete(HangulCombination *combination); bool hangul_combination_set_data(HangulCombination* combination, - ucschar* first, ucschar* second, ucschar* result, int n); + ucschar* first, ucschar* second, ucschar* result, unsigned int n); /* input context */ HangulInputContext* hangul_ic_new(const char* keyboard); @@ -130,9 +130,9 @@ void hanja_table_delete(HanjaTable *table); int hanja_list_get_size(const HanjaList *list); const char* hanja_list_get_key(const HanjaList *list); -const Hanja* hanja_list_get_nth(const HanjaList *list, int n); -const char* hanja_list_get_nth_value(const HanjaList *list, int n); -const char* hanja_list_get_nth_comment(const HanjaList *list, int n); +const Hanja* hanja_list_get_nth(const HanjaList *list, unsigned int n); +const char* hanja_list_get_nth_value(const HanjaList *list, unsigned int n); +const char* hanja_list_get_nth_comment(const HanjaList *list, unsigned int n); void hanja_list_delete(HanjaList *list); const char* hanja_get_key(const Hanja* hanja); diff --git a/hangul/hangulinputcontext.c b/hangul/hangulinputcontext.c index b10b904..fbac97a 100644 --- a/hangul/hangulinputcontext.c +++ b/hangul/hangulinputcontext.c @@ -24,6 +24,7 @@ #include #include #include +#include #include "hangul.h" @@ -236,9 +237,12 @@ hangul_combination_make_key(ucschar first, ucschar second) bool hangul_combination_set_data(HangulCombination* combination, ucschar* first, ucschar* second, ucschar* result, - int n) + unsigned int n) { - if (combination == NULL || n == 0) + if (combination == NULL) + return false; + + if (n == 0 || n > ULONG_MAX / sizeof(HangulCombinationItem)) return false; combination->table = malloc(sizeof(HangulCombinationItem) * n); diff --git a/hangul/hanja.c b/hangul/hanja.c index e7998bd..7607162 100644 --- a/hangul/hanja.c +++ b/hangul/hanja.c @@ -20,6 +20,7 @@ #include #endif +#include #include #include #include @@ -28,19 +29,19 @@ struct _Hanja { - const char *key; - const char *value; - const char *comment; + char *key; + char *value; + char *comment; }; struct _HanjaList { - const char *key; - int nitems; + char *key; + unsigned int nitems; Hanja **items; }; struct _HanjaTable { - int nmember; + unsigned int nmember; HanjaList **base; }; @@ -83,8 +84,10 @@ slist_append(struct slist *head, void *data) if (head == NULL) { head = malloc(sizeof(struct slist)); - head->data = data; - head->next = NULL; + if (head != NULL) { + head->data = data; + head->next = NULL; + } return head; } @@ -93,8 +96,10 @@ slist_append(struct slist *head, void *data) continue; tail->next = malloc(sizeof(struct slist)); - tail->next->data = data; - tail->next->next = NULL; + if (tail->next != NULL) { + tail->next->data = data; + tail->next->next = NULL; + } return head; } @@ -110,10 +115,10 @@ slist_delete(struct slist *head) } } -static int +static unsigned int slist_length(struct slist *head) { - int n = 0; + unsigned int n = 0; while (head != NULL) { head = head->next; n++; @@ -128,7 +133,7 @@ hanja_new(const char *key, const char *value, const char *comment) Hanja *item; item = malloc(sizeof(Hanja)); - if (item) { + if (item != NULL) { item->key = strdup(key); item->value = strdup(value); if (comment != NULL) @@ -167,18 +172,29 @@ hanja_get_comment(const Hanja* hanja) static HanjaList * hanja_list_new_from_slist(const char *key, struct slist *items) { + unsigned int nitems; HanjaList *list; + nitems = slist_length(items); + if (nitems > ULONG_MAX / sizeof(Hanja*)) + return NULL; + list = malloc(sizeof(HanjaList)); - if (list) { + if (list != NULL) { int i; list->key = strdup(key); - list->nitems = slist_length(items); + list->nitems = nitems; list->items = malloc(sizeof(Hanja*) * list->nitems); - - for (i = 0; i < list->nitems; i++) { - list->items[i] = items->data; - items = items->next; + if (list->items != NULL) { + for (i = 0; i < list->nitems; i++) { + list->items[i] = items->data; + items = items->next; + } + } else { + if (list->key != NULL) + free(list->key); + free(list); + list = NULL; } } @@ -188,16 +204,26 @@ hanja_list_new_from_slist(const char *key, struct slist *items) static HanjaTable * hanja_table_new_from_slist(struct slist *lists) { + unsigned int nitems; HanjaTable *table; + nitems = slist_length(lists); + if (nitems > ULONG_MAX / sizeof(HanjaList*)) + return NULL; + table = malloc(sizeof(HanjaTable)); if (table) { int i; - table->nmember = slist_length(lists); + table->nmember = nitems; table->base = malloc(sizeof(HanjaList*) * table->nmember); - for (i = 0; i < table->nmember; i++) { - table->base[i] = lists->data; - lists = lists->next; + if (table->base != NULL) { + for (i = 0; i < table->nmember; i++) { + table->base[i] = lists->data; + lists = lists->next; + } + } else { + free(table); + table = NULL; } } return table; @@ -411,24 +437,24 @@ hanja_list_get_key(const HanjaList *list) } const Hanja* -hanja_list_get_nth(const HanjaList *list, int n) +hanja_list_get_nth(const HanjaList *list, unsigned int n) { if (list != NULL) { - if (n >= 0 && n < list->nitems) + if (n < list->nitems) return list->items[n]; } return NULL; } const char* -hanja_list_get_nth_value(const HanjaList *list, int n) +hanja_list_get_nth_value(const HanjaList *list, unsigned int n) { const Hanja* hanja = hanja_list_get_nth(list, n); return hanja_get_value(hanja); } const char* -hanja_list_get_nth_comment(const HanjaList *list, int n) +hanja_list_get_nth_comment(const HanjaList *list, unsigned int n) { const Hanja* hanja = hanja_list_get_nth(list, n); return hanja_get_comment(hanja);