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

CWE-366 Race Condition within a Thread Python version dependent #704

Open
BartyBoi1128 opened this issue Dec 5, 2024 · 0 comments
Open

Comments

@BartyBoi1128
Copy link
Contributor

Trying to demonstrate a non-compliant race condition, I'm having an issue where race conditions seem to not work on python versions that are higher than python3.10. Whatever example I seem to find online doesn't work on my version of python, which is python 3.11.4.

Not too sure what to do about this, but here is my code example demonstrating a race condition. This seems to work on Python 3.9, but not on anything that's version 3.10 or higher,

""" Non-compliant Code Example """
import logging
import sys
from threading import Thread
 
logging.basicConfig(level=logging.INFO)
 
 
class Number():
    """
    Multithreading incompatible class missing locks.
    Issue only occures with more then 1 million repetitions.
    """
    value = 0
    repeats = 1000000
    amount = 100
 
    def add(self):
        """Simulating hard work"""
        for _ in range(self.repeats):
            logging.debug("Number.add: id=%i int=%s size=%s", id(self.value), self.value, sys.getsizeof(self.value))
            self.value += self.amount
 
    def remove(self):
        """Simulating hard work"""
        for _ in range(self.repeats):
            self.value -= self.amount
 
 
if __name__ == "__main__":
    #####################
    # exploiting above code example
    #####################
    number = Number()
    logging.info("id=%i int=%s size=%s", id(number.value), number.value, sys.getsizeof(number.value))
    add = Thread(target=number.add)
    substract = Thread(target=number.remove)
    add.start()
    substract.start()
 
    logging.info('Waiting for threads to finish...')
    add.join()
    substract.join()
 
    logging.info("id=%i int=%s size=%s", id(number.value), number.value, sys.getsizeof(number.value))

Not sure how to proceed with CWE-366 at the moment.

We may need another example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants