Restructured the compat functions and changed its license.

New fucntion xstrconcat for future use.
This commit is contained in:
Werner Koch 2009-08-25 20:00:24 +00:00
parent f5f0171d55
commit 20fe42d10b
11 changed files with 193 additions and 24 deletions

View File

@ -1,3 +1,7 @@
2009-08-25 Werner Koch <wk@g10code.com>
* compat.h: Add xstrconcat.
2009-08-11 David Shaw <dshaw@jabberwocky.com>
* util.h: Add string_to_utf8() from GPA.

View File

@ -15,4 +15,11 @@ int ascii_strncasecmp( const char *a, const char *b, size_t n);
char *strsep (char **stringp, const char *delim);
#endif
#if __GNUC__ >= 4
char *xstrconcat (const char *s1, ...) __attribute__ ((sentinel(0)));
#else
char *xstrconcat (const char *s1, ...);
#endif
#endif /* !_COMPAT_H_ */

View File

@ -1,3 +1,11 @@
2009-08-25 Werner Koch <wk@g10code.com>
* ksmalloc.c: New
(xtrymalloc, xfree): New.
* Makefile.am (gpgkeys_ldap_SOURCES, gpgkeys_curl_SOURCES):
(gpgkeys_hkp_SOURCES): Add ksmalloc.c.
* gpgkeys_hkp.c, gpgkeys_ldap.c: s/malloc/xtrymalloc/.
2009-07-06 David Shaw <dshaw@jabberwocky.com>
* gpgkeys_hkp.c (main, srv_replace): Minor tweaks to use the

View File

@ -27,10 +27,10 @@ gpglibexec_PROGRAMS = @GPGKEYS_LDAP@ @GPGKEYS_HKP@ @GPGKEYS_FINGER@ @GPGKEYS_CUR
gpglibexec_SCRIPTS = @GPGKEYS_MAILTO@
noinst_SCRIPTS = gpgkeys_test
gpgkeys_ldap_SOURCES = gpgkeys_ldap.c ksutil.c ksutil.h
gpgkeys_hkp_SOURCES = gpgkeys_hkp.c ksutil.c ksutil.h
gpgkeys_ldap_SOURCES = gpgkeys_ldap.c ksutil.c ksutil.h ksmalloc.c
gpgkeys_hkp_SOURCES = gpgkeys_hkp.c ksutil.c ksutil.h ksmalloc.c
gpgkeys_finger_SOURCES = gpgkeys_finger.c ksutil.c ksutil.h
gpgkeys_curl_SOURCES = gpgkeys_curl.c ksutil.c ksutil.h
gpgkeys_curl_SOURCES = gpgkeys_curl.c ksutil.c ksutil.h ksmalloc.c
other_libs = $(LIBICONV) $(LIBINTL) $(CAPLIBS)

View File

@ -288,7 +288,7 @@ curl_easy_escape(CURL *curl,char *str,int length)
else
len=strlen(str);
enc=malloc(len+1);
enc = xtrymalloc(len+1);
if(!enc)
return enc;

View File

@ -106,7 +106,7 @@ send_key(int *eof)
char *key=NULL,*encoded_key=NULL;
size_t keysize=1;
key=malloc(1);
key = xtrymalloc(1);
if(!key)
{
fprintf(console,"gpgkeys: unable to allocate memory for key\n");
@ -179,7 +179,7 @@ send_key(int *eof)
free(key);
key=malloc(8+strlen(encoded_key)+1);
key=xtrymalloc(8+strlen(encoded_key)+1);
if(!key)
{
fprintf(console,"gpgkeys: out of memory\n");
@ -324,7 +324,7 @@ get_name(const char *getkey)
goto fail;
}
request=malloc(MAX_URL+60+strlen(searchkey_encoded));
request=xtrymalloc(MAX_URL+60+strlen(searchkey_encoded));
if(!request)
{
fprintf(console,"gpgkeys: out of memory\n");
@ -408,7 +408,7 @@ search_key(const char *searchkey)
goto fail;
}
request=malloc(MAX_URL+60+strlen(searchkey_encoded));
request=xtrymalloc(MAX_URL+60+strlen(searchkey_encoded));
if(!request)
{
fprintf(console,"gpgkeys: out of memory\n");
@ -519,7 +519,7 @@ srv_replace(const char *srvtag)
char *newname,*newport;
newname=strdup(srvlist->target);
newport=malloc(MAX_PORT);
newport=xtrymalloc(MAX_PORT);
if(newname && newport)
{
free(opt->host);
@ -803,7 +803,7 @@ main(int argc,char *argv[])
if(line[0]=='\n' || line[0]=='\0')
break;
work=malloc(sizeof(struct keylist));
work=xtrymalloc(sizeof(struct keylist));
if(work==NULL)
{
fprintf(console,"gpgkeys: out of memory while "
@ -909,7 +909,7 @@ main(int argc,char *argv[])
keyptr=keyptr->next;
}
searchkey=malloc(len+1);
searchkey=xtrymalloc(len+1);
if(searchkey==NULL)
{
ret=KEYSERVER_NO_MEMORY;

View File

@ -147,7 +147,7 @@ key_in_keylist(const char *key,struct keylist *list)
static int
add_key_to_keylist(const char *key,struct keylist **list)
{
struct keylist *keyptr=malloc(sizeof(struct keylist));
struct keylist *keyptr=xtrymalloc(sizeof(struct keylist));
if(keyptr==NULL)
{
@ -320,14 +320,14 @@ make_one_attr(LDAPMod ***modlist,char *attr,const char *value)
return 0;
*modlist=grow;
grow[nummods]=malloc(sizeof(LDAPMod));
grow[nummods]=xtrymalloc(sizeof(LDAPMod));
if(!grow[nummods])
return 0;
grow[nummods]->mod_op=LDAP_MOD_REPLACE;
grow[nummods]->mod_type=attr;
if(value)
{
grow[nummods]->mod_values=malloc(sizeof(char *)*2);
grow[nummods]->mod_values=xtrymalloc(sizeof(char *)*2);
if(!grow[nummods]->mod_values)
{
grow[nummods]=NULL;
@ -586,7 +586,7 @@ send_key(int *eof)
char keyid[17],state[6];
LDAPMod **modlist,**addlist,**ml;
modlist=malloc(sizeof(LDAPMod *));
modlist=xtrymalloc(sizeof(LDAPMod *));
if(!modlist)
{
fprintf(console,"gpgkeys: can't allocate memory for keyserver record\n");
@ -596,7 +596,7 @@ send_key(int *eof)
*modlist=NULL;
addlist=malloc(sizeof(LDAPMod *));
addlist=xtrymalloc(sizeof(LDAPMod *));
if(!addlist)
{
fprintf(console,"gpgkeys: can't allocate memory for keyserver record\n");
@ -647,7 +647,7 @@ send_key(int *eof)
goto fail;
}
dn=malloc(strlen("pgpCertID=")+16+1+strlen(basekeyspacedn)+1);
dn=xtrymalloc(strlen("pgpCertID=")+16+1+strlen(basekeyspacedn)+1);
if(dn==NULL)
{
fprintf(console,"gpgkeys: can't allocate memory for keyserver record\n");
@ -657,7 +657,7 @@ send_key(int *eof)
sprintf(dn,"pgpCertID=%s,%s",keyid,basekeyspacedn);
key=malloc(1);
key=xtrymalloc(1);
if(!key)
{
fprintf(console,"gpgkeys: unable to allocate memory for key\n");
@ -812,7 +812,7 @@ send_key_keyserver(int *eof)
attrs[0]=&mod;
attrs[1]=NULL;
dn=malloc(strlen("pgpCertid=virtual,")+strlen(basekeyspacedn)+1);
dn=xtrymalloc(strlen("pgpCertid=virtual,")+strlen(basekeyspacedn)+1);
if(dn==NULL)
{
fprintf(console,"gpgkeys: can't allocate memory for keyserver record\n");
@ -823,7 +823,7 @@ send_key_keyserver(int *eof)
strcpy(dn,"pgpCertid=virtual,");
strcat(dn,basekeyspacedn);
key[0]=malloc(1);
key[0]=xtrymalloc(1);
if(key[0]==NULL)
{
fprintf(console,"gpgkeys: unable to allocate memory for key\n");
@ -1308,7 +1308,7 @@ search_key(const char *searchkey)
"pgpkeysize","pgpkeytype",NULL};
enum ks_search_type search_type;
search=malloc(2+1+9+1+3+strlen(searchkey)+3+1+15+14+1+1+20);
search=xtrymalloc(2+1+9+1+3+strlen(searchkey)+3+1+15+14+1+1+20);
if(!search)
{
fprintf(console,"gpgkeys: out of memory when building search list\n");
@ -1671,7 +1671,7 @@ find_basekeyspacedn(void)
LDAPMessage *si_res;
char *object;
object=malloc(17+strlen(context[i])+1);
object=xtrymalloc(17+strlen(context[i])+1);
if(!object)
return -1;
@ -2042,7 +2042,7 @@ main(int argc,char *argv[])
if(line[0]=='\n' || line[0]=='\0')
break;
work=malloc(sizeof(struct keylist));
work=xtrymalloc(sizeof(struct keylist));
if(work==NULL)
{
fprintf(console,"gpgkeys: out of memory while "
@ -2326,7 +2326,7 @@ main(int argc,char *argv[])
keyptr=keyptr->next;
}
searchkey=malloc((len*3)+1);
searchkey=xtrymalloc((len*3)+1);
if(searchkey==NULL)
{
ret=KEYSERVER_NO_MEMORY;

32
keyserver/ksmalloc.c Normal file
View File

@ -0,0 +1,32 @@
/* ksmalloc.c - Walloc wrapper
* Copyright (C) 2009 Free Software Foundation, Inc.
*
* The origin of this code is GnuPG.
*
* This file is free software; as a special exception the author gives
* unlimited permission to copy and/or distribute it, with or without
* modifications, as long as this notice is preserved.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
#include <stdlib.h>
/* A wrapper around malloc because libcompat requires it. */
void *
xtrymalloc (size_t n)
{
return malloc (n);
}
/* A wrapper around free becuase we are used to it. */
void
xfree (void *p)
{
if (p)
free (p);
}

View File

@ -136,4 +136,9 @@ struct curl_writer_ctx
size_t curl_writer(const void *ptr,size_t size,size_t nmemb,void *cw_ctx);
void curl_writer_finalize(struct curl_writer_ctx *ctx);
/* -- From ksmalloc.c or ../include/memory.h -- */
void *xtrymalloc (size_t n);
void xfree (void *p);
#endif /* !_KSUTIL_H_ */

View File

@ -1,3 +1,9 @@
2009-08-25 Werner Koch <wk@g10code.com>
* compat.c: Change license to all-permissive. The GPL and the
exception does no make sense here.
(do_strconcat, xstrconcat): New.
2009-08-18 Werner Koch <wk@g10code.com>
* iobuf.c (fd_cache_close): Change debug printf format assuming

View File

@ -1,5 +1,42 @@
/* compat.c - Simple compatibility functions
* Copyright (C) 2006, 2007, 2009 Free Software Foundation, Inc.
*
* The origin of this code is GnuPG.
*
* This file is free software; as a special exception the author gives
* unlimited permission to copy and/or distribute it, with or without
* modifications, as long as this notice is preserved.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* History:
* 2006-09-28 dshaw Created. Added function hextobyte from GnuPG.
* 2007-04-16 dshaw Added ascii_toupper, ascii_tolower, ascii_strcasecmp,
* ascii_strncasecmp from GnuPG.
* 2009-08-25 wk License changed by GnuPG maintainer from GPL with
* OpenSSL exception to this all permissive license.
* 2009-08-25 wk Wrote new function xstrconcat.
*/
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
/* We require an external malloc function named xtrymalloc. */
void *xtrymalloc (size_t n);
#ifndef DIM
#define DIM(v) (sizeof(v)/sizeof((v)[0]))
#endif
int
hextobyte (const char *s)
{
@ -92,3 +129,73 @@ ascii_strncasecmp (const char *a, const char *b, size_t n)
return c1 - c2;
}
static char *
do_strconcat (const char *s1, va_list arg_ptr)
{
const char *argv[48];
size_t argc;
size_t needed;
char *buffer, *p;
const char *r;
argc = 0;
argv[argc++] = s1;
needed = strlen (s1);
while (((argv[argc] = va_arg (arg_ptr, const char *))))
{
needed += strlen (argv[argc]);
if (argc >= DIM (argv)-1)
{
errno = EINVAL;
return NULL;
}
argc++;
}
needed++;
buffer = xtrymalloc (needed);
if (buffer)
{
for (p = buffer, argc=0; argv[argc]; argc++)
{
for (r = argv[argc]; *r; )
*p++ = *r++;
*p = 0;
}
}
return buffer;
}
/* Concatenate the string S1 with all the following strings up to a
NULL. Returns a malloced buffer. */
char *
xstrconcat (const char *s1, ...)
{
va_list arg_ptr;
char *result;
if (!s1)
{
result = xtrymalloc (1);
if (result)
*result = 0;
}
else
{
va_start (arg_ptr, s1);
result = do_strconcat (s1, arg_ptr);
va_end (arg_ptr);
}
if (!result)
{
if (errno == EINVAL)
fputs ("\nfatal: too many args for xstrconcat\n", stderr);
else
fputs ("\nfatal: out of memory\n", stderr);
exit (2);
}
return result;
}