-
Notifications
You must be signed in to change notification settings - Fork 29
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
A prototype for solve dividing two integers #66
base: main
Are you sure you want to change the base?
Conversation
for rounding down to zero for integer division
pyclibrary/c_parser.py
Outdated
@@ -35,6 +35,91 @@ | |||
__all__ = ['win_defs', 'CParser'] | |||
|
|||
|
|||
def wrap_int(t): | |||
logger.debug('wrap_int: {} {}'.format(t.dump(), type(t))) | |||
t[0] = "CInt({})".format(eval(t[0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rewrite without using eval.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hello @kalvdans,
Thanks for your reply, avoiding using eval is a very good simple suggestion to avoid security issues and everyone should know.
One solution is to write a C integer parser, but I'm not sure it's worth writing. Because my purpose is to bind existing open source c libraries. If some people who want to use untrusted libraries may be worried about this.
Also, I think must write in clean code for better maintenance, but I don't know of any python built-in function that can convert C integer string to a Python integer. If you know a function, I will appreciate if you tell me :)
See also this code https://github.com/r888800009/pyclibrary/blob/f5f8bf80113b9deb247e92705bb4b73529b95912/pyclibrary/c_parser.py#L1736-L1739
The token passed in by wrapper must match the regular expression. If you know how to bypass this regex to achieve python execute code, please tell me and I will be able to confirm that this is indeed a bad approach
Of course, to avoid misuse, we'd better avoiding using eval, but we also need have a good enough solution to make it easy to maintain.
For your concerns, maybe you'll want to look at the existing code in c_parser.py
pyclibrary/pyclibrary/c_parser.py
Line 1514 in 1d4dbfc
return eval(expr, *args) |
If we care about security issues, maybe we can consider rewriting the eval function of the entire project
Best regards
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not check in depth but could you use ast.literal_eval ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, int(t[0], 0)
should handle 0x prefixes fine, but doesn't work with octal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pyclibrary/c_parser.py
Outdated
@@ -35,6 +35,91 @@ | |||
__all__ = ['win_defs', 'CParser'] | |||
|
|||
|
|||
def wrap_int(t): | |||
logger.debug('wrap_int: {} {}'.format(t.dump(), type(t))) | |||
t[0] = "CInt({})".format(int(t[0], 0)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we return a string from here? I would expect this function to return a CInt instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realise this should be part of a bigger rewrite, so no need to change in this PR.
pyclibrary/c_parser.py
Outdated
# The floating regex is ugly but it is because we do not want to match | ||
# integer to it. | ||
floating = Regex(r'[+-]?\s*((((\d(\.\d*)?)|(\.\d+))[eE][+-]?\d+)|((\d\.\d*)|(\.\d+)))') | ||
number = (floating | integer) | ||
number = (floating |integer) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coding style: use spaces around binary operators
@@ -290,6 +290,17 @@ def test_values(self): | |||
macros['MACRO_H3'] == '0X000002UL' and | |||
values['MACRO_H3'] == 2) | |||
|
|||
# Octal integer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great tests!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
@@ -566,6 +577,24 @@ def test_variables(self): | |||
assert ('x2' in variables and | |||
variables['x2'] == (88342528, Type('int'))) | |||
|
|||
# Test int div 9 / 2 should be 4 | |||
assert ('x3' in variables and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assert ('x3' in variables and | |
assert ( |
the indexing will anyway if the key does not exist in the dict. No need to test it separately.
hello I created a prototype to solve issue #65, but need to do more test cases and evaluate how to improve this code
I created a CInt wrapper, and wrapped the integer in the parsing stage, which can be divided as a c integer in eval.
However, in performance, I have not yet evaluated it, but it has passed the original and new test cases.
Need more discussion, thanks