Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128394: Parse int for lltrace #128395

Closed
wants to merge 1 commit into from
Closed

Conversation

WolframAlph
Copy link
Contributor

@WolframAlph WolframAlph commented Jan 1, 2025

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

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

Thanks for doing this. atoi() is quite broken and we definitely shouldn't use it here (for example, inputting something outside the number range is UB). You probably want something like PyOS_strtol instead.

@WolframAlph
Copy link
Contributor Author

@ZeroIntensity

atoi() is quite broken and we definitely shouldn't use it here (for example, inputting something outside the number range is UB).

  1. That's true, but my understanding is that people running locally debug builds know what they are doing and trying to deliberately cause UB does not make sense.
  2. strtol and specifically PyOS_strtol is better option of course but that requires to check errno if we intend to use it properly which seems like overkill to me in this scenario. Would it make sense to use it without checking for errno just to rule out UB and keep things simple? WDYT

@ZeroIntensity
Copy link
Member

You can probably just fatal error if it fails.

@WolframAlph
Copy link
Contributor Author

WolframAlph commented Jan 1, 2025

@ZeroIntensity since PyOS_strtol returns long and lltrace is int, we need a bit more code to handle things correctly. Im thinking about creating standalone function for retrieving lltrace related env variables something like:

static inline int get_lltrace(const char* env_var) {
    int lltrace = 0;
    char* value = Py_GETENV(env_var);
    if (value != NULL) {
        long result = PyOS_strtol(value, NULL, 10);
        if (errno == ERANGE || result < 0 || result > INT_MAX) {
            char buff[100];
            PyOS_snprintf(buff, sizeof(buff), "Ivalid value for: %s", env_var);
            Py_FatalError(buff);
        }
        lltrace = (int)result;
    }
    return lltrace;
}

WDYT? The only question is where to put it

@ZeroIntensity
Copy link
Member

Sure, that works. Anywhere in the file is fine (prefereably near the lltrace code).

@Fidget-Spinner
Copy link
Member

I'm of the opinion that the added complexity to parse it isn't worth it. IIRC that comment was added by Guido, but I've never found a use for actually parsing an int? We never use any levels above 5 anyways. So it would only matter if we were to use two digit numerals (10 and onwards), which I don't see us using anytime soon.

I'd rather we just remove the comment. If we need to parse ints in the future, we can just do it then rather than preparing for something which might never happen.

@WolframAlph
Copy link
Contributor Author

WolframAlph commented Jan 2, 2025

@Fidget-Spinner what about defining this logic somewhere else? #128395 (comment). This piece of logic is used across multiple files and copied over every time someone needs it. Might be handy and cleaner IMO

@Fidget-Spinner
Copy link
Member

@Fidget-Spinner what about defining this logic somewhere else? #128395 (comment). This piece of logic is used across multiple files and copied over every time someone needs it. Might be handy and cleaner IMO

That would be nice. Thanks.

@gvanrossum
Copy link
Member

I'm sorry, this does not deserve to be fixed. For an explanation see the issue.

@gvanrossum gvanrossum closed this Jan 2, 2025
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.

5 participants