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

Use C99 Variable-length array if possible #88215

Closed
corona10 opened this issue May 5, 2021 · 3 comments
Closed

Use C99 Variable-length array if possible #88215

corona10 opened this issue May 5, 2021 · 3 comments
Labels
3.11 only security fixes type-feature A feature request or enhancement

Comments

@corona10
Copy link
Member

corona10 commented May 5, 2021

BPO 44049
Nosy @vstinner, @corona10, @pablogsal

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2021-05-05.13:36:38.608>
created_at = <Date 2021-05-05.12:10:58.792>
labels = ['type-feature', '3.11']
title = 'Use C99 Variable-length array if possible'
updated_at = <Date 2021-05-05.13:36:38.607>
user = 'https://github.com/corona10'

bugs.python.org fields:

activity = <Date 2021-05-05.13:36:38.607>
actor = 'corona10'
assignee = 'none'
closed = True
closed_date = <Date 2021-05-05.13:36:38.608>
closer = 'corona10'
components = []
creation = <Date 2021-05-05.12:10:58.792>
creator = 'corona10'
dependencies = []
files = []
hgrepos = []
issue_num = 44049
keywords = []
message_count = 3.0
messages = ['393006', '393007', '393008']
nosy_count = 3.0
nosy_names = ['vstinner', 'corona10', 'pablogsal']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue44049'
versions = ['Python 3.11']

@corona10
Copy link
Member Author

corona10 commented May 5, 2021

I had a chance to read Python/suggestion.c and I can notice that we use the static declared size of the array.

Since we live in the C99 era and the CPython codebase already uses C99(struct initialization is a good example), how about using the C99 feature if possible.

We should care about stack overflow from the big input but with the expected very small input size it will be okay to use.

-    static size_t buffer[MAX_STRING_SIZE];
-
     // Both strings are the same (by identity)
     if (a == b) {
         return 0;
@@ -68,6 +66,8 @@ levenshtein_distance(const char *a, size_t a_size,
         size_t t_size = a_size; a_size = b_size; b_size = t_size;
     }
+    size_t buffer[a_size];
+
     // quick fail when a match is impossible.
     if ((b_size - a_size) * MOVE_COST > max_cost) {
         return max_cost + 1

@corona10 corona10 added 3.11 only security fixes type-feature A feature request or enhancement labels May 5, 2021
@pablogsal
Copy link
Member

Thanks for the suggestion! Unfortunately I am not very convinced since we already have the self imposed limitation for the maximum size of the string (this is to limit the execution time and memory) so there is no point of allowing bigger buffers. Also consider that this may happen on threads where the stack is much much smaller, so we cannot allow arbitrary length arrays.

Also, on a minor note, we use a restricted set of c99 supported by all compilers so we would need to check if this is supported in all of them.

@corona10
Copy link
Member Author

corona10 commented May 5, 2021

Also, on a minor note, we use a restricted set of c99 supported by all compilers so we would need to check if this is supported in all of them

Ah, I notice that msvc does not support VLA (Sorry, I am not using Windows machine for 5 years +)
Using a C99/C11 features looks like a long future :(

@corona10 corona10 closed this as completed May 5, 2021
@corona10 corona10 closed this as completed May 5, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants