fixed some memory corruption

git-svn-id: https://vsmartcard.svn.sourceforge.net/svnroot/vsmartcard@362 96b47cad-a561-4643-ad3b-153ac7d7599c
This commit is contained in:
frankmorgner
2010-11-15 13:40:20 +00:00
parent bad4d94312
commit 6342c34f8d
2 changed files with 26 additions and 23 deletions

View File

@@ -25,8 +25,9 @@
#include "pcsc-relay.h" #include "pcsc-relay.h"
struct picc_data { struct picc_data {
char *buf; char *e_rapdu;
size_t bufmax; char *line;
size_t linemax;
FILE *fd; FILE *fd;
}; };
static int picc_encode_rapdu(const unsigned char *inbuf, size_t inlen, static int picc_encode_rapdu(const unsigned char *inbuf, size_t inlen,
@@ -90,8 +91,10 @@ int picc_decode_apdu(const char *inbuf, size_t inlen,
length = strtoul(inbuf, &end, 16); length = strtoul(inbuf, &end, 16);
/* check for ':' right behind the length */ /* check for ':' right behind the length */
if (inbuf+inlen < end+1 || end[0] != ':') if (inbuf+inlen < end+1 || end[0] != ':') {
return 0; *outlen = 0;
return 1;
}
end++; end++;
p = realloc(*outbuf, length); p = realloc(*outbuf, length);
@@ -105,11 +108,11 @@ int picc_decode_apdu(const char *inbuf, size_t inlen,
while(inbuf+inlen > end && length > pos) { while(inbuf+inlen > end && length > pos) {
b = strtoul(end, &end, 16); b = strtoul(end, &end, 16);
if (b > 0xff) { if (b > 0xff) {
ERROR( "%s:%u Error decoding C-APDU\n", __FILE__, __LINE__); ERROR("Error decoding C-APDU\n");
return 0; return 0;
} }
(*outbuf)[pos++] = b; p[pos++] = b;
} }
*outlen = length; *outlen = length;
@@ -131,8 +134,9 @@ static int picc_connect(driver_data_t **driver_data)
return 0; return 0;
*driver_data = data; *driver_data = data;
data->buf = NULL; data->e_rapdu = NULL;
data->bufmax = 0; data->line = NULL;
data->linemax = 0;
data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/ data->fd = fopen(PICCDEV, "a+"); /*O_NOCTTY ?*/
if (!data->fd) { if (!data->fd) {
@@ -155,7 +159,8 @@ static int picc_disconnect(driver_data_t *driver_data)
if (data) { if (data) {
if (data->fd) if (data->fd)
fclose(data->fd); fclose(data->fd);
free(data->buf); free(data->e_rapdu);
free(data->line);
free(data); free(data);
} }
@@ -174,25 +179,25 @@ static int picc_receive_capdu(driver_data_t *driver_data,
/* read C-APDU */ /* read C-APDU */
linelen = getline(&data->buf, &data->bufmax, data->fd); linelen = getline(&data->line, &data->linemax, data->fd);
if (linelen < 0) { if (linelen <= 0) {
if (linelen < 0) { if (linelen < 0) {
ERROR("Error reading from %s: %s\n", PICCDEV, strerror(errno)); ERROR("Error reading from %s: %s\n", PICCDEV, strerror(errno));
return 0; return 0;
} }
} if (linelen == 0) {
if (linelen == 0) { *len = 0;
*len = 0; return 1;
return 1; }
} }
if (fflush(data->fd) != 0) if (fflush(data->fd) != 0)
ERROR("Warning, fflush failed: %s\n", strerror(errno)); ERROR("Warning, fflush failed: %s\n", strerror(errno));
DEBUG("%s", data->buf); DEBUG("%s", data->line);
/* decode C-APDU */ /* decode C-APDU */
return picc_decode_apdu(data->buf, linelen, capdu, len); return picc_decode_apdu(data->line, linelen, capdu, len);
} }
static int picc_send_rapdu(driver_data_t *driver_data, static int picc_send_rapdu(driver_data_t *driver_data,
@@ -206,16 +211,14 @@ static int picc_send_rapdu(driver_data_t *driver_data,
/* encode R-APDU */ /* encode R-APDU */
if (!picc_encode_rapdu(rapdu, len, &data->buf, &buflen)) if (!picc_encode_rapdu(rapdu, len, &data->e_rapdu, &buflen))
return 0; return 0;
if (data->bufmax < buflen)
data->bufmax = buflen;
/* write R-APDU */ /* write R-APDU */
DEBUG("INF: Writing R-APDU\r\n%s\r\n", data->buf); DEBUG("INF: Writing R-APDU\r\n%s\r\n", data->e_rapdu);
if (fprintf(data->fd,"%s\r\n", data->buf) < 0) { if (fprintf(data->fd,"%s\r\n", data->e_rapdu) < 0) {
ERROR("Error writing to %s: %s\n", PICCDEV, strerror(errno)); ERROR("Error writing to %s: %s\n", PICCDEV, strerror(errno));
return 0; return 0;
} }

View File

@@ -92,7 +92,7 @@ void daemonize(void) {
ERROR("setsid: %s\n", strerror(errno)); ERROR("setsid: %s\n", strerror(errno));
goto err; goto err;
} }
/* Change the current working directory. This prevents the current /* Change the current working directory. This prevents the current
* directory from being locked; hence not being able to remove it. */ * directory from being locked; hence not being able to remove it. */
if (chdir("/") < 0) { if (chdir("/") < 0) {