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

A prototype for solve dividing two integers #66

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

r888800009
Copy link

@r888800009 r888800009 commented Apr 10, 2023

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

@r888800009 r888800009 changed the title PoC for solve dividing two integers A prototype for solve dividing two integers Apr 11, 2023
@@ -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]))

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.

Copy link
Author

@r888800009 r888800009 Nov 2, 2023

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

return eval(expr, *args)

If we care about security issues, maybe we can consider rewriting the eval function of the entire project

Best regards

Copy link
Owner

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 ?

Copy link

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.

Copy link
Author

Choose a reason for hiding this comment

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

It seems we can simply write an octal parser 44daba6 to solve this problem without too much complexity.

new patch a7f7624 uses int(t[0], 0), which can restrict the input of only integer.

@@ -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))
Copy link

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.

Copy link

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.

# 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)
Copy link

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
Copy link

Choose a reason for hiding this comment

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

great tests!

Copy link

@kalvdans kalvdans left a 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
Copy link

Choose a reason for hiding this comment

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

Suggested change
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.

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.

3 participants