Skip to content

Commit 219d049

Browse files
committed
Move utility function ot a different file
1 parent 030eac5 commit 219d049

File tree

4 files changed

+84
-30
lines changed

4 files changed

+84
-30
lines changed

Frameworks/SPMySQLFramework/SPMySQLFramework.xcodeproj/project.pbxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
507FF1E51BC0D82300104523 /* DataConversion_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF1811BC0C64100104523 /* DataConversion_Tests.m */; };
1414
507FF23B1BC0E8CA00104523 /* SPMySQL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8DC2EF5B0486A6940098B216 /* SPMySQL.framework */; };
1515
507FF23D1BC157B500104523 /* SPMySQLStringAdditions_Tests.m in Sources */ = {isa = PBXBuildFile; fileRef = 507FF23C1BC157B500104523 /* SPMySQLStringAdditions_Tests.m */; };
16+
508FDC95204329580062A9CA /* SPMySQLUtilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 508FDC94204329580062A9CA /* SPMySQLUtilities.m */; };
1617
580A331E14D75CF7000D6933 /* SPMySQLGeometryData.h in Headers */ = {isa = PBXBuildFile; fileRef = 580A331C14D75CF7000D6933 /* SPMySQLGeometryData.h */; settings = {ATTRIBUTES = (Public, ); }; };
1718
580A331F14D75CF7000D6933 /* SPMySQLGeometryData.m in Sources */ = {isa = PBXBuildFile; fileRef = 580A331D14D75CF7000D6933 /* SPMySQLGeometryData.m */; };
1819
583C734A17A489CC0056B284 /* SPMySQLStreamingResultStoreDelegate.h in Headers */ = {isa = PBXBuildFile; fileRef = 583C734917A489CC0056B284 /* SPMySQLStreamingResultStoreDelegate.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -103,6 +104,7 @@
103104
507FF1D51BC0D7D300104523 /* SPMySQL Unit Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "SPMySQL Unit Tests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; };
104105
507FF1D81BC0D7D300104523 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
105106
507FF23C1BC157B500104523 /* SPMySQLStringAdditions_Tests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SPMySQLStringAdditions_Tests.m; sourceTree = "<group>"; };
107+
508FDC94204329580062A9CA /* SPMySQLUtilities.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SPMySQLUtilities.m; path = Source/SPMySQLUtilities.m; sourceTree = "<group>"; };
106108
580A331C14D75CF7000D6933 /* SPMySQLGeometryData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SPMySQLGeometryData.h; path = Source/SPMySQLGeometryData.h; sourceTree = "<group>"; };
107109
580A331D14D75CF7000D6933 /* SPMySQLGeometryData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SPMySQLGeometryData.m; path = Source/SPMySQLGeometryData.m; sourceTree = "<group>"; };
108110
583C734917A489CC0056B284 /* SPMySQLStreamingResultStoreDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SPMySQLStreamingResultStoreDelegate.h; path = Source/SPMySQLStreamingResultStoreDelegate.h; sourceTree = "<group>"; };
@@ -273,6 +275,7 @@
273275
32C88DFF0371C24200C91783 /* Other Sources */ = {
274276
isa = PBXGroup;
275277
children = (
278+
508FDC94204329580062A9CA /* SPMySQLUtilities.m */,
276279
584294E314CB8002000F8438 /* SPMySQLConstants.h */,
277280
58C006C714E0B18A00AC489A /* SPMySQLUtilities.h */,
278281
32DBCF5E0370ADEE00C91783 /* SPMySQLFramework_Prefix.pch */,
@@ -555,6 +558,7 @@
555558
isa = PBXSourcesBuildPhase;
556559
buildActionMask = 2147483647;
557560
files = (
561+
508FDC96204329580062A9CA /* SPMySQLUtilities.m in Sources */,
558562
507FF23D1BC157B500104523 /* SPMySQLStringAdditions_Tests.m in Sources */,
559563
507FF1E51BC0D82300104523 /* DataConversion_Tests.m in Sources */,
560564
);
@@ -582,6 +586,7 @@
582586
58C00BD214E7459600AC489A /* Databases & Tables.m in Sources */,
583587
586A99FC14F02E21007F82BF /* SPMySQLStreamingResult.m in Sources */,
584588
586AA16814F30C5F007F82BF /* Convenience Methods.m in Sources */,
589+
508FDC95204329580062A9CA /* SPMySQLUtilities.m in Sources */,
585590
584D812F15057ECD00F24774 /* SPMySQLKeepAliveTimer.m in Sources */,
586591
584D82561509775000F24774 /* Copying.m in Sources */,
587592
58D2A4D216EDF1C6002EB401 /* SPMySQLEmptyResult.m in Sources */,

Frameworks/SPMySQLFramework/Source/SPMySQLConnection.m

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@
3434
#include <pthread.h>
3535
#include <SystemConfiguration/SCNetworkReachability.h>
3636
#include <errno.h>
37-
#define __STDC_WANT_LIB_EXT1__ 1
3837
#include <string.h>
39-
#include <stdlib.h>
40-
#include <dlfcn.h>
4138
#import "SPMySQLUtilities.h"
4239

4340
// Thread flag constant
@@ -56,7 +53,6 @@
5653
const char *SPMySQLSSLPermissibleCiphers = "DHE-RSA-AES256-SHA:AES256-SHA:DHE-RSA-AES128-SHA:AES128-SHA:AES256-RMD:AES128-RMD:DES-CBC3-RMD:DHE-RSA-AES256-RMD:DHE-RSA-AES128-RMD:DHE-RSA-DES-CBC3-RMD:RC4-SHA:RC4-MD5:DES-CBC3-SHA:DES-CBC-SHA:EDH-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC-SHA";
5754

5855
static void PasswordCallback(MYSQL *mysql, const char *plugin, void (^with_password)(const char *passwd));
59-
static errno_t LegacyMemsetS(void *ptr, rsize_t ignored, int value, rsize_t count);
6056

6157
@implementation SPMySQLConnection
6258

@@ -792,16 +788,8 @@ - (void)_mysqlConnection:(MYSQL *)connection wantsPassword:(void (^)(const char
792788
else {
793789
NSLog(@"%s: -getCString:maxLength:encoding: failed for password!", __PRETTY_FUNCTION__);
794790
}
795-
796-
// memset_s is 10.9+ only - if we added a link time dependency, SP wouldn't launch on older targets
797-
static errno_t (*memsetPtr)(void *, rsize_t, int, rsize_t);
798-
static dispatch_once_t findMemsetToken;
799-
dispatch_once(&findMemsetToken, ^{
800-
memsetPtr = dlsym(RTLD_DEFAULT, "memset_s");
801-
if(!memsetPtr) memsetPtr = LegacyMemsetS;
802-
});
803-
804-
memsetPtr(cBuffer, cLength, '\0', cLength); //clear password from memory
791+
792+
SPMySQLSafeEraseMemory(cBuffer, cLength); //clear password from memory
805793
free(cBuffer);
806794
}
807795

@@ -1240,18 +1228,3 @@ void PasswordCallback(MYSQL *mysql, const char *plugin, void (^with_password)(co
12401228
assert(mysql && mysql->sp_context);
12411229
[(SPMySQLConnection *)mysql->sp_context _mysqlConnection:mysql wantsPassword:with_password withPlugin:plugin];
12421230
}
1243-
1244-
/**
1245-
* This function tries to emulate the important (to us) parts
1246-
* of memset_s on pre 10.9 systems.
1247-
*
1248-
* The implementation is taken from the original memset_s proposal:
1249-
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
1250-
*/
1251-
errno_t LegacyMemsetS(void *s, rsize_t smax __attribute__((unused)), int c, rsize_t n)
1252-
{
1253-
volatile unsigned char * addr = (volatile unsigned char *)s;
1254-
while(n--) *addr++ = (unsigned char)c;
1255-
1256-
return 0;
1257-
}

Frameworks/SPMySQLFramework/Source/SPMySQLUtilities.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// Locking.h
2+
// SPMySQLUtilities.h
33
// SPMySQLFramework
44
//
55
// Created by Rowan Beentje (rowan.beent.je) on February 6, 2012
@@ -30,6 +30,16 @@
3030

3131
#include <mach/mach_time.h>
3232

33+
/**
34+
* This function provides a memset() variant that cannot be optimized
35+
* away by a compiler's dead store optimization.
36+
*
37+
* It will write cLength '\0' bytes to the buffer pointed to by cBuffer.
38+
*
39+
* @return See the documentation for memset_s in libc
40+
*/
41+
errno_t SPMySQLSafeEraseMemory(void *cBuffer, size_t cLength);
42+
3343
#pragma clang diagnostic push
3444
#pragma clang diagnostic ignored "-Wunused-function"
3545

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// SPMySQLUtilities.c
3+
// SPMySQLFramework
4+
//
5+
// Created by Max Lohrmann on 25.02.2018
6+
// Copyright (c) 2018 Max Lohrmann. All rights reserved.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the "Software"), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
//
29+
// More info at <https://github.com/sequelpro/sequelpro>
30+
31+
#include <errno.h>
32+
#define __STDC_WANT_LIB_EXT1__ 1
33+
#include <string.h>
34+
#include <dlfcn.h>
35+
#include <dispatch/dispatch.h>
36+
37+
#include "SPMySQLUtilities.h"
38+
39+
static errno_t LegacyMemsetS(void *ptr, rsize_t ignored, int value, rsize_t count);
40+
41+
errno_t SPMySQLSafeEraseMemory(void *cBuffer, size_t cLength) {
42+
// memset_s is 10.9+ only - if we added a link time dependency, SP wouldn't launch on older targets
43+
static errno_t (*memsetPtr)(void *, rsize_t, int, rsize_t);
44+
static dispatch_once_t findMemsetToken;
45+
dispatch_once(&findMemsetToken, ^{
46+
memsetPtr = dlsym(RTLD_DEFAULT, "memset_s");
47+
if(!memsetPtr) memsetPtr = &LegacyMemsetS;
48+
});
49+
50+
return (*memsetPtr)(cBuffer, cLength, '\0', cLength);
51+
}
52+
53+
/**
54+
* This function tries to emulate the important (to us) parts
55+
* of memset_s on pre 10.9 systems.
56+
*
57+
* The implementation is taken from the original memset_s proposal:
58+
* http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1381.pdf
59+
*/
60+
errno_t LegacyMemsetS(void *s, rsize_t smax __attribute__((unused)), int c, rsize_t n)
61+
{
62+
volatile unsigned char * addr = (volatile unsigned char *)s;
63+
while(n--) *addr++ = (unsigned char)c;
64+
65+
return 0;
66+
}

0 commit comments

Comments
 (0)