more bsd tweaks

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@32485 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2011-03-07 15:34:06 +00:00
parent dbaa646d12
commit dbf4119553
19 changed files with 91 additions and 68 deletions

View file

@ -1,25 +1,36 @@
2011-03-06 Richard Frith-Macdonald <rfm@gnu.org> 2011-03-06 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSPortCoder.m:
* Source/NSKeyValueCoding.m:
* Source/NSPathUtilities.m:
* Source/NSProcessInfo.m:
* Source/NSMethodSignature.m:
* Source/NSMessagePort.m:
* Source/NSInvocation.m:
* Source/dld-load.h:
* Source/NSFileManager.m:
* Source/NSURL.m:
* Source/NSString.m:
* Source/Additions/Unicode.m:
* Source/Additions/NSError+GNUstepBase.m:
* Source/Additions/GSXML.m:
* Source/Additions/GSObjCRuntime.m: * Source/Additions/GSObjCRuntime.m:
* Source/NSData.m: * Source/Additions/GSXML.m:
* Source/NSHost.m: * Source/Additions/NSData+GNUstepBase.m:
* Source/Additions/NSError+GNUstepBase.m:
* Source/Additions/Unicode.m:
* Source/dld-load.h:
* Source/GSFormat.m:
* Source/GSMDNSNetServices.m: * Source/GSMDNSNetServices.m:
* Source/GSSocketStream.m:
* Source/GSValue.m: * Source/GSValue.m:
* Source/NSArchiver.m:
* Source/NSCalendarDate.m:
* Source/NSData.m:
* Source/NSDebug.m:
* Source/NSFileManager.m:
* Source/NSHost.m:
* Source/NSInvocation.m:
* Source/NSKeyValueCoding.m:
* Source/NSMessagePort.m:
* Source/NSMessagePortNameServer.m:
* Source/NSMethodSignature.m:
* Source/NSObject.m:
* Source/NSPathUtilities.m:
* Source/NSPortCoder.m:
* Source/NSProcessInfo.m:
* Source/NSString.m:
* Source/NSTask.m: * Source/NSTask.m:
* Source/NSTimeZone.m:
* Source/NSUnarchiver.m:
* Source/NSURL.m:
* Tools/cvtenc.m:
* Tools/defaults.m: * Tools/defaults.m:
* Tools/locale_alias.m: * Tools/locale_alias.m:
Avoid strcpy and strcat to get rid if some bsd warnings. Avoid strcpy and strcat to get rid if some bsd warnings.

View file

@ -4340,7 +4340,7 @@ static BOOL warned = NO; if (warned == NO) { warned = YES; NSLog(@"WARNING, use
to[j++] = '&'; to[j++] = '&';
to[j++] = '#'; to[j++] = '#';
sprintf(buf, "%u", c); snprintf(buf, sizeof(buf), "%u", c);
while (*ptr != '\0') while (*ptr != '\0')
{ {
to[j++] = *ptr++; to[j++] = *ptr++;

View file

@ -629,7 +629,7 @@ static void MD5Transform (uint32_t buf[4], uint32_t const in[16])
/* /*
* The header is a line of the form 'begin mode filename' * The header is a line of the form 'begin mode filename'
*/ */
sprintf((char*)buf, "begin %03o ", (int)mode); snprintf((char*)buf, sizeof(buf), "begin %03o ", (int)mode);
[encoded appendBytes: buf length: strlen((const char*)buf)]; [encoded appendBytes: buf length: strlen((const char*)buf)];
[encoded appendData: [name dataUsingEncoding: NSASCIIStringEncoding]]; [encoded appendData: [name dataUsingEncoding: NSASCIIStringEncoding]];
[encoded appendBytes: "\n" length: 1]; [encoded appendBytes: "\n" length: 1];

View file

@ -137,7 +137,7 @@ strerror_r(int eno, char *buf, int len)
domain = NSPOSIXErrorDomain; domain = NSPOSIXErrorDomain;
if (strerror_r(code, buf, BUFSIZ) < 0) if (strerror_r(code, buf, BUFSIZ) < 0)
{ {
sprintf(buf, "%ld", code); snprintf(buf, sizeof(buf), "%ld", code);
} }
message = [NSString stringWithCString: buf message = [NSString stringWithCString: buf
encoding: [NSString defaultCStringEncoding]]; encoding: [NSString defaultCStringEncoding]];

View file

@ -783,7 +783,7 @@ static NSString *locale_sep()
char *from = buf; char *from = buf;
char *to; char *to;
sprintf(buf, "%g", 1.2); snprintf(buf, sizeof(buf), "%g", 1.2);
if (*from == '1') from++; if (*from == '1') from++;
to = from; to = from;
while (*to != '\0' && *to != '2') while (*to != '\0' && *to != '2')
@ -1463,12 +1463,12 @@ NSDictionary *locale)
if (specs[nspecs_done].info.is_long_double) if (specs[nspecs_done].info.is_long_double)
{ {
sprintf(buf2, buf1, snprintf(buf2, sizeof(buf2), buf1,
args_value[specs[nspecs_done].data_arg].pa_long_double); args_value[specs[nspecs_done].data_arg].pa_long_double);
} }
else else
{ {
sprintf(buf2, buf1, snprintf(buf2, sizeof(buf2), buf1,
args_value[specs[nspecs_done].data_arg].pa_double); args_value[specs[nspecs_done].data_arg].pa_double);
} }
@ -1581,12 +1581,12 @@ NSDictionary *locale)
if (specs[nspecs_done].info.is_long_double) if (specs[nspecs_done].info.is_long_double)
{ {
sprintf(buf2, buf1, snprintf(buf2, sizeof(buf2), buf1,
args_value[specs[nspecs_done].data_arg].pa_long_double); args_value[specs[nspecs_done].data_arg].pa_long_double);
} }
else else
{ {
sprintf(buf2, buf1, snprintf(buf2, sizeof(buf2), buf1,
args_value[specs[nspecs_done].data_arg].pa_double); args_value[specs[nspecs_done].data_arg].pa_double);
} }

View file

@ -1162,7 +1162,8 @@ static void DNSSD_API
struct sockaddr_in ip4; struct sockaddr_in ip4;
// oogly // oogly
sprintf(rdb, "%d.%d.%d.%d", rd[0], rd[1], rd[2], rd[3]); snprintf(rdb, sizeof(rdb),
"%d.%d.%d.%d", rd[0], rd[1], rd[2], rd[3]);
LOG(@"Found IPv4 <%s> on port %d", rdb, ntohs(service->port)); LOG(@"Found IPv4 <%s> on port %d", rdb, ntohs(service->port));
length = sizeof (struct sockaddr_in); length = sizeof (struct sockaddr_in);
@ -1183,11 +1184,12 @@ static void DNSSD_API
struct sockaddr_in6 ip6; struct sockaddr_in6 ip6;
// Even more oogly // Even more oogly
sprintf(rdb, "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x", snprintf(rdb, sizeof(rdb),
rd[0], rd[1], rd[2], rd[3], "%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x:%x%x",
rd[4], rd[5], rd[6], rd[7], rd[0], rd[1], rd[2], rd[3],
rd[8], rd[9], rd[10], rd[11], rd[4], rd[5], rd[6], rd[7],
rd[12], rd[13], rd[14], rd[15]); rd[8], rd[9], rd[10], rd[11],
rd[12], rd[13], rd[14], rd[15]);
LOG(@"Found IPv6 <%s> on port %d", rdb, ntohs(service->port)); LOG(@"Found IPv6 <%s> on port %d", rdb, ntohs(service->port));
length = sizeof (struct sockaddr_in6); length = sizeof (struct sockaddr_in6);

View file

@ -1176,7 +1176,7 @@ static NSString * const GSSOCKSAckConn = @"GSSOCKSAckConn";
{ {
buf[j++] = ':'; buf[j++] = ':';
} }
sprintf((char*)&buf[j], "%04x", val); snprintf((char*)&buf[j], 5, "%04x", val);
j += 4; j += 4;
} }
a = [NSString stringWithUTF8String: a = [NSString stringWithUTF8String:

View file

@ -1019,8 +1019,8 @@ static Class NSMutableDataMallocClass;
char header[headerLength+1]; char header[headerLength+1];
unsigned dataLength = [_data length]; unsigned dataLength = [_data length];
sprintf(header, "%s%08x:%08x:%08x:%08x:", PREFIX, systemVersion, classCount, snprintf(header, sizeof(header), "%s%08x:%08x:%08x:%08x:",
objectCount, pointerCount); PREFIX, systemVersion, classCount, objectCount, pointerCount);
if (positionInData + headerLength <= dataLength) if (positionInData + headerLength <= dataLength)
{ {

View file

@ -1864,7 +1864,7 @@ static void outputValueWithFormat(int v, char *fldfmt, DescriptionInfo *info)
char cbuf[MAX_FLD_WIDTH + 1]; char cbuf[MAX_FLD_WIDTH + 1];
int idx = 0; int idx = 0;
sprintf((char*)cbuf, fldfmt, v); snprintf((char*)cbuf, sizeof(cbuf), fldfmt, v);
Grow(info, strlen((char*)cbuf)); Grow(info, strlen((char*)cbuf));
while (cbuf[idx] != '\0') while (cbuf[idx] != '\0')
{ {

View file

@ -494,7 +494,7 @@ _GSDebugAllocationList(BOOL difference)
} }
if (val != 0) if (val != 0)
{ {
pos += 11 + strlen(class_getName(the_table[i].class)); pos += 22 + strlen(class_getName(the_table[i].class));
} }
} }
if (pos == 0) if (pos == 0)
@ -540,7 +540,8 @@ _GSDebugAllocationList(BOOL difference)
if (val != 0) if (val != 0)
{ {
sprintf(&buf[pos], "%d\t%s\n", val, class_getName(the_table[i].class)); snprintf(&buf[pos], siz - pos, "%d\t%s\n",
val, class_getName(the_table[i].class));
pos += strlen(&buf[pos]); pos += strlen(&buf[pos]);
} }
} }
@ -588,7 +589,7 @@ _GSDebugAllocationListAll(void)
if (val != 0) if (val != 0)
{ {
pos += 11 + strlen(class_getName(the_table[i].class)); pos += 22 + strlen(class_getName(the_table[i].class));
} }
} }
if (pos == 0) if (pos == 0)
@ -620,7 +621,8 @@ _GSDebugAllocationListAll(void)
if (val != 0) if (val != 0)
{ {
sprintf(&buf[pos], "%d\t%s\n", val, class_getName(the_table[i].class)); snprintf(&buf[pos], siz - pos, "%d\t%s\n",
val, class_getName(the_table[i].class));
pos += strlen(&buf[pos]); pos += strlen(&buf[pos]);
} }
} }

View file

@ -458,7 +458,7 @@ static void clean_up_names(void)
write(fd, (char*)socket_name, strlen((char*)socket_name)); write(fd, (char*)socket_name, strlen((char*)socket_name));
write(fd, "\n", 1); write(fd, "\n", 1);
sprintf((char*)buf, "%i\n", getpid()); snprintf((char*)buf, sizeof(buf), "%i\n", getpid());
write(fd, (char*)buf, strlen((char*)buf)); write(fd, (char*)buf, strlen((char*)buf));
close(fd); close(fd);

View file

@ -391,24 +391,30 @@ next_arg(const char *typePtr, NSArgumentInfo *info, char *outTypes)
const char *q; const char *q;
char *args; char *args;
char *ret; char *ret;
char *end;
char *ptr;
int alen; int alen;
int blen;
int rlen; int rlen;
/* In case we have been given a method encoding string without offsets, /* In case we have been given a method encoding string without offsets,
* we attempt to generate the frame size and offsets in a new copy of * we attempt to generate the frame size and offsets in a new copy of
* the types string. * the types string.
*/ */
ret = alloca((strlen(t)+1)*16); blen = (strlen(t) + 1) * 16; // Total buffer length
ret = alloca(blen);
end = ret + blen;
/* Copy the return type (including qualifiers) with ehough room /* Copy the return type (including qualifiers) with ehough room
* after it to store the frame size. * after it to store the frame size.
*/ */
p = t; p = t;
p = objc_skip_typespec (p); p = objc_skip_typespec (p);
strncpy(ret, t, p - t); rlen = p - t;
ret[p - t] = '\0'; strncpy(ret, t, rlen);
args = ret + (p - t) + 10; ret[rlen] = '\0';
*args = '\0'; ptr = args = ret + rlen + 10; // Allow room for a decimal integer
*ptr = '\0';
/* Skip to the first arg type, taking note of where the qualifiers start. /* Skip to the first arg type, taking note of where the qualifiers start.
* Assume that casting _argFrameLength to int will not lose information. * Assume that casting _argFrameLength to int will not lose information.
@ -423,15 +429,16 @@ next_arg(const char *typePtr, NSArgumentInfo *info, char *outTypes)
_numArgs++; _numArgs++;
size = objc_promoted_size (p); size = objc_promoted_size (p);
p = objc_skip_typespec (p); p = objc_skip_typespec (p);
strncat(args, q, p - q); memcpy(ptr, q, p - q);
sprintf(args + strlen(args), "%d", (int)_argFrameLength); ptr += (p - q);
snprintf(ptr, end - ptr, "%d", (int)_argFrameLength);
ptr += strlen(ptr);
_argFrameLength += size; _argFrameLength += size;
p = skip_offset (p); p = skip_offset (p);
q = p; q = p;
p = objc_skip_type_qualifiers (p); p = objc_skip_type_qualifiers (p);
} }
alen = strlen(args); alen = ptr - args;
rlen = strlen(ret);
sprintf(ret + rlen, "%d", (int)_argFrameLength); sprintf(ret + rlen, "%d", (int)_argFrameLength);
_methodTypes = NSZoneMalloc(NSDefaultMallocZone(), alen + rlen + 1); _methodTypes = NSZoneMalloc(NSDefaultMallocZone(), alen + rlen + 1);

View file

@ -799,7 +799,7 @@ static void
GSGarbageCollectorLog(char *msg, GC_word arg) GSGarbageCollectorLog(char *msg, GC_word arg)
{ {
char buf[strlen(msg)+1024]; char buf[strlen(msg)+1024];
sprintf(buf, msg, (unsigned long)arg); snprintf(buf, sizeof(buf), msg, (unsigned long)arg);
fprintf(stderr, "Garbage collector: %s", buf); fprintf(stderr, "Garbage collector: %s", buf);
} }
#endif #endif
@ -2000,9 +2000,9 @@ objc_create_block_classes_as_subclasses_of(Class super) __attribute__((weak));
+((aString!=NULL)?strlen((char*)aString):0)+8)]; +((aString!=NULL)?strlen((char*)aString):0)+8)];
va_list ap; va_list ap;
sprintf(fmt, FMT, GSClassNameFromObject(self), snprintf(fmt, sizeof(fmt), FMT, GSClassNameFromObject(self),
GSObjCIsInstance(self)?"instance":"class", GSObjCIsInstance(self) ? "instance" : "class",
(aString!=NULL)?aString:""); (aString != NULL) ? aString : "");
va_start(ap, aString); va_start(ap, aString);
vfprintf (stderr, fmt, ap); vfprintf (stderr, fmt, ap);
abort (); abort ();

View file

@ -132,13 +132,13 @@ typeToName1(char type)
if (bufptr == buf1) if (bufptr == buf1)
{ {
bufptr = buf2; bufptr = buf2;
} }
else else
{ {
bufptr = buf1; bufptr = buf1;
} }
sprintf(bufptr, "unknown type info - 0x%x", type); snprintf(bufptr, 32, "unknown type info - 0x%x", type);
return bufptr; return bufptr;
} }
} }
@ -177,13 +177,13 @@ typeToName2(char type)
if (bufptr == buf1) if (bufptr == buf1)
{ {
bufptr = buf2; bufptr = buf2;
} }
else else
{ {
bufptr = buf1; bufptr = buf1;
} }
sprintf(bufptr, "unknown type info - 0x%x", type); snprintf(bufptr, 32, "unknown type info - 0x%x", type);
return bufptr; return bufptr;
} }
} }
@ -1999,7 +1999,8 @@ static IMP _xRefImp; /* Serialize a crossref. */
char header[headerLength+1]; char header[headerLength+1];
unsigned dataLength = [_dst length]; unsigned dataLength = [_dst length];
sprintf(header, "%s%08x:%08x:%08x:%08x:", PREFIX, v, cc, oc, pc); snprintf(header, sizeof(header), "%s%08x:%08x:%08x:%08x:",
PREFIX, v, cc, oc, pc);
if (locationInData + headerLength <= dataLength) if (locationInData + headerLength <= dataLength)
{ {

View file

@ -564,8 +564,8 @@ static char **_gnu_noobjc_env = NULL;
int i, count; int i, count;
// Read commandline // Read commandline
proc_file_name = (char*)malloc(sizeof(char) * 2048); proc_file_name = (char*)malloc(2048);
sprintf(proc_file_name, "/proc/%d/psinfo", (int)getpid()); snprintf(proc_file_name, 2048, "/proc/%d/psinfo", (int)getpid());
ifp = fopen(proc_file_name, "r"); ifp = fopen(proc_file_name, "r");
if (ifp == NULL) if (ifp == NULL)
@ -673,8 +673,8 @@ static char **_gnu_noobjc_env = NULL;
_gnu_noobjc_env[c] = NULL; _gnu_noobjc_env[c] = NULL;
// Read commandline // Read commandline
proc_file_name = (char *)malloc(sizeof(char) * 2048); proc_file_name = (char *)malloc(2048);
sprintf(proc_file_name, "/proc/%d/cmdline", (int)getpid()); snprintf(proc_file_name, 2048, "/proc/%d/cmdline", (int)getpid());
/* /*
* We read the /proc file thrice. * We read the /proc file thrice.

View file

@ -741,7 +741,7 @@ static NSMapTable *absolutes = 0;
int m = i % 60; int m = i % 60;
char buf[9]; char buf[9];
sprintf(buf, "GMT%c%02d%02d", s, h, m); snprintf(buf, sizeof(buf), "GMT%c%02d%02d", s, h, m);
name = [[NSString alloc] initWithUTF8String: buf]; name = [[NSString alloc] initWithUTF8String: buf];
} }
else else

View file

@ -86,13 +86,13 @@ typeToName1(char type)
if (bufptr == buf1) if (bufptr == buf1)
{ {
bufptr = buf2; bufptr = buf2;
} }
else else
{ {
bufptr = buf1; bufptr = buf1;
} }
sprintf(bufptr, "unknown type info - 0x%x", type); snprintf(bufptr, 32, "unknown type info - 0x%x", type);
return bufptr; return bufptr;
} }
} }
@ -131,13 +131,13 @@ typeToName2(char type)
if (bufptr == buf1) if (bufptr == buf1)
{ {
bufptr = buf2; bufptr = buf2;
} }
else else
{ {
bufptr = buf1; bufptr = buf1;
} }
sprintf(bufptr, "unknown type info - 0x%x", type); snprintf(bufptr, 32, "unknown type info - 0x%x", type);
return bufptr; return bufptr;
} }
} }

View file

@ -280,7 +280,7 @@ main(int argc, char** argv, char **env)
} }
else else
{ {
sprintf(&c[o], "\\U%04x", u[i]); snprintf(&c[o], 6, "\\U%04x", u[i]);
o += 6; o += 6;
} }
} }

View file

@ -52,7 +52,7 @@ loc_read_file(const char *dir, const char *file)
if (strcmp(file, "POSIX") == 0) if (strcmp(file, "POSIX") == 0)
return 0; return 0;
sprintf(name, "%s/%s", dir, file); snprintf(name, sizeof(name), "%s/%s", dir, file);
fp = fopen(name, "r"); fp = fopen(name, "r");
if (fp == NULL) if (fp == NULL)
return -1; return -1;