* syslog.cc (syslog): Use a less malloc-intensive method for allocating the

buffer.  Also fix a buffer overrun.
This commit is contained in:
Christopher Faylor 2000-08-11 01:56:05 +00:00
parent 20eb1efd9d
commit 7b972f5da5
2 changed files with 28 additions and 31 deletions

View File

@ -1,3 +1,8 @@
Thu Aug 10 21:54:29 2000 Christopher Faylor <cgf@cygnus.com>
* syslog.cc (syslog): Use a less malloc-intensive method for allocating
the buffer. Also fix a buffer overrun.
Thu Aug 10 15:31:39 2000 Christopher Faylor <cgf@cygnus.com> Thu Aug 10 15:31:39 2000 Christopher Faylor <cgf@cygnus.com>
* winsup.h: Change strchr inline for strange gcc problem. * winsup.h: Change strchr inline for strange gcc problem.

View File

@ -59,7 +59,7 @@ openlog (const char *ident, int logopt, int facility)
debug_printf ("openlog called with (%s, %d, %d)", debug_printf ("openlog called with (%s, %d, %d)",
ident ? ident : "<NULL>", logopt, facility); ident ? ident : "<NULL>", logopt, facility);
if (process_ident != 0) if (process_ident != NULL)
{ {
free (process_ident); free (process_ident);
process_ident = 0; process_ident = 0;
@ -122,6 +122,7 @@ class pass_handler
int print (const char *,...); int print (const char *,...);
int print_va (const char *, va_list); int print_va (const char *, va_list);
char *get_message () const { return message_; } char *get_message () const { return message_; }
void set_message (char *s) { message_ = s; }
}; };
pass_handler::pass_handler () : fp_ (0), message_ (0), total_len_ (0) pass_handler::pass_handler () : fp_ (0), message_ (0), total_len_ (0)
@ -137,39 +138,27 @@ pass_handler::~pass_handler ()
void void
pass_handler::shutdown () pass_handler::shutdown ()
{ {
if (fp_ != 0) if (fp_ != NULL)
{ {
fclose (fp_); fclose (fp_);
fp_ = 0; fp_ = 0;
} }
if (message_ != 0)
delete[] message_;
} }
int int
pass_handler::initialize (int pass_number) pass_handler::initialize (int pass_number)
{ {
shutdown (); shutdown ();
if (pass_number == 0) if (pass_number)
{ return total_len_ + 1;
fp_ = fopen ("/dev/null", "wb"); fp_ = fopen ("/dev/null", "wb");
if (fp_ == 0) if (fp_ == NULL)
{ {
debug_printf ("failed to open /dev/null"); debug_printf ("failed to open /dev/null");
return -1; return -1;
} }
total_len_ = 0; total_len_ = 0;
}
else
{
message_ = new char[total_len_ + 1];
if (message_ == 0)
{
debug_printf ("failed to allocate message_");
return -1;
}
message_[0] = '\0';
}
return 0; return 0;
} }
@ -236,9 +225,10 @@ syslog (int priority, const char *message, ...)
if (*cp == '%' && cp[1] == 'm') if (*cp == '%' && cp[1] == 'm')
numfound++; numfound++;
char *newmessage = new char [strlen (message) + (errlen * numfound)]; char *newmessage = (char *) alloca (strlen (message) +
(errlen * numfound) + 1);
if (newmessage == 0) if (newmessage == NULL)
{ {
debug_printf ("failed to allocate newmessage"); debug_printf ("failed to allocate newmessage");
return; return;
@ -283,14 +273,16 @@ syslog (int priority, const char *message, ...)
output, then do it again to a malloc'ed string. This output, then do it again to a malloc'ed string. This
is ugly, slow, but prevents core dumps :-). is ugly, slow, but prevents core dumps :-).
*/ */
int pass_number = 0;
va_list ap; va_list ap;
pass_handler pass; pass_handler pass;
for (; pass_number < 2; ++pass_number) for (int pass_number = 0; pass_number < 2; ++pass_number)
{ {
if (pass.initialize (pass_number) == -1) int n = pass.initialize (pass_number);
if (n == -1)
return; return;
else if (n > 0)
pass.set_message ((char *) alloca (n));
/* Deal with ident_string */ /* Deal with ident_string */
if (process_ident != 0) if (process_ident != 0)
@ -369,7 +361,7 @@ syslog (int priority, const char *message, ...)
interleaved, we must lock the first byte of the file interleaved, we must lock the first byte of the file
This works on Win32 even if we created the file above. This works on Win32 even if we created the file above.
*/ */
HANDLE fHandle = dtable[fileno (fp)]->get_handle (); HANDLE fHandle = fdtab[fileno (fp)]->get_handle ();
if (LockFile (fHandle, 0, 0, 1, 0) == FALSE) if (LockFile (fHandle, 0, 0, 1, 0) == FALSE)
{ {
debug_printf ("failed to lock file %s", get_win95_event_log_path()); debug_printf ("failed to lock file %s", get_win95_event_log_path());