Browse Source

Remove use of non-standard function strdup

Related to #873.
Camilla Löwy 7 years ago
parent
commit
973bf29622
11 changed files with 29 additions and 19 deletions
  1. 1 3
      src/CMakeLists.txt
  2. 1 1
      src/cocoa_monitor.m
  3. 2 2
      src/cocoa_window.m
  4. 15 0
      src/init.c
  5. 1 1
      src/input.c
  6. 2 0
      src/internal.h
  7. 1 1
      src/monitor.c
  8. 0 5
      src/win32_platform.h
  9. 1 1
      src/wl_monitor.c
  10. 2 2
      src/wl_window.c
  11. 3 3
      src/x11_window.c

+ 1 - 3
src/CMakeLists.txt

@@ -90,9 +90,7 @@ set_target_properties(glfw PROPERTIES
                       POSITION_INDEPENDENT_CODE ON
                       FOLDER "GLFW3")
 
-target_compile_definitions(glfw PRIVATE
-                           _GLFW_USE_CONFIG_H
-                           $<$<BOOL:${UNIX}>:_XOPEN_SOURCE=600>)
+target_compile_definitions(glfw PRIVATE _GLFW_USE_CONFIG_H)
 target_include_directories(glfw PUBLIC
                            "$<BUILD_INTERFACE:${GLFW_SOURCE_DIR}/include>"
                            "$<INSTALL_INTERFACE:$<INSTALL_PREFIX>/include>")

+ 1 - 1
src/cocoa_monitor.m

@@ -264,7 +264,7 @@ void _glfwPollMonitorsNS(void)
         const CGSize size = CGDisplayScreenSize(displays[i]);
         char* name = getDisplayName(displays[i]);
         if (!name)
-            name = strdup("Unknown");
+            name = _glfw_strdup("Unknown");
 
         monitor = _glfwAllocMonitor(name, size.width, size.height);
         monitor->ns.displayID  = displays[i];

+ 2 - 2
src/cocoa_window.m

@@ -707,7 +707,7 @@ static const NSRange kEmptyRange = { NSNotFound, 0 };
         NSUInteger i;
 
         for (i = 0;  i < count;  i++)
-            paths[i] = strdup([[e nextObject] UTF8String]);
+            paths[i] = _glfw_strdup([[e nextObject] UTF8String]);
 
         _glfwInputDrop(window, (int) count, (const char**) paths);
 
@@ -1813,7 +1813,7 @@ const char* _glfwPlatformGetClipboardString(void)
     }
 
     free(_glfw.ns.clipboardString);
-    _glfw.ns.clipboardString = strdup([object UTF8String]);
+    _glfw.ns.clipboardString = _glfw_strdup([object UTF8String]);
 
     return _glfw.ns.clipboardString;
 }

+ 15 - 0
src/init.c

@@ -138,10 +138,25 @@ static void terminate(void)
 }
 
 
+//////////////////////////////////////////////////////////////////////////
+//////                       GLFW internal API                      //////
+//////////////////////////////////////////////////////////////////////////
+
+char* _glfw_strdup(const char* source)
+{
+    const size_t length = strlen(source);
+    char* result = calloc(length + 1, 1);
+    strcpy(result, source);
+    return result;
+}
+
+
 //////////////////////////////////////////////////////////////////////////
 //////                         GLFW event API                       //////
 //////////////////////////////////////////////////////////////////////////
 
+// Notifies shared code of an error
+//
 void _glfwInputError(int code, const char* format, ...)
 {
     _GLFWerror* error;

+ 1 - 1
src/input.c

@@ -398,7 +398,7 @@ _GLFWjoystick* _glfwAllocJoystick(const char* name,
 
     js = _glfw.joysticks + jid;
     js->present     = GLFW_TRUE;
-    js->name        = strdup(name);
+    js->name        = _glfw_strdup(name);
     js->axes        = calloc(axisCount, sizeof(float));
     js->buttons     = calloc(buttonCount + hatCount * 4, 1);
     js->hats        = calloc(hatCount, 1);

+ 2 - 0
src/internal.h

@@ -745,3 +745,5 @@ GLFWbool _glfwInitVulkan(int mode);
 void _glfwTerminateVulkan(void);
 const char* _glfwGetVulkanResultString(VkResult result);
 
+char* _glfw_strdup(const char* source);
+

+ 1 - 1
src/monitor.c

@@ -165,7 +165,7 @@ _GLFWmonitor* _glfwAllocMonitor(const char* name, int widthMM, int heightMM)
     monitor->heightMM = heightMM;
 
     if (name)
-        monitor->name = strdup(name);
+        monitor->name = _glfw_strdup(name);
 
     return monitor;
 }

+ 0 - 5
src/win32_platform.h

@@ -67,11 +67,6 @@
 #include <xinput.h>
 #include <dbt.h>
 
-#if defined(_MSC_VER)
- #include <malloc.h>
- #define strdup _strdup
-#endif
-
 // HACK: Define macros that some windows.h variants don't
 #ifndef WM_MOUSEHWHEEL
  #define WM_MOUSEHWHEEL 0x020E

+ 1 - 1
src/wl_monitor.c

@@ -52,7 +52,7 @@ static void geometry(void* data,
     monitor->heightMM = physicalHeight;
 
     snprintf(name, sizeof(name), "%s %s", make, model);
-    monitor->name = strdup(name);
+    monitor->name = _glfw_strdup(name);
 }
 
 static void mode(void* data,

+ 2 - 2
src/wl_window.c

@@ -437,7 +437,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window,
     }
 
     if (wndconfig->title)
-        window->wl.title = strdup(wndconfig->title);
+        window->wl.title = _glfw_strdup(wndconfig->title);
 
     if (wndconfig->visible)
     {
@@ -497,7 +497,7 @@ void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
 {
     if (window->wl.title)
         free(window->wl.title);
-    window->wl.title = strdup(title);
+    window->wl.title = _glfw_strdup(title);
     if (window->wl.shellSurface)
         wl_shell_surface_set_title(window->wl.shellSurface, title);
 }

+ 3 - 3
src/x11_window.c

@@ -1056,7 +1056,7 @@ static const char* getSelectionString(Atom selection)
             if (targets[i] == XA_STRING)
                 *selectionString = convertLatin1toUTF8(data);
             else
-                *selectionString = strdup(data);
+                *selectionString = _glfw_strdup(data);
         }
 
         XFree(data);
@@ -2834,7 +2834,7 @@ void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
 void _glfwPlatformSetClipboardString(const char* string)
 {
     free(_glfw.x11.clipboardString);
-    _glfw.x11.clipboardString = strdup(string);
+    _glfw.x11.clipboardString = _glfw_strdup(string);
 
     XSetSelectionOwner(_glfw.x11.display,
                        _glfw.x11.CLIPBOARD,
@@ -3026,7 +3026,7 @@ GLFWAPI void glfwSetX11SelectionString(const char* string)
     _GLFW_REQUIRE_INIT();
 
     free(_glfw.x11.primarySelectionString);
-    _glfw.x11.primarySelectionString = strdup(string);
+    _glfw.x11.primarySelectionString = _glfw_strdup(string);
 
     XSetSelectionOwner(_glfw.x11.display,
                        _glfw.x11.PRIMARY,