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

pySCG: Adding documentation to CWE-175 as part of #531 #687

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

Conversation

s19110
Copy link
Contributor

@s19110 s19110 commented Oct 31, 2024

Adding documentation to CWE-175 as part of #531

Signed-off-by: edanhub <hubert.daniszewski@ericsson.com>
Copy link
Contributor

Choose a reason for hiding this comment

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

code not working, line 12 and 15 are missing locale.CURRENT_LOCALE

@myteron myteron changed the title Adding documentation to CWE-175 as part of #531 pySCG: Adding documentation to CWE-175 as part of #531 Nov 6, 2024
Signed-off-by: edanhub <hubert.daniszewski@ericsson.com>
```py
""" Code Example """
import locale
word = "Title"
Copy link
Contributor

Choose a reason for hiding this comment

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

Getting:
Constant name "word" doesn't conform to UPPER_CASE naming stylePylintC0103:invalid-name
Maybe just capitalise "word" variable to silence the linters?


dt = datetime.datetime(2022, 3, 9, 12, 55, 35, 000000)

def get_date(date):
Copy link
Contributor

Choose a reason for hiding this comment

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

Getting the following on Flake:
expected 2 blank lines, found 1Flake8(E302)
Missing function or method docstringPylintC0116:missing-function-docstring

# Trying to exploit above code example
#####################

CURRENT_LOCALE = 'en_IE.utf8'
Copy link
Contributor

Choose a reason for hiding this comment

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

Getting the following on Flake:
expected 2 blank lines after class or function definition, found 1Flake8(E305)
(constant) CURRENT_LOCALE: Literal['en_IE.utf8']

# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
""" Compliant Code Example """
import datetime, locale
Copy link
Contributor

Choose a reason for hiding this comment

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

Getting these linters giving out about multiple imports on one line:
Multiple imports on one lineRuffE401
Multiple imports on one line (datetime, locale)PylintC0410:multiple-imports
multiple imports on one lineFlake8(E401)
(module) datetime

# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
""" Non-compliant Code Example """
import datetime, locale
Copy link
Contributor

Choose a reason for hiding this comment

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

Linters giving out about linters on one line:
Multiple imports on one lineRuffE401
Multiple imports on one line (datetime, locale)PylintC0410:multiple-imports
multiple imports on one lineFlake8(E401)
(module) datetime

# Trying to exploit above code example
#####################

locale.setlocale(locale.LC_ALL, CURRENT_LOCALE)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you forget to initialise CURRENT_LOCALE in here?


For example, reading values from a data file values might be misinterpreted if the developer is unaware that the program locale does not accommodate the data locale.

This simple code example does not set a locale and sets `ORIGINAL_NUMBER` for comparison to 12.345 (twelve point three-four-five). In Ireland, a comma is a thousands separator and a dot is a decimal separator. In Germany these are reversed, so a comma is a decimal separator and a decimal is a thousands separator.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
This simple code example does not set a locale and sets `ORIGINAL_NUMBER` for comparison to 12.345 (twelve point three-four-five). In Ireland, a comma is a thousands separator and a dot is a decimal separator. In Germany these are reversed, so a comma is a decimal separator and a decimal is a thousands separator.
The `example03.py` code does not set a locale and sets `ORIGINAL_NUMBER` for comparison to 12.345 (twelve point three-four-five). In Ireland, a comma is a thousands separator and a dot is a decimal separator. In Germany these are reversed, so a comma is a decimal separator and a decimal is a thousands separator.

@@ -0,0 +1,27 @@
# SPDX-FileCopyrightText: OpenSSF project contributors
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that example03.py can be simplified and that we should keep CLI interaction to a minimum. Its enough to experience that a 12.345 with a German local results in 12345.0.

Suggested change
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-FileCopyrightText: OpenSSF project contributors
# SPDX-License-Identifier: MIT
""" Non-compliant Code Example """
import locale
def compare_number(number):
input_number = locale.atof(input(f"Enter a number {number}: "))
print(f"Locale is {locale.getlocale()}, you entered {input_number}.")
print(f"Does the number {number} match {input_number}? {number == input_number}")
locale.setlocale(locale.LC_ALL, 'de_DE.utf8')
compare_number(12.345)

import locale
ORIGINAL_NUMBER = 12.345 # This will read as 12,345 in German

def compare_number(number):
Copy link
Contributor

Choose a reason for hiding this comment

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

Getting this from Flake:
expected 2 blank lines, found 1Flake8(E302)
Missing function or method docstringPylintC0116:missing-function-docstring

# Test if inputted number equals current number
return number == input_number

print(f"Locale is {locale.getlocale()}")
Copy link
Contributor

Choose a reason for hiding this comment

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

Getting this from Flake:
expected 2 blank lines after class or function definition, found 1Flake8(E305)
(function) def print(
*values: object,
sep: str | None = " ",
end: str | None = "\n",
file: SupportsWrite[str] | None = None,
flush: Literal[False] = False
) -> None
Prints the values to a stream, or to sys.stdout by default.

sep
string inserted between values, default a space.

print(f"Locale is {locale.getlocale()}")
print(f"Do the numbers match? {compare_number(ORIGINAL_NUMBER)}")

## Locale is ('English_Ireland', '1252')
Copy link
Contributor

Choose a reason for hiding this comment

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

Linter also gives out everytime there is more than one "#" for me
too many leading '#' for block commentFlake8(E266)

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""

output = io.BytesIO()
Copy link
Contributor

Choose a reason for hiding this comment

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

2 spaces here after "output"
Would change it myself but it won't let me!

""" Non-compliant Code Example """
import io

LOREM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Copy link
Contributor

Choose a reason for hiding this comment

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

Got a lot of "Line too long" from Pylint... not sure if we care about this though:
Line too long (456/100)PylintC0301:line-too-long
(constant) LOREM: Literal['Lorem ipsum dolor sit amet, consectetur adipiscing…']

Got that for lines 6,7,8 and 18

""" Compliant Code Example """
import io

LOREM = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Copy link
Contributor

Choose a reason for hiding this comment

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

Pylint giving out that lines 6, 7 and 8 are too long... not sure if we care though...

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."""

output = io.BytesIO()
Copy link
Contributor

Choose a reason for hiding this comment

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

2 spaces after "output" here

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.

3 participants