Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@palxex
Copy link
Member

@palxex palxex commented Mar 10, 2024

Test Cases( from @PalAlexx ):

  1. a simple MOD on baiyou win version:
    pal98.zip

Test method: Replace the files and open any save file(.RPG) that any PC have the magic "CoinDartII(乾坤一掷)", see its (altered) description in magic menu.

Expected: two line 10/11 seen

Actually (before this PR merged): nothing

Explanation: This mod appends two new messages(and its indexes too) to the ending, and let two previous-existing message index (second argument of 0xffff script command) points to the new ones. It makes the count-based g_TextLib.nMsgs (incorrctly) keeps not changes( since there was two old one not used, and two new one added ), which denies new added messages from displaying.

  1. a similar MOD on debug3.1 dos version:
    abnormal.zip

Test Method: Replace the files and begin a new game session, look at the first 3 lines Li's Aunt said

Expected:

Li's Aunt:
Xiaoyao! Hurry up and
come serve customers

Actually (before this PR merged):

Li's Aunt:
(NOTHING HERE)

Explanation: This mod directly modify the message index scripts and the corresponding sid/eid of the corresponding block in the generated slf . But the modified number numbers are far bigger than(14702/14703) the counted nMsgs(12xxx), which denies the index-moved messages from displaying.

What this patch did:
Instead of counting, makes nMsgs be the max number of all the IDs, and allow the middle message be null, in order to make the two kind of mod works.

NEED TESTS:
Since this patch seems affects whole lifecycle of the game, I request review from other admins, or wide tests from whole society to ensure it may not introducing new bugs.

  • Have you checked to ensure there aren't other open Pull Requests for the same change?

  • Have you added an explanation of what your changes do and why you'd like us to include them?

  • How many dependencies was introduced in this PR? Did the minimal requirement changed, for which platform?
    0

  • Have you written new tests for your changes?

  • Have you successfully run it with your changes locally?

  • Have you tested on following platforms?

    • Win32
    • UWP
    • Linux
    • Android
    • macOS
    • iOS
  • I certify that I have the right and agree to submit my contributions under the terms of GNU General Public License, version 3 (or any later version at the choice of the maintainers of the SDLPAL Project) as published by the Free Software Foundation.

@palxex palxex requested a review from louyihua March 10, 2024 15:01
@palxex palxex force-pushed the pr_fix_slf_display branch from 31445b3 to 0a5e945 Compare March 11, 2024 15:37
@palxex palxex force-pushed the pr_fix_slf_display branch from 0a5e945 to c316367 Compare March 13, 2024 01:33
@palxex palxex requested review from Copilot and removed request for louyihua June 11, 2025 06:31
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes handling of special SLF message IDs so mods with non-sequential or high IDs display correctly. It also increases the message buffer size and improves safe freeing of message strings.

  • MESSAGE_MAX_BUFFER_SIZE raised from 512 to 1024
  • Introduced maxID tracking to compute g_TextLib.nMsgs over non-sequential IDs
  • Adjusted allocation and free loops for lpMsgBuf based on the new nMsgs
Comments suppressed due to low confidence (4)

text.c:40

  • [nitpick] Consider adding a comment explaining why MESSAGE_MAX_BUFFER_SIZE was increased from 512 to 1024 so future maintainers understand the rationale.
#define   MESSAGE_MAX_BUFFER_SIZE   1024

text.c:561

  • Add unit tests for message files with non-sequential or very large ID values to verify that the new nMsgs calculation correctly allocates space for all indices.
g_TextLib.nMsgs = max(maxID, msg_cnt) + 1;

text.c:267

  • The standard C library doesn't provide a max macro by default. Either include or define a portable MAX macro (or use (maxID > eid ? maxID : eid)) to avoid compilation errors.
maxID = max(maxID, eid);

text.c:561

  • Similar to the earlier use of max, ensure a MAX macro is defined or replace this with an inline conditional expression to guarantee portability and correct sizing.
g_TextLib.nMsgs = max(maxID, msg_cnt) + 1;

if (msg->value)
{
g_TextLib.lpIndexBuf[item->index][item->indexEnd - item->index][index++] = idx_msg;
assert(idx_msg < g_TextLib.nMsgs);
Copy link

Copilot AI Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relying solely on assert means this bounds check is removed in release builds. Consider adding an explicit runtime check or error handling to prevent potential buffer overflow in production.

Suggested change
assert(idx_msg < g_TextLib.nMsgs);
if (idx_msg >= g_TextLib.nMsgs) {
fprintf(stderr, "Error: idx_msg (%d) exceeds g_TextLib.nMsgs (%d). Potential buffer overflow.\n", idx_msg, g_TextLib.nMsgs);
exit(EXIT_FAILURE);
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants