summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGluzskiy Alexandr <sss@sss.chaoslab.ru>2020-05-13 13:01:48 +0300
committerGluzskiy Alexandr <sss@sss.chaoslab.ru>2020-05-13 13:01:48 +0300
commit5345c4c9f6311fa5dc27565a3b26bc5f91d92d7f (patch)
tree33f8494dc28c9e117cd40a0c1b7b133835450dfe
parent76076f53d567580240037137ba734c56a38d8698 (diff)
fixed few possible bugsiconv_file_converter
-rw-r--r--main.c (renamed from main.cpp)40
1 files changed, 35 insertions, 5 deletions
diff --git a/main.cpp b/main.c
index 3d149d9..b56e82b 100644
--- a/main.cpp
+++ b/main.c
@@ -27,6 +27,11 @@ int main(int argc, char **argv)
{
int opt = -1;
char ifile[256], ofile[256], fcharset[32], tcharset[32];
+ memset(ifile, 0, sizeof(ifile));
+ memset(ofile, 0, sizeof(ofile));
+ memset(fcharset, 0, sizeof(fcharset));
+ memset(tcharset, 0, sizeof(tcharset));
+
ifile[0] = 0;
ofile[0] = 0;
fcharset[0] = 0;
@@ -36,16 +41,16 @@ int main(int argc, char **argv)
switch(opt)
{
case 'i':
- strcpy(ifile, optarg);
+ strncpy(ifile, optarg, sizeof(ifile) - 1);
break;
case 'o':
- strcpy(ofile, optarg);
+ strncpy(ofile, optarg, sizeof(ofile) - 1);
break;
case 'f':
- strcpy(fcharset, optarg);
+ strncpy(fcharset, optarg, sizeof(fcharset) - 1);
break;
case 't':
- strcpy(tcharset, optarg);
+ strncpy(tcharset, optarg, sizeof(tcharset) - 1);
break;
default:
printf("usage: %s -ioft\n\t-i input file\n\t-o output file\n\t-f from charset\n\t-t to charset\n", argv[0]);
@@ -71,22 +76,46 @@ int main(int argc, char **argv)
}
while(!feof(in))
{
- char *buf = (char*)malloc(4096), *outbuf = (char*)malloc(4096), *op, *ip;
+ char *buf, *outbuf, *op, *ip;
+
+ buf = (char*)malloc(4096);
+ if (!buf)
+ {
+ printf("failed to allocate memory for \"buf\"\n");
+ return 1;
+ }
+ outbuf = (char*)malloc(4096);
+ if (!outbuf)
+ {
+ printf("failed to allocate memory for \"outbuf\"");
+ free(buf);
+ return 1;
+ }
op = outbuf;
ip = buf;
if(!fgets(buf, 4096, in))
+ {
+ if (buf)
+ free(buf);
+ if (outbuf)
+ free(outbuf);
break;
+ }
size_t len = strlen(buf), outlen = 4096;
int enc_len = iconv(id, &ip, &len, &op, &outlen);
if(enc_len == -1)
{
printf("failed to convert buffer with error: %s\n", strerror(errno));
+ free(buf);
+ free(outbuf);
return 1;
}
enc_len = 4096 - outlen;
if((int)fwrite(outbuf, 1, enc_len, out) != enc_len)
{
printf("failed to write data to file with error: %s\n", strerror(errno));
+ free(buf);
+ free(outbuf);
return 1;
}
free(buf);
@@ -94,3 +123,4 @@ int main(int argc, char **argv)
}
return 0;
}
+