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;
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) {
int filedesc = mkstemp(file);
if (filedesc == -1) {
perror("Can't create temp file");
+ PT_IF_FREE_MEM(file);
return NULL;
}
if(rst) {
fprintf(stderr, "Unlink temporary file fails\n");
}
+ PT_IF_FREE_MEM(file);
return NULL;
}
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;
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;
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;
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;
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;
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;
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;
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++;
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");
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;
}
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;
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;
}
return NULL;
}
- if(len <= 0) {
+ if (len == 0 || len > UINT32_MAX) {
fprintf(stderr, "Invalid length(%d)\n", len);
pt_free_drvm(drv);
return NULL;
return NULL;
}
- if(len <= 0) {
+ if(len == 0 || len > UINT32_MAX) {
fprintf(stderr, "Invalid length(%d)\n", len);
pt_free_model(mod);
return NULL;
return NULL;
}
- if(len <= 0) {
+ if(len == 0 || len > UINT32_MAX) {
fprintf(stderr, "Invalid length(%d)\n", len);
pt_free_model(mod);
return NULL;
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;
}
#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;
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)
fpos_t position;
char buf[255];
char *args;
+ int ret = -1;
stream = fopen(PT_USER_OPTION_CONFIG_FILE,"r+");
if(stream == NULL) {
}
*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);
}