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

py/qstr: Special case qstr_find_strn for empty string. #12894

Merged
merged 2 commits into from
Nov 7, 2023

Conversation

jimmo
Copy link
Member

@jimmo jimmo commented Nov 6, 2023

Reported here: #12853 (comment)

Copy link

github-actions bot commented Nov 6, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS

Copy link

codecov bot commented Nov 6, 2023

Codecov Report

Merging #12894 (5b604e5) into master (bea6ff8) will increase coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 5b604e5 differs from pull request most recent head 4212799. Consider uploading reports for the commit 4212799 to get more accurate results

@@           Coverage Diff           @@
##           master   #12894   +/-   ##
=======================================
  Coverage   98.39%   98.39%           
=======================================
  Files         158      158           
  Lines       20971    20972    +1     
=======================================
+ Hits        20635    20636    +1     
  Misses        336      336           
Files Coverage Δ
py/qstr.c 95.48% <100.00%> (+0.02%) ⬆️

@@ -110,8 +110,9 @@ char *strchr(const char *s, int c) {
}

int strncmp(const char *s1, const char *s2, size_t n) {
while (*s1 && *s2 && n-- > 0) {
while (n > 0 && *s1 && *s2) {
Copy link
Member

Choose a reason for hiding this comment

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

Ha, this actually fixes the case when n=0 is passed in to this function, with both strings non-null.

C99 says that strncmp has UB for either string being NULL, so the
current behavior is technically correct, but it's an easy fix to handle
this case correctly.

7.1.4: "unless explicitly stated otherwise in the detailed
description... if an argument to a function has ...null pointer.. the
behavior is undefined".

7.21.1: "Unless explicitly stated otherwise in the description of a
particular function in this subclause, pointer arguments on such a call
shall still have valid values, as described in 7.1.4".

Also make the same change for the minimal version in bare-arm/lib.c.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This handles the case where an empty bytes/bytearray/str could pass in
NULL as the str argument (with length zero). This would result in UB in
strncmp. Even though our bare-metal implementation of strncmp handles
this, best to avoid it for when we're using system strncmp.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
@dpgeorge dpgeorge merged commit 4212799 into micropython:master Nov 7, 2023
37 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants