security/nss/lib/base/error.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/base/error.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,269 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * error.c
    1.10 + *
    1.11 + * This file contains the code implementing the per-thread error 
    1.12 + * stacks upon which most NSS routines report their errors.
    1.13 + */
    1.14 +
    1.15 +#ifndef BASE_H
    1.16 +#include "base.h"
    1.17 +#endif /* BASE_H */
    1.18 +#include <limits.h> /* for UINT_MAX */
    1.19 +#include <string.h> /* for memmove */
    1.20 +
    1.21 +#define NSS_MAX_ERROR_STACK_COUNT 16 /* error codes */
    1.22 +
    1.23 +/*
    1.24 + * The stack itself has a header, and a sequence of integers.
    1.25 + * The header records the amount of space (as measured in stack
    1.26 + * slots) already allocated for the stack, and the count of the
    1.27 + * number of records currently being used.
    1.28 + */
    1.29 +
    1.30 +struct stack_header_str {
    1.31 +  PRUint16 space;
    1.32 +  PRUint16 count;
    1.33 +};
    1.34 +
    1.35 +struct error_stack_str {
    1.36 +  struct stack_header_str header;
    1.37 +  PRInt32 stack[1];
    1.38 +};
    1.39 +typedef struct error_stack_str error_stack;
    1.40 +
    1.41 +/*
    1.42 + * error_stack_index
    1.43 + *
    1.44 + * Thread-private data must be indexed.  This is that index.
    1.45 + * See PR_NewThreadPrivateIndex for more information.
    1.46 + *
    1.47 + * Thread-private data indexes are in the range [0, 127].
    1.48 + */
    1.49 +
    1.50 +#define INVALID_TPD_INDEX UINT_MAX
    1.51 +static PRUintn error_stack_index = INVALID_TPD_INDEX;
    1.52 +
    1.53 +/*
    1.54 + * call_once
    1.55 + *
    1.56 + * The thread-private index must be obtained (once!) at runtime.
    1.57 + * This block is used for that one-time call.
    1.58 + */
    1.59 +
    1.60 +static PRCallOnceType error_call_once;
    1.61 +
    1.62 +/*
    1.63 + * error_once_function
    1.64 + *
    1.65 + * This is the once-called callback.
    1.66 + */
    1.67 +static PRStatus
    1.68 +error_once_function ( void)
    1.69 +{
    1.70 +  return PR_NewThreadPrivateIndex(&error_stack_index, PR_Free);
    1.71 +}
    1.72 +
    1.73 +/*
    1.74 + * error_get_my_stack
    1.75 + *
    1.76 + * This routine returns the calling thread's error stack, creating
    1.77 + * it if necessary.  It may return NULL upon error, which implicitly
    1.78 + * means that it ran out of memory.
    1.79 + */
    1.80 +
    1.81 +static error_stack *
    1.82 +error_get_my_stack ( void)
    1.83 +{
    1.84 +  PRStatus st;
    1.85 +  error_stack *rv;
    1.86 +  PRUintn new_size;
    1.87 +  PRUint32 new_bytes;
    1.88 +  error_stack *new_stack;
    1.89 +
    1.90 +  if( INVALID_TPD_INDEX == error_stack_index ) {
    1.91 +    st = PR_CallOnce(&error_call_once, error_once_function);
    1.92 +    if( PR_SUCCESS != st ) {
    1.93 +      return (error_stack *)NULL;
    1.94 +    }
    1.95 +  }
    1.96 +
    1.97 +  rv = (error_stack *)PR_GetThreadPrivate(error_stack_index);
    1.98 +  if( (error_stack *)NULL == rv ) {
    1.99 +    /* Doesn't exist; create one */
   1.100 +    new_size = 16;
   1.101 +  } else if( rv->header.count == rv->header.space  &&
   1.102 +             rv->header.count  < NSS_MAX_ERROR_STACK_COUNT ) {
   1.103 +    /* Too small, expand it */
   1.104 +    new_size = PR_MIN( rv->header.space * 2, NSS_MAX_ERROR_STACK_COUNT);
   1.105 +  } else {
   1.106 +    /* Okay, return it */
   1.107 +    return rv;
   1.108 +  }
   1.109 +
   1.110 +  new_bytes = (new_size * sizeof(PRInt32)) + sizeof(error_stack);
   1.111 +  /* Use NSPR's calloc/realloc, not NSS's, to avoid loops! */
   1.112 +  new_stack = PR_Calloc(1, new_bytes);
   1.113 +  
   1.114 +  if( (error_stack *)NULL != new_stack ) {
   1.115 +    if( (error_stack *)NULL != rv ) {
   1.116 +	(void)nsslibc_memcpy(new_stack,rv,rv->header.space);
   1.117 +    }
   1.118 +    new_stack->header.space = new_size;
   1.119 +  }
   1.120 +
   1.121 +  /* Set the value, whether or not the allocation worked */
   1.122 +  PR_SetThreadPrivate(error_stack_index, new_stack);
   1.123 +  return new_stack;
   1.124 +}
   1.125 +
   1.126 +/*
   1.127 + * The error stack
   1.128 + *
   1.129 + * The public methods relating to the error stack are:
   1.130 + *
   1.131 + *  NSS_GetError
   1.132 + *  NSS_GetErrorStack
   1.133 + *
   1.134 + * The nonpublic methods relating to the error stack are:
   1.135 + *
   1.136 + *  nss_SetError
   1.137 + *  nss_ClearErrorStack
   1.138 + *
   1.139 + */
   1.140 +
   1.141 +/*
   1.142 + * NSS_GetError
   1.143 + *
   1.144 + * This routine returns the highest-level (most general) error set
   1.145 + * by the most recent NSS library routine called by the same thread
   1.146 + * calling this routine.
   1.147 + *
   1.148 + * This routine cannot fail.  However, it may return zero, which
   1.149 + * indicates that the previous NSS library call did not set an error.
   1.150 + *
   1.151 + * Return value:
   1.152 + *  0 if no error has been set
   1.153 + *  A nonzero error number
   1.154 + */
   1.155 +
   1.156 +NSS_IMPLEMENT PRInt32
   1.157 +NSS_GetError ( void)
   1.158 +{
   1.159 +  error_stack *es = error_get_my_stack();
   1.160 +
   1.161 +  if( (error_stack *)NULL == es ) {
   1.162 +    return NSS_ERROR_NO_MEMORY; /* Good guess! */
   1.163 +  }
   1.164 +
   1.165 +  if( 0 == es->header.count ) {
   1.166 +    return 0;
   1.167 +  }
   1.168 +
   1.169 +  return es->stack[ es->header.count-1 ];
   1.170 +}
   1.171 +
   1.172 +/*
   1.173 + * NSS_GetErrorStack
   1.174 + *
   1.175 + * This routine returns a pointer to an array of integers, containing
   1.176 + * the entire sequence or "stack" of errors set by the most recent NSS
   1.177 + * library routine called by the same thread calling this routine.
   1.178 + * NOTE: the caller DOES NOT OWN the memory pointed to by the return
   1.179 + * value.  The pointer will remain valid until the calling thread
   1.180 + * calls another NSS routine.  The lowest-level (most specific) error 
   1.181 + * is first in the array, and the highest-level is last.  The array is
   1.182 + * zero-terminated.  This routine may return NULL upon error; this
   1.183 + * indicates a low-memory situation.
   1.184 + *
   1.185 + * Return value:
   1.186 + *  NULL upon error, which is an implied NSS_ERROR_NO_MEMORY
   1.187 + *  A NON-caller-owned pointer to an array of integers
   1.188 + */
   1.189 +
   1.190 +NSS_IMPLEMENT PRInt32 *
   1.191 +NSS_GetErrorStack ( void)
   1.192 +{
   1.193 +  error_stack *es = error_get_my_stack();
   1.194 +
   1.195 +  if( (error_stack *)NULL == es ) {
   1.196 +    return (PRInt32 *)NULL;
   1.197 +  }
   1.198 +
   1.199 +  /* Make sure it's terminated */
   1.200 +  es->stack[ es->header.count ] = 0;
   1.201 +
   1.202 +  return es->stack;
   1.203 +}
   1.204 +
   1.205 +/*
   1.206 + * nss_SetError
   1.207 + *
   1.208 + * This routine places a new error code on the top of the calling 
   1.209 + * thread's error stack.  Calling this routine wiht an error code
   1.210 + * of zero will clear the error stack.
   1.211 + */
   1.212 +
   1.213 +NSS_IMPLEMENT void
   1.214 +nss_SetError ( PRUint32 error)
   1.215 +{
   1.216 +  error_stack *es;
   1.217 +
   1.218 +  if( 0 == error ) {
   1.219 +    nss_ClearErrorStack();
   1.220 +    return;
   1.221 +  }
   1.222 +
   1.223 +  es = error_get_my_stack();
   1.224 +  if( (error_stack *)NULL == es ) {
   1.225 +    /* Oh, well. */
   1.226 +    return;
   1.227 +  }
   1.228 +
   1.229 +  if (es->header.count < es->header.space) {
   1.230 +    es->stack[ es->header.count++ ] = error;
   1.231 +  } else {
   1.232 +    memmove(es->stack, es->stack + 1, 
   1.233 +		(es->header.space - 1) * (sizeof es->stack[0]));
   1.234 +    es->stack[ es->header.space - 1 ] = error;
   1.235 +  }
   1.236 +  return;
   1.237 +}
   1.238 +
   1.239 +/*
   1.240 + * nss_ClearErrorStack
   1.241 + *
   1.242 + * This routine clears the calling thread's error stack.
   1.243 + */
   1.244 +
   1.245 +NSS_IMPLEMENT void
   1.246 +nss_ClearErrorStack ( void)
   1.247 +{
   1.248 +  error_stack *es = error_get_my_stack();
   1.249 +  if( (error_stack *)NULL == es ) {
   1.250 +    /* Oh, well. */
   1.251 +    return;
   1.252 +  }
   1.253 +
   1.254 +  es->header.count = 0;
   1.255 +  es->stack[0] = 0;
   1.256 +  return;
   1.257 +}
   1.258 +
   1.259 +/*
   1.260 + * nss_DestroyErrorStack
   1.261 + *
   1.262 + * This routine frees the calling thread's error stack.
   1.263 + */
   1.264 +
   1.265 +NSS_IMPLEMENT void
   1.266 +nss_DestroyErrorStack ( void)
   1.267 +{
   1.268 +  if( INVALID_TPD_INDEX != error_stack_index ) {
   1.269 +    PR_SetThreadPrivate(error_stack_index, NULL);
   1.270 +  }
   1.271 +  return;
   1.272 +}

mercurial