Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
vsnprintf.c File Reference
#include "config.h"
#include <sys/param.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <varargs.h>
#include <setjmp.h>

Go to the source code of this file.

Defines

#define roundup(x, y)   ((((x)+((y)-1))/(y))*(y))
#define EXTRABYTES   2 /* XXX: why 2? you don't want to know */

Functions

static char * msetup (char *str, size_t n)
static void mcatch (int i)
static void mcleanup (char *str, size_t n, char *p)
int vsnprintf (char *str, size_t n, char *fmt, char *ap)
int snprintf (char *str, size_t n, char *fmt, va_alist)

Variables

static int pgsize
static char * curobj
static sigjmp_buf bail

Define Documentation

#define EXTRABYTES   2 /* XXX: why 2? you don't want to know */

Definition at line 56 of file vsnprintf.c.

#define roundup (   x,
  y 
)    ((((x)+((y)-1))/(y))*(y))

Definition at line 49 of file vsnprintf.c.


Function Documentation

static void mcatch ( int  i) [static]

Definition at line 85 of file vsnprintf.c.

{
       siglongjmp(bail, 1);
}

Here is the caller graph for this function:

static void mcleanup ( char *  str,
size_t  n,
char *  p 
) [static]

Definition at line 91 of file vsnprintf.c.

{
       strncpy(str, p, n-1);
       str[n-1] = '\0';
       if (mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
           PROT_READ|PROT_WRITE|PROT_EXEC) == -1)
              mprotect((caddr_t)(p + n + EXTRABYTES), pgsize,
                  PROT_READ|PROT_WRITE);
       free(curobj);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static char* msetup ( char *  str,
size_t  n 
) [static]

Definition at line 59 of file vsnprintf.c.

{
       char *e;

       if (n == 0)
              return NULL;
       if (pgsize == 0)
              pgsize = getpagesize();
       curobj = (char *)malloc(n + EXTRABYTES + pgsize * 2);
       if (curobj == NULL)
              return NULL;
       e = curobj + n + EXTRABYTES;
       e = (char *)roundup((unsigned long)e, pgsize);
       if (mprotect(e, pgsize, PROT_NONE) == -1) {
              free(curobj);
              curobj = NULL;
              return NULL;
       }
       e = e - n - EXTRABYTES;
       *e = '\0';
       return (e);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int snprintf ( char *  str,
size_t  n,
char *  fmt,
va_alist   
)

Definition at line 145 of file vsnprintf.c.

{
       va_list ap;
#if __STDC__
       va_start(ap, fmt);
#else
       va_start(ap);
#endif

       return (vsnprintf(str, n, fmt, ap));
       va_end(ap);
}

Here is the call graph for this function:

int vsnprintf ( char *  str,
size_t  n,
char *  fmt,
char *  ap 
)

Definition at line 109 of file vsnprintf.c.

{
       struct sigaction osa, nsa;
       char *p;
       int ret = n + 1;     /* if we bail, indicated we overflowed */

       memset(&nsa, 0, sizeof nsa);
       nsa.sa_handler = mcatch;
       sigemptyset(&nsa.sa_mask);

       p = msetup(str, n);
       if (p == NULL) {
              *str = '\0';
              return 0;
       }
       if (sigsetjmp(bail, 1) == 0) {
              if (sigaction(SIGSEGV, &nsa, &osa) == -1) {
                     mcleanup(str, n, p);
                     return (0);
              }
              ret = vsprintf(p, fmt, ap);
       }
       mcleanup(str, n, p);
       (void) sigaction(SIGSEGV, &osa, NULL);
       return (ret);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

sigjmp_buf bail [static]

Definition at line 54 of file vsnprintf.c.

char* curobj [static]

Definition at line 53 of file vsnprintf.c.

int pgsize [static]

Definition at line 52 of file vsnprintf.c.