Skip to content

Commit

Permalink
Fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
rfm committed Dec 19, 2024
1 parent 546ca94 commit 5aa5692
Showing 1 changed file with 45 additions and 42 deletions.
87 changes: 45 additions & 42 deletions Source/NSNumberFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
#import "Foundation/NSCharacterSet.h"

#import "GNUstepBase/GSLocale.h"
#import "GSPrivate.h"

@class NSDoubleNumber;

Expand Down Expand Up @@ -1407,55 +1408,57 @@ - (NSString *) stringFromNumber: (NSNumber *)number

- (NSNumber *) numberFromString: (NSString *)string
{
NSNumber *result = nil;
// This is a 10.4 and above method and should not work with earlier version.
#if GS_USE_ICU == 1
NSNumber *result;
NSUInteger length;
NSRange range;
UErrorCode err = U_ZERO_ERROR;
unichar *ustring;
int64_t intNum;
double doubleNum;

if (string == nil)
return nil;

length = [string length];
ustring = NSZoneMalloc ([self zone], sizeof(unichar) * length);
if (ustring == NULL)
return nil;
NSUInteger length = [string length];

if (length > 0)
{
NSRange range;
UErrorCode err = U_ZERO_ERROR;
int64_t intNum;
double doubleNum;
GS_BEGINITEMBUF(ustring, length * sizeof(unichar), unichar)

[string getCharacters: ustring range: NSMakeRange(0, length)];
[string getCharacters: ustring range: NSMakeRange(0, length)];

// FIXME: Not sure if this is correct....
range = [string rangeOfString: @"."];
if (range.location == NSNotFound)
{
intNum = unum_parseInt64(internal->_formatter,
ustring, length, NULL, &err);
if (U_FAILURE(err))
return nil;
if (intNum == 0 || intNum == 1)
result = [NSNumber numberWithBool: (BOOL) intNum];
else if (intNum < INT_MAX && intNum > INT_MIN)
result = [NSNumber numberWithInt: (int32_t)intNum];
// FIXME: Not sure if this is correct....
range = [string rangeOfString: @"."];
if (range.location == NSNotFound)
{
intNum = unum_parseInt64(internal->_formatter,
ustring, length, NULL, &err);
if (!U_FAILURE(err))
{
if (intNum == 0 || intNum == 1)
{
result = [NSNumber numberWithBool: (BOOL) intNum];
}
else if (intNum < INT_MAX && intNum > INT_MIN)
{
result = [NSNumber numberWithInt: (int32_t)intNum];
}
else
{
result = [NSNumber numberWithLongLong: intNum];
}
}
}
else
result = [NSNumber numberWithLongLong: intNum];
}
else
{
doubleNum = unum_parseDouble(internal->_formatter,
ustring, length, NULL, &err);
if (U_FAILURE(err))
return nil;
result = [NSNumber numberWithDouble: doubleNum];
{
doubleNum = unum_parseDouble(internal->_formatter,
ustring, length, NULL, &err);
if (!U_FAILURE(err))
{
result = [NSNumber numberWithDouble: doubleNum];
}
}

GS_ENDITEMBUF()
}

NSZoneFree ([self zone], ustring);
return result;
#else
return nil;
#endif
return result;
}


Expand Down

0 comments on commit 5aa5692

Please sign in to comment.