From 6e5b062650d937a3a1c7af3bf112fe61d54d6a78 Mon Sep 17 00:00:00 2001 From: culler Date: Mon, 18 Nov 2019 15:05:55 +0000 Subject: Edit comments, add tests for NULL pointers. --- macosx/tkMacOSXClipboard.c | 11 ++++++----- macosx/tkMacOSXFont.c | 42 +++++++++++++++++++++++++++++------------- macosx/tkMacOSXPrivate.h | 2 +- 3 files changed, 36 insertions(+), 19 deletions(-) diff --git a/macosx/tkMacOSXClipboard.c b/macosx/tkMacOSXClipboard.c index bdcccf9..db45428 100644 --- a/macosx/tkMacOSXClipboard.c +++ b/macosx/tkMacOSXClipboard.c @@ -135,19 +135,20 @@ TkSelGetSelection( string = [pb stringForType:type]; } if (string) { - int utfSize; - char *tclUni = NSStringToTclUni(string, &utfSize); /* - * Re-encode the string using the encoding which is used in Tcl + * Encode the string using the encoding which is used in Tcl * when TCL_UTF_MAX = 3. This replaces each UTF-16 surrogate with * a 3-byte sequence generated using the UTF-8 algorithm. (Even * though UTF-8 does not allow encoding surrogates, the algorithm * does produce a 3-byte sequence.) */ - result = proc(clientData, interp, tclUni); - ckfree(tclUni); + char *bytes = NSStringToTclUni(string, NULL); + result = proc(clientData, interp, bytes); + if (bytes) { + ckfree(bytes); + } } } else { Tcl_SetObjResult(interp, Tcl_ObjPrintf( diff --git a/macosx/tkMacOSXFont.c b/macosx/tkMacOSXFont.c index aeb90f8..9b2d11c 100644 --- a/macosx/tkMacOSXFont.c +++ b/macosx/tkMacOSXFont.c @@ -104,7 +104,7 @@ static void DrawCharsInContext(Display *display, Drawable drawable, /* *--------------------------------------------------------------------------- * - * NSStringFromTclUTF -- + * TclUniToNSString -- * * When Tcl is compiled with TCL_UTF_MAX = 3 (the default for 8.6) it cannot * deal directly with UTF-8 encoded non-BMP characters, since their UTF-8 @@ -163,8 +163,9 @@ TclUniToNSString( * Returns the number of bytes written. * * Side effects: - * Bytes are written to the address uni and the unicode code point is written - * to the integer at address code. + * Bytes are written to the char array referenced by the pointer uni and + * the unicode code point is written to the integer referenced by the + * pointer code. * */ @@ -199,14 +200,22 @@ TclUniAtIndex( * * NSStringToTclUni -- * - * Encodes the unicode string represented by an NSString object using the - * special internal Tcl encoding used when TCL_UTF_MAX = 3. This encoding + * Encodes the unicode string represented by an NSString object with the + * internal encoding that Tcl uses when TCL_UTF_MAX = 3. This encoding * is similar to UTF-8 except that non-BMP characters are encoded as two * successive 3-byte sequences which are constructed from UTF-16 surrogates * by applying the UTF-8 algorithm. Even though the UTF-8 encoding does not * allow encoding surrogates, the algorithm does produce a well-defined * 3-byte sequence. * + * Results: + * Returns a pointer to a null-terminated byte array which encodes the + * NSString. + * + * Side effects: + * Memory is allocated to hold the byte array, which must be freed with + * ckalloc. If the pointer numBytes is not NULL the number of non-null + * bytes written to the array is stored in the integer it references. */ MODULE_SCOPE char* @@ -215,16 +224,23 @@ NSStringToTclUni( int *numBytes) { unsigned int code; - int i, length = [string length]; - char *ptr, *result = ckalloc(6*length + 1); - for (i = 0, ptr = result; i < length; i++) { - ptr += TclUniAtIndex(string, i, ptr, &code); - if (code > 0xffff){ - i++; + int i; + char *ptr, *bytes = ckalloc(6*[string length] + 1); + + ptr = bytes; + if (ptr) { + for (i = 0; i < [string length]; i++) { + ptr += TclUniAtIndex(string, i, ptr, &code); + if (code > 0xffff){ + i++; + } } + *ptr = '\0'; + } + if (numBytes) { + *numBytes = ptr - bytes; } - *ptr = '\0'; - return result; + return bytes; } #define GetNSFontTraitsFromTkFontAttributes(faPtr) \ diff --git a/macosx/tkMacOSXPrivate.h b/macosx/tkMacOSXPrivate.h index a285bba..9417b62 100644 --- a/macosx/tkMacOSXPrivate.h +++ b/macosx/tkMacOSXPrivate.h @@ -239,9 +239,9 @@ MODULE_SCOPE int TkMacOSXServices_Init(Tcl_Interp *interp); MODULE_SCOPE int TkMacOSXRegisterServiceWidgetObjCmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]); +MODULE_SCOPE NSString* TclUniToNSString(const char *source, int numBytes); MODULE_SCOPE int TclUniAtIndex(NSString *string, int index, char *uni, unsigned int *code); -MODULE_SCOPE NSString* TclUniToNSString(const char *source, int numBytes); MODULE_SCOPE char* NSStringToTclUni(NSString *string, int *numBytes); #pragma mark Private Objective-C Classes -- cgit v0.12