Skip to content

use resize instead of substr and reassigning - #6517

Open
IamRezaMousavi wants to merge 1 commit into
official-stockfish:masterfrom
IamRezaMousavi:use-resize-substr
Open

use resize instead of substr and reassigning#6517
IamRezaMousavi wants to merge 1 commit into
official-stockfish:masterfrom
IamRezaMousavi:use-resize-substr

Conversation

@IamRezaMousavi

@IamRezaMousavi IamRezaMousavi commented Jan 3, 2026

Copy link
Copy Markdown

this avoids unnecessary copying

@IamRezaMousavi IamRezaMousavi changed the title use resize instead of substr started from 0 use resize instead of substr and reassigning Jan 3, 2026
Comment thread src/shm.h Outdated
if (shm_name.size() > SF_MAX_SEM_NAME_LEN)
{
shm_name = shm_name.substr(0, SF_MAX_SEM_NAME_LEN - 1);
shm_name.resize(SF_MAX_SEM_NAME_LEN - 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The change looks fine but looking at the code I would expect that we resize the string to a size of SF_MAX_SEM_NAME_LEN rather than SF_MAX_SEM_NAME_LEN - 1.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This line does the same as the previous one.

@snicolet

snicolet commented Jan 3, 2026

Copy link
Copy Markdown
Member
            -  shm_name = shm_name.substr(0, SF_MAX_SEM_NAME_LEN - 1);
            +  shm_name.resize(SF_MAX_SEM_NAME_LEN - 1);

Why did we use SF_MAX_SEM_NAME_LEN - 1 in master for the length instead of SF_MAX_SEM_NAME_LEN, by the way?

Edit: comments crossed with UniQP

@IamRezaMousavi

Copy link
Copy Markdown
Author
            -  shm_name = shm_name.substr(0, SF_MAX_SEM_NAME_LEN - 1);
            +  shm_name.resize(SF_MAX_SEM_NAME_LEN - 1);

Why did we use SF_MAX_SEM_NAME_LEN - 1 in master for the length instead of SF_MAX_SEM_NAME_LEN, by the way?

Edit: comments crossed with UniQP

I'm not sure, but if you'd like I can modify it to use resize(SF_MAX_SEM_NAME_LEN).

@UniQP

UniQP commented Jan 3, 2026

Copy link
Copy Markdown
Contributor
            -  shm_name = shm_name.substr(0, SF_MAX_SEM_NAME_LEN - 1);
            +  shm_name.resize(SF_MAX_SEM_NAME_LEN - 1);

Why did we use SF_MAX_SEM_NAME_LEN - 1 in master for the length instead of SF_MAX_SEM_NAME_LEN, by the way?
Edit: comments crossed with UniQP

I'm not sure, but if you'd like I can modify it to use resize(SF_MAX_SEM_NAME_LEN).

Please change it to resize(SF_MAX_SEM_NAME_LEN).

@Disservin

Copy link
Copy Markdown
Member

i believe it was because we use c_str for the shm function calls and c_str is null terminated, so the actual amount of characters we can use is - 1

@UniQP

UniQP commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

i believe it was because we use c_str for the shm function calls and c_str is null terminated, so the actual amount of characters we can use is - 1

But then the check is wrong and needs to be changed to shm_name.size() >= SF_MAX_SEM_NAME_LEN.

@Disservin

Copy link
Copy Markdown
Member

i believe it was because we use c_str for the shm function calls and c_str is null terminated, so the actual amount of characters we can use is - 1

But then the check is wrong and needs to be changed to shm_name.size() >= SF_MAX_SEM_NAME_LEN.

mhh that is true..

@UniQP

UniQP commented Jan 3, 2026

Copy link
Copy Markdown
Contributor

Wait, if we use #define SF_MAX_SEM_NAME_LEN NAME_MAX and NAME_MAX is defined as the length without the null terminator, we don't need the -1. (I haven't checked the other #defines.)

@IamRezaMousavi

IamRezaMousavi commented Jan 3, 2026

Copy link
Copy Markdown
Author

the size() method returns the number of characters in the string, not including any null-termination.
so the check is ok and must be shm_name.size() > SF_MAX_SEM_NAME_LEN

here the example code:

#include <iostream>
#include <string>
#include <cstring>

#define MAX_LEN 5

int main()
{
    std::string s = "123456789";
    std::cout << "size is " << s.size() << '\n';
    
    if (s.size() > MAX_LEN) {
        s.resize(MAX_LEN);
    }
    
    std::cout << "now string is " << s << '\n';
    std::cout << "and c_str returns " << s.c_str() << '\n';
    std::cout << "and strlen returns " << strlen(s.c_str()) << '\n';

    return 0;
}

Its return:

size is 9
now string is 12345
and c_str returns 12345
and strlen returns 5 // <- we need to char[6] to store this string

@IamRezaMousavi

Copy link
Copy Markdown
Author

What should I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants