From 3e096315b51bd1c356ffc196dea19ecf722c42de Mon Sep 17 00:00:00 2001 From: GangHeok Kim Date: Mon, 8 Apr 2013 20:56:14 +0900 Subject: [PATCH] Sync with master Change-Id: I0d094076778e90cc443ac1a1da64481dc355c882 --- include/pt_api.h | 3 +- libptdb/ptdb.c | 134 ++++++++++++++++++++++++++++--------------- packaging/print-service.spec | 2 +- src/pt_optionmapping.c | 93 ++++++++++++++++++++++-------- src/pt_ppd.c | 4 +- src/pt_utils.c | 4 ++ 6 files changed, 165 insertions(+), 75 deletions(-) diff --git a/include/pt_api.h b/include/pt_api.h index 2f2d262..f7ead41 100644 --- a/include/pt_api.h +++ b/include/pt_api.h @@ -178,7 +178,8 @@ typedef enum _pt_vendor_e { } pt_vendor_e; typedef enum _pt_print_option_e { - PT_OPTION_ID_PAPERSIZE = 0, + PT_OPTION_ID_PRINTER = 0, + PT_OPTION_ID_PAPERSIZE, PT_OPTION_ID_COPIES, PT_OPTION_ID_RANGE, PT_OPTION_ID_QUALITY, diff --git a/libptdb/ptdb.c b/libptdb/ptdb.c index 8f80435..ccd2f88 100644 --- a/libptdb/ptdb.c +++ b/libptdb/ptdb.c @@ -201,12 +201,13 @@ pt_file_info *pt_create_file_info(char *name, char *dir, pt_file_type type) if(!info) { return NULL; } - bzero(info, sizeof(*info)); + memset(info, '\0', sizeof(pt_file_info)); char *tmp_name = NULL; if(name) { tmp_name = strdup(name); if(!tmp_name) { fprintf(stderr, "String duplicating fails\n"); + pt_free_file_info(info); return NULL; } info->file_name = tmp_name; @@ -290,14 +291,16 @@ pt_file *pt_create_file(pt_file_info *file_info) char *string = strdup(TEMPLATE); if(!string) { perror("Can't duplicate string with strdup()"); + return NULL; } char *drname = dirname(string); - if(g_mkdir_with_parents(drname, S_IRWXU | S_IRWXG)) { + if(g_mkdir_with_parents(drname, S_IRWXU | S_IRWXG) == -1) { perror("Can't create nested directory path"); + PT_IF_FREE_MEM(string); return NULL; } - free(string); + PT_IF_FREE_MEM(string); file = strdup(TEMPLATE); if (!file) { @@ -309,6 +312,7 @@ pt_file *pt_create_file(pt_file_info *file_info) int filedesc = mkstemp(file); if (filedesc == -1) { perror("Can't create temp file"); + PT_IF_FREE_MEM(file); return NULL; } @@ -322,6 +326,7 @@ pt_file *pt_create_file(pt_file_info *file_info) if(rst) { fprintf(stderr, "Unlink temporary file fails\n"); } + PT_IF_FREE_MEM(file); return NULL; } @@ -336,9 +341,10 @@ pt_file *pt_create_file(pt_file_info *file_info) if(fclose(fd)) { PT_ERROR("File error: %s", strerror(errno)); } + PT_IF_FREE_MEM(file); return NULL; } - bzero(ptfile, sizeof(pt_file)); + memset(ptfile, '\0', sizeof(pt_file)); ptfile->ptfile = fd; ptfile->ptpath = file; @@ -353,7 +359,7 @@ char *reliable_read(int fd) fprintf(stderr, "call malloc() failed: %s\n", strerror(errno)); return NULL; } - bzero(buffer, BUFF_SIZE); + memset(buffer, '\0', BUFF_SIZE); new_buffer = buffer; ssize_t rst = 0; @@ -373,7 +379,7 @@ char *reliable_read(int fd) free(buffer); return NULL; } - bzero(new_buffer, new_size); + memset(new_buffer, '\0', new_size); strncpy(new_buffer, buffer, BUFF_SIZE); free(buffer); buffer = new_buffer; @@ -554,6 +560,9 @@ pt_file *extract_drv_by_key(drvm drv, const char *key, pt_search_key type) pt_free_file_info(file_info); if(pt_sync_file(file)) { fprintf(stderr, "Synchronize file with storage device fails\n"); + if(pt_free_file(file, PT_FREE_FILE_DEL)) { + fprintf(stderr, "Deleting temporary file fails\n"); + } return NULL; } break; @@ -610,26 +619,20 @@ outstr *filter_string(const char *original) char *value = NULL; char *begin = NULL, *end = NULL, *word = NULL; + if(!original) { + PT_ERROR("Argument is NULL"); + return NULL; + } + outstr *string = (outstr*)malloc(sizeof(outstr)); if(!string) { fprintf(stderr, "Allocating memory fails\n"); return NULL; } - bzero(string, sizeof(outstr)); - string->value = NULL; - string->modified = NULL; + memset(string, '\0', sizeof(outstr)); - char *copied = malloc(strlen(original)+1); - char *full_string = copied; - if (!copied) { - PT_ERROR("Can't allocate memory\n"); - clean_outstr(string); - free(string); - return NULL; - } ptrdiff_t strln = 0; - strcpy(copied, original); - begin = copied; + begin = (char *)original; unsigned char count = 0; @@ -650,13 +653,12 @@ outstr *filter_string(const char *original) strln = end - begin; value = malloc(strln+1); if (!value) { - free(full_string); clean_outstr(string); free(string); fprintf(stderr, "Can't allocate memory\n"); return NULL; } - bzero(value, strln+1); + memset(value, '\0', strln+1); strncpy(value, begin, strln); string->value = value; string->modified = end; @@ -673,11 +675,10 @@ outstr *filter_string(const char *original) if (!value) { clean_outstr(string); free(string); - free(full_string); fprintf(stderr, "Can't allocate memory\n"); return NULL; } - bzero(value, strln+1); + memset(value, '\0', strln+1); strncpy(value, begin, strln); string->value = value; string->modified = end; @@ -699,11 +700,10 @@ outstr *filter_string(const char *original) if (!value) { clean_outstr(string); free(string); - free(full_string); fprintf(stderr, "Can't allocate memory\n"); return NULL; } - bzero(value, strln+1); + memset(value, '\0', strln+1); strncpy(value, begin, strln); string->value = value; end++; @@ -713,10 +713,9 @@ outstr *filter_string(const char *original) char *filter_product(char *original, pt_search_key key) { - static const char *pname = "Product"; - static const char *attr = "Attribute"; - outstr *parse_str; - char *value = NULL; + const char *pname = "Product"; + const char *attr = "Attribute"; + outstr *result = NULL; if (!original) { fprintf(stderr, "original is null\n"); @@ -727,21 +726,63 @@ char *filter_product(char *original, pt_search_key key) fprintf(stderr, "Can't allocate memory\n"); return NULL; } + char *full_string = copied; + char *modified = NULL; + char *value = NULL; strcpy(copied, original); - parse_str = filter_string(copied); - if (!strcmp(parse_str->value, attr)) { - clean_outstr(parse_str); - parse_str = filter_string(parse_str->modified); - if(!strcmp(parse_str->value, pname)) { - clean_outstr(parse_str); - parse_str = filter_string(parse_str->modified); - clean_outstr(parse_str); - parse_str = filter_string(parse_str->modified); - value = parse_str->value; + result = filter_string(copied); + if(!result) { + PT_ERROR("Error in processing string"); + free(full_string); + return NULL; + } + modified = result->modified; + value = result->value; + free(result); + + if (!strncmp(value, attr, strlen(attr))) { + result = filter_string(modified); + if(!result) { + PT_ERROR("Error in processing string"); + free(value); + free(full_string); + return NULL; + } + free(value); + modified = result->modified; + value = result->value; + free(result); + + if(!strncmp(value, pname, strlen(pname))) { + result = filter_string(modified); + if(!result) { + PT_ERROR("Error in processing string"); + free(value); + free(full_string); + return NULL; + } + free(value); + modified = result->modified; + value = result->value; + free(result); + + result = filter_string(modified); + free(value); + value = result->value; + free(result); + } else { + free(value); + free(full_string); + return NULL; } + } else { + free(value); + free(full_string); + return NULL; } + free(full_string); return value; } @@ -837,7 +878,7 @@ drvm *read_drv(FILE *fd) GPtrArray *models; GPtrArray *strings; - drv = malloc(sizeof(drvm)); + drv = (drvm *)calloc(1, sizeof(drvm)); if (!drv) { fprintf(stderr, "Can't allocate memory\n"); return NULL; @@ -848,12 +889,13 @@ drvm *read_drv(FILE *fd) result = fread(&len, sizeof(uint32_t), 1, fd); if (result != 1) { fprintf(stderr, "Can't read data from file\n"); - free(drv); + pt_free_drvm(drv); return NULL; } - if(len <= 0) { + if(len == 0 || len > UINT32_MAX) { fprintf(stderr, "Invalid length(%d)\n", len); + pt_free_drvm(drv); return NULL; } @@ -883,7 +925,7 @@ drvm *read_drv(FILE *fd) return NULL; } - if(len <= 0) { + if (len == 0 || len > UINT32_MAX) { fprintf(stderr, "Invalid length(%d)\n", len); pt_free_drvm(drv); return NULL; @@ -950,7 +992,7 @@ model *read_model(FILE *fd) return NULL; } - if(len <= 0) { + if(len == 0 || len > UINT32_MAX) { fprintf(stderr, "Invalid length(%d)\n", len); pt_free_model(mod); return NULL; @@ -989,7 +1031,7 @@ model *read_model(FILE *fd) return NULL; } - if(len <= 0) { + if(len == 0 || len > UINT32_MAX) { fprintf(stderr, "Invalid length(%d)\n", len); pt_free_model(mod); return NULL; @@ -1077,7 +1119,7 @@ pt_dbconfig *pt_tune_db(FILE *file, const char *path) free(buffer); return NULL; } - bzero(config, sizeof(pt_dbconfig)); + memset(config, '\0', sizeof(pt_dbconfig)); config->dbfile = file; config->dbpath = copied_path; config->dbbuffer = buffer; diff --git a/packaging/print-service.spec b/packaging/print-service.spec index 88258e5..9ebeeb2 100755 --- a/packaging/print-service.spec +++ b/packaging/print-service.spec @@ -2,7 +2,7 @@ Name: print-service Summary: print service library -Version: 1.2.8 +Version: 1.2.9 Release: 1 Group: System/Libraries License: Flora Software License diff --git a/src/pt_optionmapping.c b/src/pt_optionmapping.c index d827961..66ac5ae 100644 --- a/src/pt_optionmapping.c +++ b/src/pt_optionmapping.c @@ -852,6 +852,26 @@ static void testprint(int paper) } #endif +static bool pt_validate_settings(int s, int q, int p, int c, int d) +{ + if (s < 0 || s >= paper_sizes->num_choices) { + return false; + } + if (q < PT_QUALITY_DRAFT || q > PT_QUALITY_HIGH) { + return false; + } + if (p < PT_PAPER_NORMAL || p > PT_PAPER_PHOTO) { + return false; + } + if (c < PT_GRAYSCALE_GRAYSCALE || c > PT_GRAYSCALE_COLOUR) { + return false; + } + if (d < PT_DUPLEX_OFF || d > PT_DUPLEX_TUMBLE) { + return false; + } + return true; +} + static void pt_try_settings(int s, int q, int p, int c, int d) { int sPaperSize = aPaperSize; @@ -860,51 +880,65 @@ static void pt_try_settings(int s, int q, int p, int c, int d) int sColor = aColor; int sDuplex = aDuplex; - aPaperSize = s; - aQuality = q; - aPaper = p; - aColor = c; - aDuplex = d; - - if (!pt_is_enabled(PT_OPTION_ID_QUALITY, q) || !pt_is_enabled(PT_OPTION_ID_PAPER, p) || - !pt_is_enabled(PT_OPTION_ID_GRAYSCALE, c) || !pt_is_enabled(PT_OPTION_ID_DUPLEX, d)) { - - aPaperSize = sPaperSize; - aQuality = sQuality; - aPaper = sPaper; - aColor = sColor; - aDuplex = sDuplex; + if(pt_validate_settings(s, q, p, c, d)) { + if (!pt_is_enabled(PT_OPTION_ID_QUALITY, q) || !pt_is_enabled(PT_OPTION_ID_PAPER, p) || + !pt_is_enabled(PT_OPTION_ID_GRAYSCALE, c) || !pt_is_enabled(PT_OPTION_ID_DUPLEX, d)) { + aPaperSize = sPaperSize; + aQuality = sQuality; + aPaper = sPaper; + aColor = sColor; + aDuplex = sDuplex; + } else { + aPaperSize = s; + aQuality = q; + aPaper = p; + aColor = c; + aDuplex = d; + } } } void pt_load_user_choice(void) { FILE *stream; - char buf[255]; + char buf[255] = {0,}; char *args; int s; int q; int p; int c; int d; + char *null_check = NULL; stream = fopen(PT_USER_OPTION_CONFIG_FILE,"r"); if(stream == NULL) { PT_DEBUG("Can't open settings file"); return; } - while(fgets(buf, sizeof(buf)-1, stream) != NULL) { - args = strchr(buf, ','); - if(args == NULL) { + while(fgets(buf, sizeof(buf), stream) != NULL) { + null_check = strchr(buf, '\n'); + if (null_check) { + *null_check = '\0'; + } + + gchar **tokens = g_strsplit((gchar *)buf, ",", 0); + if (g_strv_length(tokens) != 6) { + g_strfreev(tokens); continue; } - *args++ = '\0'; - if (!strcasecmp(buf, aModelName)) { - if (5 == sscanf(args, "%d,%d,%d,%d,%d", &s, &q, &p, &c, &d)) { - pt_try_settings(s, q, p, c, d); - } + + if (!strcasecmp(tokens[0], aModelName)) { + s = atoi(tokens[1]); + q = atoi(tokens[2]); + p = atoi(tokens[3]); + c = atoi(tokens[4]); + d = atoi(tokens[5]); + pt_try_settings(s, q, p, c, d); + g_strfreev(tokens); break; } + g_strfreev(tokens); } + fclose(stream); } int pt_set_choice(int op, int ch) @@ -1138,6 +1172,7 @@ void pt_save_user_choice(void) { fpos_t position; char buf[255]; char *args; + int ret = -1; stream = fopen(PT_USER_OPTION_CONFIG_FILE,"r+"); if(stream == NULL) { @@ -1156,14 +1191,22 @@ void pt_save_user_choice(void) { } *args++ = '\0'; if(!strcasecmp(buf, aModelName)) { - fsetpos(stream, &position); + ret = fsetpos(stream, &position); + if (ret == -1) { + fclose(stream); + return; + } fprintf(stream, "%s,%03d,%03d,%03d,%03d,%03d\n", aModelName, aPaperSize, aQuality, aPaper, aColor, aDuplex); fclose(stream); return; } fgetpos(stream, &position); } - fsetpos(stream, &position); + ret = fsetpos(stream, &position); + if (ret == -1) { + fclose(stream); + return; + } fprintf(stream, "%s,%03d,%03d,%03d,%03d,%03d\n", aModelName, aPaperSize, aQuality, aPaper, aColor, aDuplex); fclose(stream); } diff --git a/src/pt_ppd.c b/src/pt_ppd.c index 64af905..0983adf 100644 --- a/src/pt_ppd.c +++ b/src/pt_ppd.c @@ -208,9 +208,9 @@ int pt_get_printer_ppd(pt_printer_mgr_t *printer) return PT_ERR_FAIL; } - bzero(printer->ppd, PT_MAX_LENGTH); + memset(printer->ppd, '\0', PT_MAX_LENGTH); - strncpy(printer->ppd, abspath, PT_MAX_LENGTH); + strncpy(printer->ppd, abspath, PT_MAX_LENGTH-1); free(abspath); free(output); PRINT_SERVICE_FUNC_LEAVE; diff --git a/src/pt_utils.c b/src/pt_utils.c index 4f3cc3e..194b455 100644 --- a/src/pt_utils.c +++ b/src/pt_utils.c @@ -355,6 +355,10 @@ void pt_utils_remove_files_in(const char *path) ret = chdir(path); if (ret == 0) { dir = opendir(path); + if (dir == NULL) { + PT_IF_FREE_MEM(cwd); + return; + } while ((entry = readdir(dir)) != NULL) { PT_DEBUG("Remove %s", entry->d_name); iret = remove(entry->d_name); -- 2.7.4