diff --git a/ChangeLog b/ChangeLog index 4849729a2..0c9ee9634 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,29 @@ +2011-03-06 Richard Frith-Macdonald + + * 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/NSData.m: + * Source/NSHost.m: + * Source/GSMDNSNetServices.m: + * Source/GSValue.m: + * Source/NSTask.m: + * Tools/defaults.m: + * Tools/locale_alias.m: + Avoid strcpy and strcat to get rid if some bsd warnings. + 2011-03-06 Richard Frith-Macdonald * config/config.initialize.m: Correct formatting. Increase time diff --git a/Source/Additions/GSObjCRuntime.m b/Source/Additions/GSObjCRuntime.m index e7a361994..7950c4c13 100644 --- a/Source/Additions/GSObjCRuntime.m +++ b/Source/Additions/GSObjCRuntime.m @@ -427,6 +427,7 @@ GSObjCMakeClass(NSString *name, NSString *superName, NSDictionary *iVars) Class classSuperClass; const char *classNameCString; char *tmp; + int len; NSCAssert(name, @"no name"); NSCAssert(superName, @"no superName"); @@ -437,8 +438,10 @@ GSObjCMakeClass(NSString *name, NSString *superName, NSDictionary *iVars) NSCAssert1(!NSClassFromString(name), @"A class %@ already exists", name); classNameCString = [name UTF8String]; - tmp = malloc(strlen(classNameCString) + 1); - strcpy(tmp, classNameCString); + len = strlen(classNameCString); + tmp = malloc(len + 1); + strncpy(tmp, classNameCString, len); + tmp[len] = '\0'; classNameCString = tmp; newClass = objc_allocateClassPair(classSuperClass, classNameCString, 0); diff --git a/Source/Additions/GSXML.m b/Source/Additions/GSXML.m index 507f70ed9..3151dba5b 100644 --- a/Source/Additions/GSXML.m +++ b/Source/Additions/GSXML.m @@ -150,7 +150,7 @@ static char * xml_strdup(const char *from) { unsigned len = (from == 0) ? 1 : (strlen(from) + 1); char *to = malloc(len); - strcpy(to, from); + memcpy(to, from, len); return to; } diff --git a/Source/Additions/NSError+GNUstepBase.m b/Source/Additions/NSError+GNUstepBase.m index e535035bf..f104031ac 100644 --- a/Source/Additions/NSError+GNUstepBase.m +++ b/Source/Additions/NSError+GNUstepBase.m @@ -60,14 +60,15 @@ strerror_r(int eno, char *buf, int len) ptr = strerror(eno); if (ptr == 0) { - strncpy(buf, "unknown error number", len); + strncpy(buf, "unknown error number", len - 1); result = -1; } else { - strncpy(buf, strerror(eno), len); + strncpy(buf, strerror(eno), len - 1); result = 0; } + buf[len - 1] = '\0'; [gnustep_global_lock unlock]; return result; } @@ -80,10 +81,11 @@ strerror_r(int eno, char *buf, int len) if (eno < 0 || eno >= sys_nerr) { - strncpy(buf, "unknown error number", len); + strncpy(buf, "unknown error number", len - 1); return -1; } - strncpy(buf, sys_errlist[eno], len); + strncpy(buf, sys_errlist[eno], len - 1); + buf[len - 1] = '\0'; return 0; } #endif diff --git a/Source/Additions/Unicode.m b/Source/Additions/Unicode.m index d381a06e0..19557344b 100644 --- a/Source/Additions/Unicode.m +++ b/Source/Additions/Unicode.m @@ -324,15 +324,16 @@ static void GSSetupEncodingTable(void) if (entry->iconv != 0 && *(entry->iconv) != 0) { iconv_t c; + int l; char *lossy; /* * See if we can do a lossy conversion. */ - lossy = NSZoneMalloc(NSDefaultMallocZone(), - strlen(entry->iconv) + 12); - strcpy(lossy, entry->iconv); - strcat(lossy, "//TRANSLIT"); + l = strlen(entry->iconv); + lossy = NSZoneMalloc(NSDefaultMallocZone(), l + 11); + strncpy(lossy, entry->iconv, l); + strncpy(lossy + l, "//TRANSLIT", 11); c = iconv_open(UNICODE_ENC, entry->iconv); if (c == (iconv_t)-1) { @@ -2565,8 +2566,8 @@ GSPrivateDefaultCStringEncoding() /* Take it from the system locale information. */ [gnustep_global_lock lock]; strncpy(encbuf, nl_langinfo(CODESET), sizeof(encbuf)-1); - [gnustep_global_lock unlock]; encbuf[sizeof(encbuf)-1] = '\0'; + [gnustep_global_lock unlock]; encoding = encbuf; /* diff --git a/Source/GSMDNSNetServices.m b/Source/GSMDNSNetServices.m index aeb40a03a..f186bd557 100644 --- a/Source/GSMDNSNetServices.m +++ b/Source/GSMDNSNetServices.m @@ -1523,8 +1523,6 @@ static void DNSSD_API break; } - strcat(key, "\0"); - if ([[values objectAtIndex: i] isKindOfClass: [NSString class]]) { char value[256]; diff --git a/Source/GSValue.m b/Source/GSValue.m index 1b18727bf..463d305ce 100644 --- a/Source/GSValue.m +++ b/Source/GSValue.m @@ -100,8 +100,10 @@ typeSize(const char* type) data = (void *)NSZoneMalloc([self zone], size); memcpy(data, value, size); } - objctype = (char *)NSZoneMalloc([self zone], strlen(type)+1); - strcpy(objctype, type); + size = strlen(type); + objctype = (char *)NSZoneMalloc([self zone], size + 1); + strncpy(objctype, type, size); + objctype[size] = '\0'; } return self; } diff --git a/Source/NSData.m b/Source/NSData.m index 531fe301c..bdcf7d99f 100644 --- a/Source/NSData.m +++ b/Source/NSData.m @@ -1327,7 +1327,8 @@ failure: if (local_c_path != 0 && strlen(local_c_path) < (BUFSIZ*2)) { - strcpy(theRealPath,local_c_path); + strncpy(theRealPath, local_c_path, sizeof(theRealPath) - 1); + theRealPath[sizeof(theRealPath) - 1] = '\0'; error_BadPath = NO; } } @@ -1344,8 +1345,9 @@ failure: int desc; int mask; - strcpy(thePath, theRealPath); - strcat(thePath, "XXXXXX"); + strncpy(thePath, theRealPath, sizeof(thePath) - 1); + thePath[sizeof(thePath) - 1] = '\0'; + strncat(thePath, "XXXXXX", 6); if ((desc = mkstemp(thePath)) < 0) { NSWarnMLog(@"mkstemp (%s) failed - %@", thePath, [NSError _last]); @@ -1361,7 +1363,8 @@ failure: } else { - strcpy(thePath, theRealPath); + strncpy(thePath, theRealPath, sizeof(thePath) - 1); + thePath[sizeof(thePath) - 1] = '\0'; theFile = fopen(thePath, "wb"); } #else @@ -1381,8 +1384,9 @@ failure: goto failure; } #else - strcpy(thePath, theRealPath); - strcat(thePath, "XXXXXX"); + strncpy(thePath, theRealPath, sizeof(thePath) - 1); + thePath[sizeof(thePath) - 1] = '\0'; + strncat(thePath, "XXXXXX", 6); if (mktemp(thePath) == 0) { NSWarnMLog(@"mktemp (%s) failed - %@", thePath, [NSError _last]); @@ -1395,7 +1399,8 @@ failure: #if defined(__MINGW__) wcscpy(wthePath,wtheRealPath); #else - strcpy(thePath, theRealPath); + strncpy(thePath, theRealPath, sizeof(thePath) - 1); + thePath[sizeof(thePath) - 1] = '\0'; #endif } diff --git a/Source/NSFileManager.m b/Source/NSFileManager.m index cd2e14f32..553799806 100644 --- a/Source/NSFileManager.m +++ b/Source/NSFileManager.m @@ -813,7 +813,7 @@ static NSStringEncoding defaultEncoding; return NO; } - strcpy(dirpath, lpath); + strncpy(dirpath, lpath, len); dirpath[len] = '\0'; if (dirpath[len-1] == '/') dirpath[len-1] = '\0'; diff --git a/Source/NSHost.m b/Source/NSHost.m index 51af298da..7cd54543a 100644 --- a/Source/NSHost.m +++ b/Source/NSHost.m @@ -280,7 +280,8 @@ myHostName() } else if (name == nil || strcmp(old, buf) != 0) { - strcpy(old, buf); + strncpy(old, buf, sizeof(old) - 1); + old[sizeof(old) - 1] = '\0'; RELEASE(name); name = [[NSString alloc] initWithCString: buf]; } diff --git a/Source/NSInvocation.m b/Source/NSInvocation.m index 7d9aa5682..0508582c4 100644 --- a/Source/NSInvocation.m +++ b/Source/NSInvocation.m @@ -481,10 +481,13 @@ _arg_addr(NSInvocation *inv, int index) } else { + int len; char *tmp; - tmp = NSZoneMalloc(NSDefaultMallocZone(), strlen(newstr)+1); - strcpy(tmp, newstr); + len = strlen(newstr); + tmp = NSZoneMalloc(NSDefaultMallocZone(), len + 1); + strncpy(tmp, newstr, len); + tmp[len] = '\0'; _set_arg(self, index, tmp); } if (oldstr != 0) @@ -615,9 +618,12 @@ _arg_addr(NSInvocation *inv, int index) if (str != 0) { char *tmp; + int len; - tmp = NSZoneMalloc(NSDefaultMallocZone(), strlen(str)+1); - strcpy(tmp, str); + len = strlen(str); + tmp = NSZoneMalloc(NSDefaultMallocZone(), len + 1); + strncpy(tmp, str, len); + tmp[len] = '\0'; _set_arg(self, i-1, &tmp); } } diff --git a/Source/NSKeyValueCoding.m b/Source/NSKeyValueCoding.m index 6086ae67c..71ed6bc6d 100644 --- a/Source/NSKeyValueCoding.m +++ b/Source/NSKeyValueCoding.m @@ -87,17 +87,17 @@ SetValueForKey(NSObject *self, id anObject, const char *key, unsigned size) if (size > 0) { const char *name; - char buf[size+6]; + char buf[size + 6]; char lo; char hi; - strcpy(buf, "_set"); - strcpy(&buf[4], key); + strncpy(buf, "_set", 4); + strncpy(&buf[4], key, size); lo = buf[4]; hi = islower(lo) ? toupper(lo) : lo; buf[4] = hi; - buf[size+4] = ':'; - buf[size+5] = '\0'; + buf[size + 4] = ':'; + buf[size + 5] = '\0'; name = &buf[1]; // setKey: type = NULL; @@ -111,7 +111,7 @@ SetValueForKey(NSObject *self, id anObject, const char *key, unsigned size) sel = 0; if ([[self class] accessInstanceVariablesDirectly] == YES) { - buf[size+4] = '\0'; + buf[size + 4] = '\0'; buf[3] = '_'; buf[4] = lo; name = &buf[3]; // _key @@ -159,12 +159,13 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) if (size > 0) { const char *name; - char buf[size+5]; + char buf[size + 5]; char lo; char hi; - strcpy(buf, "_get"); - strcpy(&buf[4], key); + strncpy(buf, "_get", 4); + strncpy(&buf[4], key, size); + buf[size + 4] = '\0'; lo = buf[4]; hi = islower(lo) ? toupper(lo) : lo; buf[4] = hi; @@ -353,7 +354,7 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) - (void) setValue: (id)anObject forKey: (NSString*)aKey { unsigned size = [aKey length] * 8; - char key[size+1]; + char key[size + 1]; #ifdef WANT_DEPRECATED_KVC_COMPAT IMP o = [self methodForSelector: @selector(takeValue:forKey:)]; @@ -366,7 +367,7 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) #endif [aKey getCString: key - maxLength: size+1 + maxLength: size + 1 encoding: NSUTF8StringEncoding]; size = strlen(key); SetValueForKey(self, anObject, key, size); @@ -472,16 +473,17 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) } else { - char name[size+16]; + char name[size + 16]; SEL sel; BOOL (*imp)(id,SEL,id*,id*); - strcpy(name, "validate"); + strncpy(name, "validate", 8); [aKey getCString: &name[8] - maxLength: size+1 + maxLength: size + 1 encoding: NSUTF8StringEncoding]; size = strlen(&name[8]); - strcpy(&name[size+8], ":error:"); + strncpy(&name[size + 8], ":error:", 7); + name[size + 15] = '\0'; if (islower(name[8])) { name[8] = toupper(name[8]); @@ -521,10 +523,10 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) - (id) valueForKey: (NSString*)aKey { unsigned size = [aKey length] * 8; - char key[size+1]; + char key[size + 1]; [aKey getCString: key - maxLength: size+1 + maxLength: size + 1 encoding: NSUTF8StringEncoding]; size = strlen(key); return ValueForKey(self, key, size); @@ -607,17 +609,18 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) const char *type = NULL; int off; const char *name; - char key[size+1]; - char buf[size+5]; + char key[size + 1]; + char buf[size + 5]; char lo; char hi; - strcpy(buf, "_get"); + strncpy(buf, "_get", 4); [aKey getCString: key - maxLength: size+1 + maxLength: size + 1 encoding: NSUTF8StringEncoding]; size = strlen(key); - strcpy(&buf[4], key); + strncpy(&buf[4], key, size); + buf[size + 4] = '\0'; lo = buf[4]; hi = islower(lo) ? toupper(lo) : lo; buf[4] = hi; @@ -691,22 +694,23 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) const char *type; int off; const char *name; - char key[size+1]; - char buf[size+6]; + char key[size + 1]; + char buf[size + 6]; char lo; char hi; - strcpy(buf, "_set"); + strncpy(buf, "_set", 4); [aKey getCString: key - maxLength: size+1 + maxLength: size + 1 encoding: NSUTF8StringEncoding]; size = strlen(key); - strcpy(&buf[4], key); + strncpy(&buf[4], key, size); + buf[size + 4] = '\0'; lo = buf[4]; hi = islower(lo) ? toupper(lo) : lo; buf[4] = hi; - buf[size+4] = ':'; - buf[size+5] = '\0'; + buf[size + 4] = ':'; + buf[size + 5] = '\0'; name = buf; // _setKey: type = NULL; @@ -716,7 +720,7 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) sel = 0; if ([[self class] accessInstanceVariablesDirectly] == YES) { - buf[size+4] = '\0'; + buf[size + 4] = '\0'; buf[4] = lo; buf[3] = '_'; name = &buf[3]; // _key @@ -728,7 +732,7 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) } if (type == NULL) { - buf[size+4] = ':'; + buf[size + 4] = ':'; buf[4] = hi; buf[3] = 't'; name = &buf[1]; // setKey: @@ -803,27 +807,27 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) const char *type = 0; int off; unsigned size = [aKey length] * 8; - char key[size+1]; + char key[size + 1]; GSOnceMLog(@"This method is deprecated, use -setValue:forKey:"); [aKey getCString: key - maxLength: size+1 + maxLength: size + 1 encoding: NSUTF8StringEncoding]; size = strlen(key); if (size > 0) { const char *name; - char buf[size+6]; + char buf[size + 6]; char lo; char hi; - strcpy(buf, "_set"); - strcpy(&buf[4], key); + strncpy(buf, "_set", 4); + strncpy(&buf[4], key, size); lo = buf[4]; hi = islower(lo) ? toupper(lo) : lo; buf[4] = hi; - buf[size+4] = ':'; - buf[size+5] = '\0'; + buf[size + 4] = ':'; + buf[size + 5] = '\0'; name = &buf[1]; // setKey: type = NULL; @@ -837,7 +841,7 @@ static id ValueForKey(NSObject *self, const char *key, unsigned size) sel = 0; if ([[self class] accessInstanceVariablesDirectly] == YES) { - buf[size+4] = '\0'; + buf[size + 4] = '\0'; buf[3] = '_'; buf[4] = lo; name = &buf[4]; // key diff --git a/Source/NSMessagePort.m b/Source/NSMessagePort.m index dbed3d6ca..c02aa189e 100644 --- a/Source/NSMessagePort.m +++ b/Source/NSMessagePort.m @@ -187,7 +187,7 @@ newDataWithEncodedPort(NSMessagePort *port) pih->type = GSSwapHostI32ToBig(GSP_PORT); pih->length = GSSwapHostI32ToBig(plen); pi = (GSPortInfo*)&pih[1]; - strcpy((char*)pi->addr, (char*)name); + strncpy((char*)pi->addr, (char*)name, strlen(name) + 1); NSDebugFLLog(@"NSMessagePort", @"Encoded port as '%s'", pi->addr); diff --git a/Source/NSMethodSignature.m b/Source/NSMethodSignature.m index e6a07806d..9e3e30b91 100644 --- a/Source/NSMethodSignature.m +++ b/Source/NSMethodSignature.m @@ -391,6 +391,8 @@ next_arg(const char *typePtr, NSArgumentInfo *info, char *outTypes) const char *q; char *args; char *ret; + int alen; + int rlen; /* 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 @@ -428,11 +430,14 @@ next_arg(const char *typePtr, NSArgumentInfo *info, char *outTypes) q = p; p = objc_skip_type_qualifiers (p); } - sprintf(ret + strlen(ret), "%d", (int)_argFrameLength); - _methodTypes = NSZoneMalloc(NSDefaultMallocZone(), - strlen(args) + strlen(ret) + 1); - strcpy((char*)_methodTypes, ret); - strcat((char*)_methodTypes, args); + alen = strlen(args); + rlen = strlen(ret); + sprintf(ret + rlen, "%d", (int)_argFrameLength); + + _methodTypes = NSZoneMalloc(NSDefaultMallocZone(), alen + rlen + 1); + strncpy((char*)_methodTypes, ret, rlen); + strncpy(((char*)_methodTypes) + rlen, args, alen); + ((char*)_methodTypes)[alen + rlen] = '\0'; } return self; } diff --git a/Source/NSPathUtilities.m b/Source/NSPathUtilities.m index b3bb7b681..8f9def23f 100644 --- a/Source/NSPathUtilities.m +++ b/Source/NSPathUtilities.m @@ -1511,7 +1511,8 @@ NSUserName(void) [gnustep_global_lock lock]; pwent = getpwuid (uid); - strcpy(buf, pwent->pw_name); + strncpy(buf, pwent->pw_name, sizeof(buf) - 1); + buf[sizeof(buf) - 1] = '\0'; [gnustep_global_lock unlock]; loginName = buf; #endif /* HAVE_GETPWUID */ diff --git a/Source/NSPortCoder.m b/Source/NSPortCoder.m index d96012b6a..9111e94bc 100644 --- a/Source/NSPortCoder.m +++ b/Source/NSPortCoder.m @@ -872,6 +872,7 @@ static IMP _xRefImp; /* Serialize a crossref. */ else { char *tmp; + int len; if (xref != GSIArrayCount(_ptrAry)) { @@ -879,9 +880,10 @@ static IMP _xRefImp; /* Serialize a crossref. */ format: @"extra string crossref - %d", xref]; } (*_dDesImp)(_src, dDesSel, &tmp, @encode(char*), &_cursor, nil); - *(void**)address = GSAutoreleasedBuffer(strlen(tmp)+1); + len = strlen(tmp); + *(void**)address = GSAutoreleasedBuffer(len + 1); GSIArrayAddItem(_ptrAry, (GSIArrayItem)*(void**)address); - strcpy(*(char**)address, tmp); + memcpy(*(char**)address, tmp, len + 1); NSZoneFree(NSDefaultMallocZone(), tmp); } return; diff --git a/Source/NSProcessInfo.m b/Source/NSProcessInfo.m index 271fc380f..aa3c01c96 100644 --- a/Source/NSProcessInfo.m +++ b/Source/NSProcessInfo.m @@ -236,8 +236,11 @@ _gnu_process_args(int argc, char *argv[], char *env[]) if (argv != 0 && argv[0] != 0) { - _gnu_arg_zero = (char*)malloc(strlen(argv[0]) + 1); - strcpy(_gnu_arg_zero, argv[0]); + int len; + + len = strlen(argv[0]) + 1; + _gnu_arg_zero = (char*)malloc(len); + memcpy(_gnu_arg_zero, argv[0], len); arg0 = [[NSString alloc] initWithCString: _gnu_arg_zero]; } else @@ -246,6 +249,7 @@ _gnu_process_args(int argc, char *argv[], char *env[]) unichar *buffer; int buffer_size = 0; int needed_size = 0; + int len; const char *tmp; while (needed_size == buffer_size) @@ -272,8 +276,9 @@ _gnu_process_args(int argc, char *argv[], char *env[]) } } tmp = [arg0 cStringUsingEncoding: [NSString defaultCStringEncoding]]; - _gnu_arg_zero = (char*)malloc(strlen(tmp) + 1); - strcpy(_gnu_arg_zero, tmp); + len = strlen(tmp) + 1; + _gnu_arg_zero = (char*)malloc(len); + memcpy(_gnu_arg_zero, tmp, len); #else fprintf(stderr, "Error: for some reason, argv not properly set up " "during GNUstep base initialization\n"); @@ -423,7 +428,7 @@ _gnu_process_args(int argc, char *argv[], char *env[]) { char buf[len+2]; - strcpy(buf, env[i]); + memcpy(buf, env[i], len + 1); cp = &buf[cp - env[i]]; *cp++ = '\0'; [keys addObject: diff --git a/Source/NSString.m b/Source/NSString.m index 71573fe4a..96bfd46d7 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -3398,7 +3398,7 @@ static NSFileManager *fm = nil; { return NO; } - strcpy(buffer, ptr); + strncpy(buffer, ptr, size); return YES; } #endif @@ -4187,7 +4187,7 @@ static NSFileManager *fm = nil; { return IMMUTABLE(self); /* Resolved name too long. */ } - memcpy(dest, start, len); + memmove(dest, start, len); dest += len; *dest = '\0'; @@ -4197,7 +4197,8 @@ static NSFileManager *fm = nil; } if (S_ISLNK(st.st_mode)) { - char buf[PATH_MAX]; + char buf[PATH_MAX]; + int l; if (++num_links > MAXSYMLINKS) { @@ -4210,7 +4211,8 @@ static NSFileManager *fm = nil; } buf[n] = '\0'; - if ((n + strlen(end)) >= PATH_MAX) + l = strlen(end); + if ((n + l) >= PATH_MAX) { return IMMUTABLE(self); /* Path too long. */ } @@ -4218,8 +4220,11 @@ static NSFileManager *fm = nil; * Concatenate the resolved name with the string still to * be processed, and start using the result as input. */ - strcat(buf, end); - strcpy(extra, buf); + memcpy(buf + n, end, l); + n += l; + buf[n] = '\0'; + memcpy(extra, buf, n); + extra[n] = '\0'; name = end = extra; if (buf[0] == '/') @@ -4262,7 +4267,9 @@ static NSFileManager *fm = nil; if (lstat(&newBuf[8], &st) == 0) { - strcpy(newBuf, &newBuf[8]); + int l = strlen(newBuf) - 7; + + memmove(newBuf, &newBuf[8], l); } } return [[NSFileManager defaultManager] diff --git a/Source/NSTask.m b/Source/NSTask.m index a09cff767..b4c06a2af 100644 --- a/Source/NSTask.m +++ b/Source/NSTask.m @@ -167,7 +167,7 @@ pty_master(char* name, int len) } else { - strcpy(name, (char*)slave); + strncpy(name, (char*)slave, len); } } else @@ -178,7 +178,7 @@ pty_master(char* name, int len) master = -1; if (len > 10) { - strcpy(name, "/dev/ptyXX"); + strncpy(name, "/dev/ptyXX", len); while (master < 0 && *groups != '\0') { int i; diff --git a/Source/NSURL.m b/Source/NSURL.m index dec39cb8a..f36bb0ef5 100644 --- a/Source/NSURL.m +++ b/Source/NSURL.m @@ -166,6 +166,7 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) char *buf; char *ptr; char *tmp; + int l; unsigned int len = 1; if (rel->scheme != 0) @@ -222,8 +223,9 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) if (rel->scheme != 0) { - strcpy(ptr, rel->scheme); - ptr = &ptr[strlen(ptr)]; + l = strlen(rel->scheme); + memcpy(ptr, rel->scheme, l); + ptr += l; *ptr++ = ':'; } if (rel->isGeneric == YES @@ -235,14 +237,16 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) { if (rel->user != 0) { - strcpy(ptr, rel->user); - ptr = &ptr[strlen(ptr)]; + l = strlen(rel->user); + memcpy(ptr, rel->user, l); + ptr += l; } if (rel->password != 0) { *ptr++ = ':'; - strcpy(ptr, rel->password); - ptr = &ptr[strlen(ptr)]; + l = strlen(rel->password); + memcpy(ptr, rel->password, l); + ptr += l; } if (rel->host != 0 || rel->port != 0) { @@ -251,14 +255,16 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) } if (rel->host != 0) { - strcpy(ptr, rel->host); - ptr = &ptr[strlen(ptr)]; + l = strlen(rel->host); + memcpy(ptr, rel->host, l); + ptr += l; } if (rel->port != 0) { *ptr++ = ':'; - strcpy(ptr, rel->port); - ptr = &ptr[strlen(ptr)]; + l = strlen(rel->port); + memcpy(ptr, rel->port, l); + ptr += l; } } @@ -273,11 +279,15 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) { *tmp++ = '/'; } - strcpy(tmp, rpath); + l = strlen(rpath); + memcpy(tmp, rpath, l); + tmp += l; } else if (base == 0) { - strcpy(tmp, rpath); + l = strlen(rpath); + memcpy(tmp, rpath, l); + tmp += l; } else if (rpath[0] == 0) { @@ -285,7 +295,9 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) { *tmp++ = '/'; } - strcpy(tmp, base->path); + l = strlen(base->path); + memcpy(tmp, base->path, l); + tmp += l; } else { @@ -295,12 +307,15 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) if (end != 0) { *tmp++ = '/'; - strncpy(tmp, start, end - start); + memcpy(tmp, start, end - start); tmp += (end - start); } *tmp++ = '/'; - strcpy(tmp, rpath); + l = strlen(rpath); + memcpy(tmp, rpath, l); + tmp += l; } + *tmp = '\0'; if (standardize == YES) { @@ -322,7 +337,8 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) } else { - strcpy(tmp, &tmp[2]); + l = strlen(&tmp[2]) + 1; + memmove(tmp, &tmp[2], l); } } else @@ -338,7 +354,8 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) { if (tmp[0] == '/' && tmp[1] == '/') { - strcpy(tmp, &tmp[1]); + l = strlen(&tmp[1]) + 1; + memmove(tmp, &tmp[1], l); } else { @@ -370,7 +387,8 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) } else { - strcpy(tmp, next); + l = strlen(next) + 1; + memmove(tmp, next, l); } } /* @@ -379,7 +397,7 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) tmp = ptr; if (*tmp == '\0') { - strcpy(tmp, "/"); + memcpy(tmp, "/", 2); } } ptr = &ptr[strlen(ptr)]; @@ -387,21 +405,25 @@ static char *buildURL(parsedURL *base, parsedURL *rel, BOOL standardize) if (rel->parameters != 0) { *ptr++ = ';'; - strcpy(ptr, rel->parameters); - ptr = &ptr[strlen(ptr)]; + l = strlen(rel->parameters); + memcpy(ptr, rel->parameters, l); + ptr += l; } if (rel->query != 0) { *ptr++ = '?'; - strcpy(ptr, rel->query); - ptr = &ptr[strlen(ptr)]; + l = strlen(rel->query); + memcpy(ptr, rel->query, l); + ptr += l; } if (rel->fragment != 0) { *ptr++ = '#'; - strcpy(ptr, rel->fragment); + l = strlen(rel->fragment); + memcpy(ptr, rel->fragment, l); + ptr += l; } - + *ptr = '\0'; return buf; } @@ -1341,8 +1363,9 @@ static unsigned urlAlign; - (char*) _path: (char*)buf { - char *ptr = buf; - char *tmp = buf; + char *ptr = buf; + char *tmp = buf; + int l; if (myData->pathIsAbsolute == YES) { @@ -1352,14 +1375,16 @@ static unsigned urlAlign; } if (myData->path != 0) { - strcpy(tmp, myData->path); + l = strlen(myData->path); + memcpy(tmp, myData->path, l + 1); } } else if (_baseURL == nil) { if (myData->path != 0) { - strcpy(tmp, myData->path); + l = strlen(myData->path); + memcpy(tmp, myData->path, l + 1); } } else if (*myData->path == 0) @@ -1370,7 +1395,8 @@ static unsigned urlAlign; } if (baseData->path != 0) { - strcpy(tmp, baseData->path); + l = strlen(baseData->path); + memcpy(tmp, baseData->path, l + 1); } } else @@ -1387,7 +1413,8 @@ static unsigned urlAlign; *tmp++ = '/'; if (myData->path != 0) { - strcpy(tmp, myData->path); + l = strlen(myData->path); + memcpy(tmp, myData->path, l + 1); } } diff --git a/Source/dld-load.h b/Source/dld-load.h index b02d51102..65200b3e2 100644 --- a/Source/dld-load.h +++ b/Source/dld-load.h @@ -49,33 +49,37 @@ static void **dld_ctor_list = 0; static void** __dld_construct_ctor_list(dl_handle_t module) { - int i, ctors, length; + int i, ctors, length; - length = 100; - ctors = 1; - if (dld_ctor_list) - free(dld_ctor_list); - dld_ctor_list = (void **) __objc_xmalloc(length * sizeof(void *)); - /* Find all symbols with the GLOBAL_PREFIX prefix */ - for (i=0; i < TABSIZE; i++) { - struct glosym *sym_entry = _dld_symtab[i]; - for (; sym_entry; sym_entry = sym_entry->link) { - if (strstr(sym_entry->name, GLOBAL_PREFIX) - && strstr(sym_entry->defined_by->filename, module)) { - dld_ctor_list[ctors] = (void **)sym_entry->value; - ctors++; - if (ctors > length) { - length *= 2; - dld_ctor_list = (void **) __objc_xrealloc(dld_ctor_list, - length * sizeof(void *)); + length = 100; + ctors = 1; + if (dld_ctor_list) + free(dld_ctor_list); + dld_ctor_list = (void **) __objc_xmalloc(length * sizeof(void *)); + /* Find all symbols with the GLOBAL_PREFIX prefix */ + for (i=0; i < TABSIZE; i++) + { + struct glosym *sym_entry = _dld_symtab[i]; + for (; sym_entry; sym_entry = sym_entry->link) + { + if (strstr(sym_entry->name, GLOBAL_PREFIX) + && strstr(sym_entry->defined_by->filename, module)) + { + dld_ctor_list[ctors] = (void **)sym_entry->value; + ctors++; + if (ctors > length) + { + length *= 2; + dld_ctor_list = (void **) __objc_xrealloc(dld_ctor_list, + length * sizeof(void *)); } } } } - dld_ctor_list[ctors] = (void **)0; - dld_ctor_list[0] = (void **)(ctors - 1); + dld_ctor_list[ctors] = (void **)0; + dld_ctor_list[0] = (void **)(ctors - 1); - return dld_ctor_list; + return dld_ctor_list; } /* Do any initialization necessary. Return 0 on success (or @@ -84,7 +88,7 @@ __dld_construct_ctor_list(dl_handle_t module) static int __objc_dynamic_init(const char* exec_path) { - return dld_init(exec_path); + return dld_init(exec_path); } /* Link in the module given by the name 'module'. Return a handle which can @@ -93,15 +97,17 @@ __objc_dynamic_init(const char* exec_path) static dl_handle_t __objc_dynamic_link(const char* module, int mode, const char* debug_file) { - int error; - dl_handle_t handle; + int error; + int length = strlen(module); + dl_handle_t handle; - error = dld_link(module); - if (error) - return NULL; - handle = (dl_handle_t)__objc_xmalloc (strlen(module) + 1); - strcpy(handle, module); - return handle; + error = dld_link(module); + if (error) + return NULL; + handle = (dl_handle_t)__objc_xmalloc (length + 1); + strncpy(handle, module, length); + handle[length] = '\0'; + return handle; } /* Return the address of a symbol given by the name 'symbol' from the module @@ -115,20 +121,21 @@ __objc_dynamic_find_symbol(dl_handle_t handle, const char* symbol) static dl_symbol_t __objc_dynamic_find_symbol(dl_handle_t handle, const char* symbol) { - if (strcmp(symbol, DLD_CTOR_LIST) == 0) { - return (dl_symbol_t)__dld_construct_ctor_list(handle); + if (strcmp(symbol, DLD_CTOR_LIST) == 0) + { + return (dl_symbol_t)__dld_construct_ctor_list(handle); } - return dld_get_bare_symbol(symbol); + return dld_get_bare_symbol(symbol); } /* remove the code from memory associated with the module 'handle' */ static int __objc_dynamic_unlink(dl_handle_t handle) { - int error; - error = dld_unlink_by_file(handle, 0); - free(handle); - return error; + int error; + error = dld_unlink_by_file(handle, 0); + free(handle); + return error; } /* Print an error message (prefaced by 'error_string') relevant to the @@ -137,21 +144,21 @@ __objc_dynamic_unlink(dl_handle_t handle) static void __objc_dynamic_error(FILE *error_stream, const char *error_string) { - /* dld won't print to error stream, sorry */ - dld_perror(error_string); + /* dld won't print to error stream, sorry */ + dld_perror(error_string); } /* Debugging: define these if they are available */ static int __objc_dynamic_undefined_symbol_count(void) { - return dld_undefined_sym_count; + return dld_undefined_sym_count; } static char** __objc_dynamic_list_undefined_symbols(void) { - return dld_list_undefined_sym(); + return dld_list_undefined_sym(); } /* current dld version does not support an equivalent of dladdr() */ diff --git a/Tools/defaults.m b/Tools/defaults.m index d2de1f562..8ef4d2ba5 100644 --- a/Tools/defaults.m +++ b/Tools/defaults.m @@ -55,7 +55,7 @@ static NSString *input(char **ptr) { if (*tmp == '\'') { - strcpy(&tmp[-1], tmp); + memmove(&tmp[-1], tmp, strlen(tmp) + 1); } else { diff --git a/Tools/locale_alias.m b/Tools/locale_alias.m index da20655d2..98a9e57ae 100644 --- a/Tools/locale_alias.m +++ b/Tools/locale_alias.m @@ -68,7 +68,8 @@ loc_read_file(const char *dir, const char *file) } if ((s = strstr(buf, "ocale for")) != NULL) { - strcpy(country, s+10); + strncpy(country, s + 10, sizeof(country) - 1); + country[sizeof(country) - 1] = '\0'; s = strchr(country, '\n'); if (s) *s = '\0'; @@ -77,10 +78,11 @@ loc_read_file(const char *dir, const char *file) break; } - strcpy(locale, file); + strncpy(locale, file, sizeof(locale) - 1); + locale[sizeof(locale) - 1] = '\0'; if (strlen(country) > 0 && strcmp(country, language) != 0) { - strcat(country, language); + strncat(country, language, sizeof(country) - 1 - strlen(country)); [dict setObject: [NSString stringWithUTF8String: country] forKey: [NSString stringWithUTF8String: locale]]; }