Added array bounds checking

SVN revision: 1842
This commit is contained in:
Stefan Ritt 2007-04-13 15:25:21 +00:00
parent 9d490514de
commit 5887d3ad5d

View File

@ -644,39 +644,79 @@ static void memory_error_and_abort(char *func)
print an error message and abort. */ print an error message and abort. */
void *xmalloc(size_t bytes) void *xmalloc(size_t bytes)
{ {
void *temp; char *temp;
temp = malloc(bytes); temp = (char *) malloc(bytes + 12);
if (temp == 0) if (temp == 0)
memory_error_and_abort("xmalloc"); memory_error_and_abort("xmalloc");
return (temp);
/* put magic number around array and put array size */
*(unsigned int *)(temp + 0) = bytes;
*(unsigned int *)(temp + 4) = 0xdeadc0de;
*(unsigned int *)(temp + bytes + 8) = 0xdeadc0de;
return (temp+8);
} }
void *xcalloc(size_t count, size_t bytes) void *xcalloc(size_t count, size_t bytes)
{ {
void *temp; char *temp;
temp = calloc(count, bytes); temp = (char *) malloc(count*bytes + 12);
if (temp == 0) if (temp == 0)
memory_error_and_abort("xcalloc"); memory_error_and_abort("xcalloc");
return (temp); memset(temp, 0, count*bytes + 12);
/* put magic number around array */
*(unsigned int *)(temp + 0) = count*bytes;
*(unsigned int *)(temp + 4) = 0xdeadc0de;
*(unsigned int *)(temp + count*bytes + 8) = 0xdeadc0de;
return (temp+8);
} }
void *xrealloc(void *pointer, size_t bytes) void *xrealloc(void *pointer, size_t bytes)
{ {
void *temp; char *temp;
int old_size;
temp = pointer ? realloc(pointer, bytes) : malloc(bytes); if (pointer == NULL)
return xmalloc(bytes);
/* check old magic number */
temp = pointer;
assert(*((unsigned int *)(temp-4)) == 0xdeadc0de);
old_size = *((unsigned int *)(temp-8));
assert(*((unsigned int *)(temp+old_size)) == 0xdeadc0de);
temp = (char *) realloc(temp-8, bytes+12);
if (temp == 0) if (temp == 0)
memory_error_and_abort("xrealloc"); memory_error_and_abort("xrealloc");
return (temp);
/* put magic number around array */
*(unsigned int *)(temp + 0) = bytes;
*(unsigned int *)(temp + 4) = 0xdeadc0de;
*(unsigned int *)(temp + bytes + 8) = 0xdeadc0de;
return (temp+8);
} }
void xfree(void *pointer) void xfree(void *pointer)
{ {
if (pointer) char *temp;
free(pointer); int old_size;
if (!pointer)
return;
/* check for magic byte */
temp = pointer;
assert(*((unsigned int *)(temp-4)) == 0xdeadc0de);
old_size = *((unsigned int *)(temp-8));
assert(*((unsigned int *)(temp+old_size)) == 0xdeadc0de);
free(temp-8);
} }
char *xstrdup(const char *string) char *xstrdup(const char *string)
@ -4171,12 +4211,12 @@ int el_retrieve(LOGBOOK * lbs,
text, textsize, in_reply_to, reply_to, attachment, encoding, locked_by); text, textsize, in_reply_to, reply_to, attachment, encoding, locked_by);
} }
message = malloc(TEXT_SIZE + 1000); message = xmalloc(TEXT_SIZE + 1000);
lseek(fh, lbs->el_index[index].offset, SEEK_SET); lseek(fh, lbs->el_index[index].offset, SEEK_SET);
i = my_read(fh, message, TEXT_SIZE + 1000 - 1); i = my_read(fh, message, TEXT_SIZE + 1000 - 1);
if (i <= 0) { if (i <= 0) {
free(message); xfree(message);
close(fh); close(fh);
return EL_FILE_ERROR; return EL_FILE_ERROR;
} }
@ -4185,7 +4225,7 @@ int el_retrieve(LOGBOOK * lbs,
close(fh); close(fh);
if (strncmp(message, "$@MID@$:", 8) != 0) { if (strncmp(message, "$@MID@$:", 8) != 0) {
free(message); xfree(message);
/* file might have been edited, rebuild index */ /* file might have been edited, rebuild index */
el_build_index(lbs, TRUE); el_build_index(lbs, TRUE);
return el_retrieve(lbs, message_id, date, attr_list, attrib, n_attr, return el_retrieve(lbs, message_id, date, attr_list, attrib, n_attr,
@ -4194,7 +4234,7 @@ int el_retrieve(LOGBOOK * lbs,
/* check for correct ID */ /* check for correct ID */
if (atoi(message + 8) != message_id) { if (atoi(message + 8) != message_id) {
free(message); xfree(message);
return EL_FILE_ERROR; return EL_FILE_ERROR;
} }
@ -4272,7 +4312,7 @@ int el_retrieve(LOGBOOK * lbs,
if ((int) strlen(p) >= *textsize) { if ((int) strlen(p) >= *textsize) {
strlcpy(text, p, *textsize); strlcpy(text, p, *textsize);
show_error("Entry too long to display. Please increase TEXT_SIZE and recompile elogd."); show_error("Entry too long to display. Please increase TEXT_SIZE and recompile elogd.");
free(message); xfree(message);
return EL_FILE_ERROR; return EL_FILE_ERROR;
} else { } else {
strlcpy(text, p, *textsize); strlcpy(text, p, *textsize);
@ -4292,7 +4332,7 @@ int el_retrieve(LOGBOOK * lbs,
} }
} }
free(message); xfree(message);
return EL_SUCCESS; return EL_SUCCESS;
} }
@ -11124,7 +11164,7 @@ void adjust_crlf(char *buffer, int bufsize)
} }
if ((int) strlen(buffer) + 2 >= bufsize) { if ((int) strlen(buffer) + 2 >= bufsize) {
free(tmpbuf); xfree(tmpbuf);
return; return;
} }
@ -21710,7 +21750,7 @@ BOOL convert_password_file(char *file_name)
return FALSE; return FALSE;
len = lseek(fh, 0, SEEK_END); len = lseek(fh, 0, SEEK_END);
lseek(fh, 0, SEEK_SET); lseek(fh, 0, SEEK_SET);
buf = malloc(len + 1); buf = xmalloc(len + 1);
assert(buf); assert(buf);
i = my_read(fh, buf, len); i = my_read(fh, buf, len);
buf[i] = 0; buf[i] = 0;
@ -21742,7 +21782,7 @@ BOOL convert_password_file(char *file_name)
name[i] = *p++; name[i] = *p++;
name[i] = 0; name[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
free(buf); xfree(buf);
return FALSE; return FALSE;
} }
@ -21750,7 +21790,7 @@ BOOL convert_password_file(char *file_name)
password[i] = *p++; password[i] = *p++;
password[i] = 0; password[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
free(buf); xfree(buf);
return FALSE; return FALSE;
} }
@ -21758,7 +21798,7 @@ BOOL convert_password_file(char *file_name)
full_name[i] = *p++; full_name[i] = *p++;
full_name[i] = 0; full_name[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
free(buf); xfree(buf);
return FALSE; return FALSE;
} }
@ -21766,7 +21806,7 @@ BOOL convert_password_file(char *file_name)
email[i] = *p++; email[i] = *p++;
email[i] = 0; email[i] = 0;
if (*p++ != ':') { if (*p++ != ':') {
free(buf); xfree(buf);
return FALSE; return FALSE;
} }
@ -21774,7 +21814,7 @@ BOOL convert_password_file(char *file_name)
email_notify[i] = *p++; email_notify[i] = *p++;
email_notify[i] = 0; email_notify[i] = 0;
if (*p && *p != '\n' && *p != '\r') { if (*p && *p != '\n' && *p != '\r') {
free(buf); xfree(buf);
return FALSE; return FALSE;
} }
@ -21800,7 +21840,7 @@ BOOL convert_password_file(char *file_name)
printf("Ok\n"); printf("Ok\n");
free(buf); xfree(buf);
return TRUE; return TRUE;
} }