*(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10))
#define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1))
-#define safe_malloc( x ) malloc( x )
-#define xstrdup( x ) (x)?strdup(x):0
+static void *
+xmalloc (size_t n)
+{
+ char *p = malloc (n);
+ if (!p)
+ {
+ fputs ("\nfatal: out of core\n", stderr);
+ exit (4);
+ }
+ return p;
+}
+
+/* Please: Don't call an allocation function xfoo when it may return NULL. */
+/* Wrong: #define xstrdup( x ) (x)?strdup(x):0 */
+/* Right: */
+static char *
+xstrdup (const char *string)
+{
+ char *p = xmalloc (strlen (string));
+ strcpy (p, string);
+ return p;
+}
+
+
static void
safe_free( void** x )
free( *x );
*x = 0;
}
+
char *
trim_trailing_spaces( char *string )
{
n = s - string;
if (!n)
return NULL; /* empty key */
- array->key = p = safe_malloc (n+1);
+ array->key = p = xmalloc (n+1);
memcpy (p, string, n);
if (!n || (n & 1))
return NULL; /* empty or odd number of digits */
n /= 2;
- array->value = p = safe_malloc (n+1);
+ array->value = p = xmalloc (n+1);
for (s1=string; n; s1 += 2, n--)
n++;
}
- array->value = p = safe_malloc (n+1);
+ array->value = p = xmalloc (n+1);
for (s=string; n; s++, n--)
arraysize = 7; /* C,ST,L,O,OU,CN,email */
arrayidx = 0;
- array = safe_malloc ((arraysize+1) * sizeof *array);
+ array = xmalloc ((arraysize+1) * sizeof *array);
while (*string)
struct DnPair *a2;
arraysize += 5;
- a2 = safe_malloc ((arraysize+1) * sizeof *array);
+ a2 = xmalloc ((arraysize+1) * sizeof *array);
for (i=0; i < arrayidx; i++)
{
a2[i].key = array[i].key;
len += 4; /* ',' and '=', and possibly "(" and ")" */
}
}
- result = (char*)safe_malloc( (len+1)*sizeof(char) );
+ result = xmalloc( (len+1)*sizeof(char) );
*result = 0;
/* add standard parts */
const char* patterns[] = { pattern, NULL };
fprintf( stderr, "startListCertificates( \"%s\", %d )", pattern, remote );
- it = (struct CertIterator*)safe_malloc( sizeof( struct CertIterator ) );
+ it = xmalloc( sizeof( struct CertIterator ) );
err = gpgme_new (&(it->ctx));
/*fprintf( stderr, "2: gpgme returned %d\n", err );*/
{
int len = strlen(fpr);
int i = 0;
- char* result = safe_malloc( (len + len/2 + 1)*sizeof(char) );
+ char* result = xmalloc( (len + len/2 + 1)*sizeof(char) );
if( !result ) return NULL;
for(; *fpr; ++fpr, ++i ) {
if( i%3 == 2) {
names[idx] = xstrdup( s );
}
- it->info.userid = safe_malloc( sizeof( char* ) * (idx+1) );
+ it->info.userid = xmalloc( sizeof( char* ) * (idx+1) );
memset( it->info.userid, 0, sizeof( char* ) * (idx+1) );
it->info.dnarray = 0;
for( idx = 0; names[idx] != 0; ++idx ) {
it->info.userid[idx] = 0;
s = gpgme_key_get_string_attr (key, GPGME_ATTR_SERIAL, 0, 0);
- it->info.serial = xstrdup(s);
+ it->info.serial = s? xstrdup(s) : NULL;
s = gpgme_key_get_string_attr (key, GPGME_ATTR_FPR, 0, 0);
it->info.fingerprint = make_fingerprint( s );
it->info.issuer = NULL;
}
s = gpgme_key_get_string_attr (key, GPGME_ATTR_CHAINID, 0, 0);
- it->info.chainid = xstrdup(s);
+ it->info.chainid = s? xstrdup(s): NULL;
s = gpgme_key_get_string_attr (key, GPGME_ATTR_KEY_CAPS, 0, 0);
- it->info.caps = xstrdup(s);
+ it->info.caps = s? xstrdup(s) : NULL;
u = gpgme_key_get_ulong_attr (key, GPGME_ATTR_CREATED, 0, 0);
it->info.created = u;
return err;
}
- buf = safe_malloc( sizeof(char)*( strlen( fingerprint ) + 1 ) );
+ buf = malloc( sizeof(char)*( strlen( fingerprint ) + 1 ) );
if( !buf ) {
gpgme_recipients_release( recips );
gpgme_data_release( keydata );
return 0;
}
- // // // // // // // // // // // // // // // // // // // // // // // // //
+/* // // // // // // // // // // // // // // // // // // // // // // // // //
// //
// Continuation of CryptPlug code //
// //
-// // // // // // // // // // // // // // // // // // // // // // // // //
+// // // // // // // // // // // // // // // // // // // // // // // // //*/
/*
int* newSize,
bool secretOnly )
{
- static int maxCerts = 1024;
- // use const char declarations since all of them are needed twice
+#define MAXCERTS 1024;
+ /* use const char declarations since all of them are needed twice *.
const char* delimiter = "\1";
const char* openBracket = " (";
const char* closeBracket = ")";
int nFound = 0;
int iFound = 0;
int siz = 0;
- char* DNs[maxCerts];
- char* FPRs[maxCerts];
+ char* DNs[MAXCERTS];
+ char* FPRs[MAXCERTS];
if( ! certificates ){
fprintf( stderr, "gpgme: findCertificates called with invalid *certificates pointer\n" );
*certificates = 0;
*newSize = 0;
- // calculate length of buffer needed for certs plus fingerprints
- memset( DNs, 0, sizeof( DNs ) );
- memset( FPRs, 0, sizeof( FPRs ) );
+ /* calculate length of buffer needed for certs plus fingerprints */
gpgme_new (&ctx);
gpgme_set_protocol (ctx, GPGMEPLUG_PROTOCOL);
err = gpgme_op_keylist_start(ctx, addressee, secretOnly ? 1 : 0);
siz += strlen( s2 );
siz += strlen( closeBracket );
DNs[ nFound ] = dn;
+ dn = NULL;
FPRs[nFound ] = xstrdup( s2 );
++nFound;
if( nFound >= maxCerts ) {
fprintf( stderr,
"gpgme: findCertificates found too many certificates (%d)\n",
- maxCerts );
+ MAXCERTS );
break;
}
}
if( 0 < siz ) {
- // add one for trailing ZERO char
+ /* add one for trailing ZERO char */
++siz;
*newSize = siz;
- // allocate the buffer
- *certificates = malloc( sizeof(char) * siz );
+ /* allocate the buffer */
+ *certificates = xmalloc( sizeof(char) * siz );
memset( *certificates, 0, sizeof(char) * siz );
- // fill the buffer
+ /* fill the buffer */
for( iFound=0; iFound < nFound; ++iFound ) {
if( !iFound )
strcpy(*certificates, DNs[iFound] );