IOSAD-72 using PEPAutoFree
authorDirk Zimmermann <dirk@pep-project.org>
Thu, 12 Apr 2018 07:15:39 +0200
changeset 563dc4dab1c4a45
parent 562 8be1319d0838
child 564 9eba9cef4911
IOSAD-72 using PEPAutoFree
pEpObjCAdapter/PEPAutoFree.h
pEpObjCAdapter/PEPAutoFree.m
pEpObjCAdapter/PEPInternalSession.m
     1.1 --- a/pEpObjCAdapter/PEPAutoFree.h	Thu Apr 12 07:15:38 2018 +0200
     1.2 +++ b/pEpObjCAdapter/PEPAutoFree.h	Thu Apr 12 07:15:39 2018 +0200
     1.3 @@ -13,6 +13,9 @@
     1.4   */
     1.5  @interface PEPAutoFree : NSObject
     1.6  
     1.7 -- (void **)pointerPointer;
     1.8 +- (void **)voidPointerPointer;
     1.9 +- (char **)charPointerPointer;
    1.10 +- (void *)voidPointer;
    1.11 +- (char *)charPointer;
    1.12  
    1.13  @end
     2.1 --- a/pEpObjCAdapter/PEPAutoFree.m	Thu Apr 12 07:15:38 2018 +0200
     2.2 +++ b/pEpObjCAdapter/PEPAutoFree.m	Thu Apr 12 07:15:39 2018 +0200
     2.3 @@ -16,11 +16,26 @@
     2.4  
     2.5  @implementation PEPAutoFree
     2.6  
     2.7 -- (void **)pointerPointer
     2.8 +- (void **)voidPointerPointer
     2.9  {
    2.10      return &_thePointer;
    2.11  }
    2.12  
    2.13 +- (char **)charPointerPointer
    2.14 +{
    2.15 +    return (char **) self.voidPointerPointer;
    2.16 +}
    2.17 +
    2.18 +- (void *)voidPointer
    2.19 +{
    2.20 +    return self.thePointer;
    2.21 +}
    2.22 +
    2.23 +- (char *)charPointer
    2.24 +{
    2.25 +    return (char *) self.voidPointer;
    2.26 +}
    2.27 +
    2.28  - (void)dealloc
    2.29  {
    2.30      free(_thePointer);
     3.1 --- a/pEpObjCAdapter/PEPInternalSession.m	Thu Apr 12 07:15:38 2018 +0200
     3.2 +++ b/pEpObjCAdapter/PEPInternalSession.m	Thu Apr 12 07:15:39 2018 +0200
     3.3 @@ -17,6 +17,7 @@
     3.4  #import "PEPIdentity.h"
     3.5  #import "PEPMessage.h"
     3.6  #import "NSError+PEP.h"
     3.7 +#import "PEPAutoFree.h"
     3.8  
     3.9  @implementation PEPInternalSession
    3.10  
    3.11 @@ -434,18 +435,20 @@
    3.12          unsigned int value;
    3.13          [[NSScanner scannerWithString:str] scanHexInt:&value];
    3.14  
    3.15 -        char *word = NULL;
    3.16 +        PEPAutoFree *word = [PEPAutoFree new];
    3.17          size_t size;
    3.18  
    3.19 -        PEP_STATUS status = trustword(_session, value, [languageID UTF8String], &word, &size);
    3.20 +        PEP_STATUS status = trustword(_session,
    3.21 +                                      value,
    3.22 +                                      [languageID UTF8String],
    3.23 +                                      word.charPointerPointer,
    3.24 +                                      &size);
    3.25  
    3.26          if ([NSError setError:error fromPEPStatus:status]) {
    3.27 -            free(word);
    3.28              return nil;
    3.29          }
    3.30  
    3.31 -        [array addObject:[NSString stringWithUTF8String:word]];
    3.32 -        free(word);
    3.33 +        [array addObject:[NSString stringWithUTF8String:word.charPointer]];
    3.34      }
    3.35  
    3.36      return array;
    3.37 @@ -617,42 +620,36 @@
    3.38      pEp_identity *ident2 = PEP_identityToStruct(identity2);
    3.39      PEP_STATUS status;
    3.40  
    3.41 -    char *trustwords = nil;
    3.42 +    PEPAutoFree *trustwords = [PEPAutoFree new];
    3.43      size_t sizeWritten = 0;
    3.44  
    3.45      [self lockWrite];
    3.46      status = get_trustwords(_session, ident1, ident2,
    3.47                              [[language precomposedStringWithCanonicalMapping]
    3.48                               UTF8String],
    3.49 -                            &trustwords, &sizeWritten, full);
    3.50 +                            trustwords.charPointerPointer, &sizeWritten, full);
    3.51      [self unlockWrite];
    3.52  
    3.53      NSString *result = nil;
    3.54  
    3.55      if (![NSError setError:error fromPEPStatus:status]) {
    3.56 -        result = [NSString stringWithCString:trustwords
    3.57 +        result = [NSString stringWithCString:trustwords.charPointer
    3.58                                      encoding:NSUTF8StringEncoding];
    3.59      }
    3.60  
    3.61 -    if (trustwords) {
    3.62 -        free(trustwords);
    3.63 -    }
    3.64 -
    3.65      return result;
    3.66  }
    3.67  
    3.68  - (NSArray<PEPLanguage *> * _Nullable)languageListWithError:(NSError * _Nullable * _Nullable)error
    3.69  {
    3.70 -    char *chLangs = NULL;
    3.71 -    PEP_STATUS status = get_languagelist(_session, &chLangs);
    3.72 +    PEPAutoFree *chLangs = [PEPAutoFree new];
    3.73 +    PEP_STATUS status = get_languagelist(_session, chLangs.charPointerPointer);
    3.74  
    3.75      if ([NSError setError:error fromPEPStatus:status]) {
    3.76 -        free(chLangs);
    3.77          return nil;
    3.78      }
    3.79  
    3.80 -    NSString *parserInput = [NSString stringWithUTF8String:chLangs];
    3.81 -    free(chLangs);
    3.82 +    NSString *parserInput = [NSString stringWithUTF8String:chLangs.charPointer];
    3.83  
    3.84      NSMutableArray<NSString *> *tokens = [NSMutableArray array];
    3.85      PEPCSVScanner *scanner = [[PEPCSVScanner alloc] initWithString:parserInput];