diff --git a/src/platforms/rcore_desktop_glfw.c b/src/platforms/rcore_desktop_glfw.c index 64596b376..b0bdf70f3 100644 --- a/src/platforms/rcore_desktop_glfw.c +++ b/src/platforms/rcore_desktop_glfw.c @@ -1829,7 +1829,7 @@ int InitPlatform(void) { CORE.Input.Gamepad.ready[i] = true; CORE.Input.Gamepad.axisCount[i] = GLFW_GAMEPAD_AXIS_LAST + 1; - strncpy(CORE.Input.Gamepad.name[i], glfwGetJoystickName(i), MAX_GAMEPAD_NAME_LENGTH - 1); + snprintf(CORE.Input.Gamepad.name[i], MAX_GAMEPAD_NAME_LENGTH, "%s", glfwGetJoystickName(i)); } } //---------------------------------------------------------------------------- @@ -2044,7 +2044,7 @@ static void WindowDropCallback(GLFWwindow *window, int count, const char **paths for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) { CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char)); - strncpy(CORE.Window.dropFilepaths[i], paths[i], MAX_FILEPATH_LENGTH - 1); + snprintf(CORE.Window.dropFilepaths[i], MAX_FILEPATH_LENGTH, "%s", paths[i]); } } } @@ -2191,7 +2191,7 @@ static void JoystickCallback(int jid, int event) // WARNING: If glfwGetJoystickName() is longer than MAX_GAMEPAD_NAME_LENGTH, // only copy up to (MAX_GAMEPAD_NAME_LENGTH -1) to destination string memset(CORE.Input.Gamepad.name[jid], 0, MAX_GAMEPAD_NAME_LENGTH); - strncpy(CORE.Input.Gamepad.name[jid], glfwGetJoystickName(jid), MAX_GAMEPAD_NAME_LENGTH - 1); + snprintf(CORE.Input.Gamepad.name[jid], MAX_GAMEPAD_NAME_LENGTH, "%s", glfwGetJoystickName(jid)); } else if (event == GLFW_DISCONNECTED) { diff --git a/src/platforms/rcore_desktop_rgfw.c b/src/platforms/rcore_desktop_rgfw.c index 211917359..2e973108b 100644 --- a/src/platforms/rcore_desktop_rgfw.c +++ b/src/platforms/rcore_desktop_rgfw.c @@ -486,14 +486,14 @@ static void RGFW_cb_dropfunc(const RGFW_event *e) CORE.Window.dropFilepaths = (char **)RL_CALLOC(1024, sizeof(char *)); CORE.Window.dropFilepaths[CORE.Window.dropFileCount] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char)); - strcpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], e->drop.value->data); + snprintf(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], MAX_FILEPATH_LENGTH, "%s", e->drop.value->data); CORE.Window.dropFileCount++; } else if (CORE.Window.dropFileCount < 1024) { CORE.Window.dropFilepaths[CORE.Window.dropFileCount] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char)); - strcpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], e->drop.value->data); + snprintf(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], MAX_FILEPATH_LENGTH, "%s", e->drop.value->data); CORE.Window.dropFileCount++; } @@ -1609,74 +1609,76 @@ void PollInputEvents(void) //----------------------------------------------------------------------------- mg_event gamepad_event; - while (mg_gamepads_check_event(&platform.minigamepad, &gamepad_event)) { + while (mg_gamepads_check_event(&platform.minigamepad, &gamepad_event)) + { int gamepadIndex = gamepad_event.gamepad->index; - switch (gamepad_event.type) { + + switch (gamepad_event.type) + { case MG_EVENT_BUTTON_PRESS: + { + int button = mg_buttonConvertTable[gamepad_event.button]; + if (button >= 0) { - int button = mg_buttonConvertTable[gamepad_event.button]; - if (button >= 0) - { - CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = 1; - CORE.Input.Gamepad.lastButtonPressed = button; - } - } break; + CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = 1; + CORE.Input.Gamepad.lastButtonPressed = button; + } + } break; case MG_EVENT_BUTTON_RELEASE: + { + int button = mg_buttonConvertTable[gamepad_event.button]; + if (button >= 0) { - int button = mg_buttonConvertTable[gamepad_event.button]; - if (button >= 0) - { - CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = 0; - if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0; - } - } break; + CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = 0; + if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0; + } + } break; case MG_EVENT_AXIS_MOVE: - { - int axis = mg_axisConvertTable[gamepad_event.axis]; + { + int axis = mg_axisConvertTable[gamepad_event.axis]; - switch (axis) { - case GAMEPAD_AXIS_LEFT_X: - case GAMEPAD_AXIS_LEFT_Y: - case GAMEPAD_AXIS_RIGHT_X: - case GAMEPAD_AXIS_RIGHT_Y: - CORE.Input.Gamepad.axisState[gamepadIndex][axis] = platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value; - break; - case GAMEPAD_AXIS_LEFT_TRIGGER: - case GAMEPAD_AXIS_RIGHT_TRIGGER: - CORE.Input.Gamepad.axisState[gamepadIndex][axis] = platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value; + switch (axis) { + case GAMEPAD_AXIS_LEFT_X: + case GAMEPAD_AXIS_LEFT_Y: + case GAMEPAD_AXIS_RIGHT_X: + case GAMEPAD_AXIS_RIGHT_Y: + { + CORE.Input.Gamepad.axisState[gamepadIndex][axis] = platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value; + } break; + case GAMEPAD_AXIS_LEFT_TRIGGER: + case GAMEPAD_AXIS_RIGHT_TRIGGER: + { + CORE.Input.Gamepad.axisState[gamepadIndex][axis] = platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value; - // Trigger button press when axis is all the way - int button = (axis == GAMEPAD_AXIS_LEFT_TRIGGER) ? GAMEPAD_BUTTON_LEFT_TRIGGER_2 : GAMEPAD_BUTTON_RIGHT_TRIGGER_2; - int pressed = (platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value >= 1.0f); + // Trigger button press when axis is all the way + int button = (axis == GAMEPAD_AXIS_LEFT_TRIGGER) ? GAMEPAD_BUTTON_LEFT_TRIGGER_2 : GAMEPAD_BUTTON_RIGHT_TRIGGER_2; + int pressed = (platform.minigamepad.gamepads[gamepadIndex].axes[gamepad_event.axis].value >= 1.0f); - CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = pressed; - if (pressed) CORE.Input.Gamepad.lastButtonPressed = button; - else if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0; - break; - } - } break; + CORE.Input.Gamepad.currentButtonState[gamepadIndex][button] = pressed; + if (pressed) CORE.Input.Gamepad.lastButtonPressed = button; + else if (CORE.Input.Gamepad.lastButtonPressed == button) CORE.Input.Gamepad.lastButtonPressed = 0; + } break; + default: break; + } + } break; case MG_EVENT_GAMEPAD_CONNECT: + { CORE.Input.Gamepad.ready[gamepadIndex] = true; CORE.Input.Gamepad.axisState[gamepadIndex][GAMEPAD_AXIS_LEFT_TRIGGER] = -1.0f; CORE.Input.Gamepad.axisState[gamepadIndex][GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f; int axisCount = 0; - for (int i = 0; i < MG_AXIS_COUNT; i += 1) { - if (platform.minigamepad.gamepads[gamepadIndex].axes[i].supported) - { - axisCount += 1; - } - else - { - break; - } + for (int i = 0; i < MG_AXIS_COUNT; i += 1) + { + if (platform.minigamepad.gamepads[gamepadIndex].axes[i].supported) axisCount += 1; + else break; } + CORE.Input.Gamepad.axisCount[gamepadIndex] = axisCount; - strcpy(CORE.Input.Gamepad.name[gamepadIndex], platform.minigamepad.gamepads[gamepadIndex].name); - break; - case MG_EVENT_GAMEPAD_DISCONNECT: - CORE.Input.Gamepad.ready[gamepadIndex] = false; - break; + snprintf(CORE.Input.Gamepad.name[gamepadIndex], MAX_GAMEPAD_NAME_LENGTH, "%s", platform.minigamepad.gamepads[gamepadIndex].name); + + } break; + case MG_EVENT_GAMEPAD_DISCONNECT: CORE.Input.Gamepad.ready[gamepadIndex] = false; break; default: break; } } diff --git a/src/platforms/rcore_desktop_sdl.c b/src/platforms/rcore_desktop_sdl.c index 8fd67b6d0..130d0c184 100644 --- a/src/platforms/rcore_desktop_sdl.c +++ b/src/platforms/rcore_desktop_sdl.c @@ -1512,9 +1512,9 @@ void PollInputEvents(void) // Event memory is now managed by SDL, so it should not be freed in SDL_EVENT_DROP_FILE, // in case data needs to be hold onto the text in SDL_EVENT_TEXT_EDITING and SDL_EVENT_TEXT_INPUT events, // a copy is required, SDL_TEXTINPUTEVENT_TEXT_SIZE is no longer necessary and has been removed - strncpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], event.drop.data, MAX_FILEPATH_LENGTH - 1); + snprintf(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], MAX_FILEPATH_LENGTH, "%s", event.drop.data); #else - strncpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], event.drop.file, MAX_FILEPATH_LENGTH - 1); + snprintf(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], MAX_FILEPATH_LENGTH, "%s", event.drop.file); SDL_free(event.drop.file); #endif @@ -1525,9 +1525,9 @@ void PollInputEvents(void) CORE.Window.dropFilepaths[CORE.Window.dropFileCount] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char)); #if defined(USING_VERSION_SDL3) - strncpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], event.drop.data, MAX_FILEPATH_LENGTH - 1); + snprintf(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], MAX_FILEPATH_LENGTH, "%s", event.drop.data); #else - strncpy(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], event.drop.file, MAX_FILEPATH_LENGTH - 1); + snprintf(CORE.Window.dropFilepaths[CORE.Window.dropFileCount], MAX_FILEPATH_LENGTH, "%s", event.drop.file); SDL_free(event.drop.file); #endif @@ -1812,8 +1812,8 @@ void PollInputEvents(void) CORE.Input.Gamepad.axisState[nextAvailableSlot][GAMEPAD_AXIS_RIGHT_TRIGGER] = -1.0f; memset(CORE.Input.Gamepad.name[nextAvailableSlot], 0, MAX_GAMEPAD_NAME_LENGTH); const char *controllerName = SDL_GameControllerNameForIndex(nextAvailableSlot); - if (controllerName != NULL) strncpy(CORE.Input.Gamepad.name[nextAvailableSlot], controllerName, MAX_GAMEPAD_NAME_LENGTH - 1); - else strncpy(CORE.Input.Gamepad.name[nextAvailableSlot], "noname", 6); + if (controllerName != NULL) snprintf(CORE.Input.Gamepad.name[nextAvailableSlot], MAX_GAMEPAD_NAME_LENGTH, "%s", controllerName); + else memcpy(CORE.Input.Gamepad.name[nextAvailableSlot], "noname", 6); } else TRACELOG(LOG_WARNING, "PLATFORM: Unable to open game controller [ERROR: %s]", SDL_GetError()); } @@ -2191,7 +2191,7 @@ int InitPlatform(void) #else const char *joystickName = SDL_GameControllerNameForIndex(i); #endif - strncpy(CORE.Input.Gamepad.name[i], joystickName, MAX_GAMEPAD_NAME_LENGTH - 1); + snprintf(CORE.Input.Gamepad.name[i], MAX_GAMEPAD_NAME_LENGTH, "%s", joystickName); CORE.Input.Gamepad.name[i][MAX_GAMEPAD_NAME_LENGTH - 1] = '\0'; } else TRACELOG(LOG_WARNING, "PLATFORM: Unable to open game controller [ERROR: %s]", SDL_GetError()); diff --git a/src/platforms/rcore_web.c b/src/platforms/rcore_web.c index 9d16c3e87..dcc83accf 100644 --- a/src/platforms/rcore_web.c +++ b/src/platforms/rcore_web.c @@ -1579,7 +1579,7 @@ static void WindowDropCallback(GLFWwindow *window, int count, const char **paths for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) { CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char)); - strncpy(CORE.Window.dropFilepaths[i], paths[i], MAX_FILEPATH_LENGTH - 1); + snprintf(CORE.Window.dropFilepaths[i], MAX_FILEPATH_LENGTH, "%s", paths[i]); } } } diff --git a/src/platforms/rcore_web_emscripten.c b/src/platforms/rcore_web_emscripten.c index ff78ac547..28f26f861 100644 --- a/src/platforms/rcore_web_emscripten.c +++ b/src/platforms/rcore_web_emscripten.c @@ -1439,7 +1439,7 @@ static void WindowDropCallback(GLFWwindow *window, int count, const char **paths for (unsigned int i = 0; i < CORE.Window.dropFileCount; i++) { CORE.Window.dropFilepaths[i] = (char *)RL_CALLOC(MAX_FILEPATH_LENGTH, sizeof(char)); - strncpy(CORE.Window.dropFilepaths[i], paths[i], MAX_FILEPATH_LENGTH - 1); + snprintf(CORE.Window.dropFilepaths[i], MAX_FILEPATH_LENGTH, "%s", paths[i]); } } } diff --git a/src/raudio.c b/src/raudio.c index 094ea7533..77b6060c8 100644 --- a/src/raudio.c +++ b/src/raudio.c @@ -1154,7 +1154,7 @@ bool ExportWaveAsCode(Wave wave, const char *fileName) // Get file name from path and convert variable name to uppercase char varFileName[256] = { 0 }; - strncpy(varFileName, GetFileNameWithoutExt(fileName), 256 - 1); + snprintf(varFileName, 256, "%s", GetFileNameWithoutExt(fileName)); // NOTE: Using function provided by [rcore] module for (int i = 0; varFileName[i] != '\0'; i++) if (varFileName[i] >= 'a' && varFileName[i] <= 'z') { varFileName[i] = varFileName[i] - 32; } // Add wave information @@ -2827,7 +2827,7 @@ static const char *GetFileNameWithoutExt(const char *filePath) static char fileName[MAX_FILENAMEWITHOUTEXT_LENGTH] = { 0 }; memset(fileName, 0, MAX_FILENAMEWITHOUTEXT_LENGTH); - if (filePath != NULL) strncpy(fileName, GetFileName(filePath), MAX_FILENAMEWITHOUTEXT_LENGTH - 1); // Get filename with extension + if (filePath != NULL) snprintf(fileName, MAX_FILENAMEWITHOUTEXT_LENGTH, "%s", GetFileName(filePath)); int fileNameLength = (int)strlen(fileName); // Get size in bytes diff --git a/src/rcore.c b/src/rcore.c index d1163a515..1f987b233 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -108,7 +108,7 @@ #include // Required for: srand(), rand(), exit() #include // Required for: FILE, fopen(), fseek(), ftell(), fread(), fwrite(), fprintf(), vprintf(), fclose(), sprintf() [Used in OpenURL()] -#include // Required for: strlen(), strncpy(), strcmp(), strrchr(), memset(), strcat() +#include // Required for: strlen(), strcmp(), strrchr(), memset(), memcpy(), strcat() #include // Required for: va_list, va_start(), va_end() [Used in TraceLog()] #include // Required for: time() [Used in InitTimer()] #include // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()] @@ -1819,7 +1819,7 @@ void TakeScreenshot(const char *fileName) Image image = { imgData, (int)((float)CORE.Window.render.width*scale.x), (int)((float)CORE.Window.render.height*scale.y), 1, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8 }; char path[MAX_FILEPATH_LENGTH] = { 0 }; - strncpy(path, TextFormat("%s/%s", CORE.Storage.basePath, fileName), MAX_FILEPATH_LENGTH - 1); + snprintf(path, MAX_FILEPATH_LENGTH, "%s", TextFormat("%s/%s", CORE.Storage.basePath, fileName)); ExportImage(image, path); // WARNING: Module required: rtextures RL_FREE(imgData); @@ -1885,12 +1885,12 @@ void TraceLog(int logType, const char *text, ...) switch (logType) { - case LOG_TRACE: strncpy(buffer, "TRACE: ", 8); break; - case LOG_DEBUG: strncpy(buffer, "DEBUG: ", 8); break; - case LOG_INFO: strncpy(buffer, "INFO: ", 7); break; - case LOG_WARNING: strncpy(buffer, "WARNING: ", 10); break; - case LOG_ERROR: strncpy(buffer, "ERROR: ", 8); break; - case LOG_FATAL: strncpy(buffer, "FATAL: ", 8); break; + case LOG_TRACE: memcpy(buffer, "TRACE: ", 7); break; + case LOG_DEBUG: memcpy(buffer, "DEBUG: ", 7); break; + case LOG_INFO: memcpy(buffer, "INFO: ", 6); break; + case LOG_WARNING: memcpy(buffer, "WARNING: ", 9); break; + case LOG_ERROR: memcpy(buffer, "ERROR: ", 7); break; + case LOG_FATAL: memcpy(buffer, "FATAL: ", 7); break; default: break; } @@ -2063,7 +2063,7 @@ bool ExportDataAsCode(const unsigned char *data, int dataSize, const char *fileN // Get file name from path char varFileName[256] = { 0 }; - strncpy(varFileName, GetFileNameWithoutExt(fileName), 256 - 1); + snprintf(varFileName, 256, "%s", GetFileNameWithoutExt(fileName)); for (int i = 0; varFileName[i] != '\0'; i++) { // Convert variable name to uppercase @@ -2507,7 +2507,7 @@ const char *GetFileNameWithoutExt(const char *filePath) if (filePath != NULL) { - strncpy(fileName, GetFileName(filePath), MAX_FILENAME_LENGTH - 1); // Get filename.ext without path + snprintf(fileName, MAX_FILENAME_LENGTH, "%s", GetFileName(filePath)); // Get filename.ext without path int fileNameLength = (int)strlen(fileName); // Get size in bytes for (int i = fileNameLength; i > 0; i--) // Reverse search '.' @@ -2561,7 +2561,6 @@ const char *GetDirectoryPath(const char *filePath) } else { - // NOTE: Be careful, strncpy() is not safe, it does not care about '\0' char *dirPathPtr = dirPath; if ((filePath[1] != ':') && (filePath[0] != '\\') && (filePath[0] != '/')) dirPathPtr += 2; // Skip drive letter, "C:" memcpy(dirPathPtr, filePath, strlen(filePath) - (strlen(lastSlash) - 1)); @@ -2579,7 +2578,7 @@ const char *GetPrevDirectoryPath(const char *dirPath) memset(prevDirPath, 0, MAX_FILEPATH_LENGTH); int dirPathLength = (int)strlen(dirPath); - if (dirPathLength <= 3) strncpy(prevDirPath, dirPath, MAX_FILEPATH_LENGTH - 1); + if (dirPathLength <= 3) snprintf(prevDirPath, MAX_FILEPATH_LENGTH, "%s", dirPath); for (int i = (dirPathLength - 1); (i >= 0) && (dirPathLength > 3); i--) { @@ -2588,7 +2587,7 @@ const char *GetPrevDirectoryPath(const char *dirPath) // Check for root: "C:\" or "/" if (((i == 2) && (dirPath[1] ==':')) || (i == 0)) i++; - strncpy(prevDirPath, dirPath, i); + memcpy(prevDirPath, dirPath, i); break; } } diff --git a/src/rlgl.h b/src/rlgl.h index d72469e11..de8f12d1a 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -2469,7 +2469,7 @@ void rlLoadExtensions(void *loader) // NOTE: String duplication rquired because glGetString() returns a const string int extensionsLength = (int)strlen(extensions); // Get extensions string size in bytes char *extensionsDup = (char *)RL_CALLOC(extensionsLength + 1, sizeof(char)); // Allocate space for copy with additional EOL byte - strncpy(extensionsDup, extensions, extensionsLength); + snprintf(extensionsDup, extensionsLength, "%s", extensions); extList[numExt] = extensionsDup; for (int i = 0; i < extensionsLength; i++) diff --git a/src/rmodels.c b/src/rmodels.c index 28fb3c9ec..ad00225d7 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -50,9 +50,9 @@ #include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2 #include "raymath.h" // Required for: Vector3, Quaternion and Matrix functionality -#include // Required for: sprintf() +#include // Required for: sprintf(), snprintf() #include // Required for: malloc(), calloc(), free() -#include // Required for: memcmp(), strlen(), strncpy() +#include // Required for: strlen(), memcmp() #include // Required for: sinf(), cosf(), sqrtf(), fabsf() #if SUPPORT_FILEFORMAT_OBJ || SUPPORT_FILEFORMAT_MTL @@ -2073,7 +2073,7 @@ bool ExportMeshAsCode(Mesh mesh, const char *fileName) // Get file name from path and convert variable name to uppercase char varFileName[256] = { 0 }; - strncpy(varFileName, GetFileNameWithoutExt(fileName), 256 - 1); // NOTE: Using function provided by [rcore] module + snprintf(varFileName, 256, "%s", GetFileNameWithoutExt(fileName)); // NOTE: Using function provided by [rcore] module for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; } // Add image information @@ -4421,7 +4421,7 @@ static Model LoadOBJ(const char *fileName) } char currentDir[MAX_FILEPATH_LENGTH] = { 0 }; - strncpy(currentDir, GetWorkingDirectory(), MAX_FILEPATH_LENGTH - 1); // Save current working directory + snprintf(currentDir, MAX_FILEPATH_LENGTH, "%s", GetWorkingDirectory()); // Save current working directory const char *workingDir = GetDirectoryPath(fileName); // Switch to OBJ directory for material path correctness if (CHDIR(workingDir) != 0) TRACELOG(LOG_WARNING, "MODEL: [%s] Failed to change working directory", workingDir); @@ -5377,7 +5377,7 @@ static BoneInfo *LoadBoneInfoGLTF(cgltf_skin skin, int *boneCount) for (unsigned int i = 0; i < skin.joints_count; i++) { cgltf_node node = *skin.joints[i]; - if (node.name != NULL) strncpy(bones[i].name, node.name, sizeof(bones[i].name) - 1); + if (node.name != NULL) snprintf(bones[i].name, sizeof(bones[i].name), "%s", node.name); // Find parent bone index by walking up the node tree past any // non-joint ancestors (intermediate transform nodes used by some @@ -6660,7 +6660,7 @@ static ModelAnimation *LoadModelAnimationsGLTF(const char *fileName, int *animCo animDuration = (time > animDuration)? time : animDuration; } - if (animData.name != NULL) strncpy(animations[a].name, animData.name, sizeof(animations[a].name) - 1); + if (animData.name != NULL) snprintf(animations[a].name, sizeof(animations[a].name), "%s", animData.name); animations[a].keyframeCount = (int)(animDuration*GLTF_FRAMERATE) + 1; animations[a].keyframePoses = (Transform **)RL_CALLOC(animations[a].keyframeCount, sizeof(Transform *)); @@ -7150,7 +7150,7 @@ static Model LoadM3D(const char *fileName) for (i = 0; i < (int)m3d->numbone; i++) { model.skeleton.bones[i].parent = m3d->bone[i].parent; - strncpy(model.skeleton.bones[i].name, m3d->bone[i].name, sizeof(model.skeleton.bones[i].name) - 1); + snprintf(model.skeleton.bones[i].name, sizeof(model.skeleton.bones[i].name), "%s", m3d->bone[i].name); model.skeleton.bindPose[i].translation.x = m3d->vertex[m3d->bone[i].pos].x*m3d->scale; model.skeleton.bindPose[i].translation.y = m3d->vertex[m3d->bone[i].pos].y*m3d->scale; model.skeleton.bindPose[i].translation.z = m3d->vertex[m3d->bone[i].pos].z*m3d->scale; @@ -7258,14 +7258,14 @@ static ModelAnimation *LoadModelAnimationsM3D(const char *fileName, int *animCou animations[a].keyframeCount = m3d->action[a].durationmsec/M3D_ANIMDELAY; animations[a].keyframePoses = (Transform **)RL_CALLOC(animations[a].keyframeCount, sizeof(Transform *)); - strncpy(animations[a].name, m3d->action[a].name, sizeof(animations[a].name) - 1); + snprintf(animations[a].name, sizeof(animations[a].name), "%s", m3d->action[a].name); TRACELOG(LOG_INFO, "MODEL: [%s] Loaded animation: %s | Frames: %d | Duration: %fs", fileName, animations[a].name, animations[a].keyframeCount, m3d->action[a].durationmsec); for (i = 0; i < (int)m3d->numbone; i++) { bones[i].parent = m3d->bone[i].parent; - strncpy(bones[i].name, m3d->bone[i].name, sizeof(bones[i].name) - 1); + snprintf(bones[i].name, sizeof(bones[i].name), "%s", m3d->bone[i].name); } // A special, never transformed "no bone" bone, used for boneless vertices diff --git a/src/rtext.c b/src/rtext.c index 9d5943ce8..acd1a5433 100644 --- a/src/rtext.c +++ b/src/rtext.c @@ -58,8 +58,8 @@ #include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 2.1, 3.3+ or ES2 -> Only DrawTextPro() #include // Required for: malloc(), free() -#include // Required for: vsprintf() -#include // Required for: strcmp(), strstr(), strncpy() [Used in TextReplace()], sscanf() [Used in LoadBMFont()] +#include // Required for: vsprintf(), snprintf() +#include // Required for: strcmp(), strstr(), strncpy(), sscanf() [Used in LoadBMFont()] #include // Required for: va_list, va_start(), vsprintf(), va_end() [Used in TextFormat()] #include // Required for: toupper(), tolower() [Used in TextToUpper(), TextToLower()] @@ -536,7 +536,7 @@ Font LoadFontFromMemory(const char *fileType, const unsigned char *fileData, int Font font = { 0 }; char fileExtLower[16] = { 0 }; - strncpy(fileExtLower, TextToLower(fileType), 16 - 1); + snprintf(fileExtLower, 16, "%s", TextToLower(fileType)); font.baseSize = fontSize; font.glyphPadding = 0; @@ -1030,7 +1030,7 @@ bool ExportFontAsCode(Font font, const char *fileName) // Get file name from path char fileNamePascal[256] = { 0 }; - strncpy(fileNamePascal, TextToPascal(GetFileNameWithoutExt(fileName)), 256 - 1); + snprintf(fileNamePascal, 256, "%s", TextToPascal(GetFileNameWithoutExt(fileName))); // Get font atlas image and size, required to estimate code file size // NOTE: This mechanism is highly coupled to raylib @@ -1532,7 +1532,7 @@ char **LoadTextLines(const char *text, int *count) if ((text[i] == '\n') || (text[i] == '\0')) { lines[l] = (char *)RL_CALLOC(lineLen + 1, 1); - strncpy(lines[l], &text[i - lineLen], lineLen); + memcpy(lines[l], &text[i - lineLen], lineLen); lineLen = 0; l++; } @@ -1755,8 +1755,8 @@ char *GetTextBetween(const char *text, const char *begin, const char *end) { endIndex += (beginIndex + beginLen); int len = (endIndex - beginIndex - beginLen); - if (len < (MAX_TEXT_BUFFER_LENGTH - 1)) strncpy(buffer, text + beginIndex + beginLen, len); - else strncpy(buffer, text + beginIndex + beginLen, MAX_TEXT_BUFFER_LENGTH - 1); + if (len < (MAX_TEXT_BUFFER_LENGTH - 1)) memcpy(buffer, text + beginIndex + beginLen, len); + else snprintf(buffer, MAX_TEXT_BUFFER_LENGTH, "%s", text + beginIndex + beginLen); } } @@ -1764,7 +1764,7 @@ char *GetTextBetween(const char *text, const char *begin, const char *end) } // Replace text string -// REQUIRES: strstr(), strncpy() +// REQUIRES: strstr(), memcpy() // NOTE: Limited text replace functionality, using static string char *TextReplace(const char *text, const char *search, const char *replacement) { @@ -1821,7 +1821,7 @@ char *TextReplace(const char *text, const char *search, const char *replacement) // Copy remaind text part after replacement to result (pointed by moving temp) // NOTE: Text pointer internal copy has been updated along the process - strncpy(tempPtr, text, TextLength(text)); + memcpy(tempPtr, text, TextLength(text)); } else TRACELOG(LOG_WARNING, "Text with replacement is longer than internal buffer, use TextReplaceAlloc()"); } @@ -1830,7 +1830,7 @@ char *TextReplace(const char *text, const char *search, const char *replacement) } // Replace text string -// REQUIRES: strstr(), strncpy() +// REQUIRES: strstr(), memcpy() // WARNING: Allocated memory must be manually freed char *TextReplaceAlloc(const char *text, const char *search, const char *replacement) { @@ -1887,7 +1887,7 @@ char *TextReplaceAlloc(const char *text, const char *search, const char *replace // Copy remaind text part after replacement to result (pointed by moving temp) // NOTE: Text pointer internal copy has been updated along the process - strncpy(temp, text, TextLength(text)); + memcpy(temp, text, TextLength(text)); } } diff --git a/src/rtextures.c b/src/rtextures.c index 41ee1ffd1..219067a66 100644 --- a/src/rtextures.c +++ b/src/rtextures.c @@ -813,7 +813,7 @@ bool ExportImageAsCode(Image image, const char *fileName) // Get file name from path and convert variable name to uppercase char varFileName[256] = { 0 }; - strncpy(varFileName, GetFileNameWithoutExt(fileName), 256 - 1); // NOTE: Using function provided by [rcore] module + snprintf(varFileName, 256, "%s", GetFileNameWithoutExt(fileName)); // NOTE: Using function provided by [rcore] module for (int i = 0; varFileName[i] != '\0'; i++) if ((varFileName[i] >= 'a') && (varFileName[i] <= 'z')) { varFileName[i] = varFileName[i] - 32; } // Add image information diff --git a/tools/rexm/rexm.c b/tools/rexm/rexm.c index f268a7d44..cb753e031 100644 --- a/tools/rexm/rexm.c +++ b/tools/rexm/rexm.c @@ -2331,7 +2331,7 @@ static rlExampleInfo *LoadExampleInfo(const char *exFileName) // Example found in collection exInfo = (rlExampleInfo *)RL_CALLOC(1, sizeof(rlExampleInfo)); - strncpy(exInfo->name, GetFileNameWithoutExt(exFileName), 128 - 1); + snprintf(exInfo->name, 128, "%s", GetFileNameWithoutExt(exFileName)); strncpy(exInfo->category, exInfo->name, TextFindIndex(exInfo->name, "_")); char *exText = LoadFileText(exFileName); @@ -2377,7 +2377,7 @@ static rlExampleInfo *LoadExampleInfo(const char *exFileName) int copyrightIndex = TextFindIndex(exText, "Copyright (c) "); int yearStartIndex = copyrightIndex + 14; char yearText[5] = { 0 }; - strncpy(yearText, exText + yearStartIndex, 4); + snprintf(yearText, 5, "%s", exText + yearStartIndex); exInfo->yearCreated = TextToInteger(yearText); // Check for review year included (or just use creation year) if (exText[yearStartIndex + 4] == '-') strncpy(yearText, exText + yearStartIndex + 5, 4); @@ -2425,7 +2425,7 @@ static int ParseExampleInfoLine(const char *line, rlExampleInfo *entry) #define MAX_EXAMPLE_INFO_LINE_LEN 512 char temp[MAX_EXAMPLE_INFO_LINE_LEN] = { 0 }; - strncpy(temp, line, MAX_EXAMPLE_INFO_LINE_LEN); + snprintf(temp, MAX_EXAMPLE_INFO_LINE_LEN, "%s", line); temp[MAX_EXAMPLE_INFO_LINE_LEN - 1] = '\0'; // Ensure null termination int tokenCount = 0; @@ -2902,7 +2902,7 @@ static void UpdateWebMetadata(const char *exHtmlPath, const char *exFilePath) char exTitle[64] = { 0 }; // Example title: fileName without extension, replacing underscores by spaces // Get example name: replace underscore by spaces - strncpy(exName, GetFileNameWithoutExt(exHtmlPathCopy), 64 - 1); + snprintf(exName, 64, "%s", GetFileNameWithoutExt(exHtmlPathCopy)); strcpy(exTitle, exName); for (int i = 0; (i < 64) && (exTitle[i] != '\0'); i++) { if (exTitle[i] == '_') exTitle[i] = ' '; }