Skip to content

Conversation

@jimmo
Copy link
Member

@jimmo jimmo commented Nov 6, 2023

Reported here: #12853 (comment)

@github-actions
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

@codecov
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%) ⬆️

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Nov 7, 2023

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.

jimmo added 2 commits November 7, 2023 16:01
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 <[email protected]>
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 <[email protected]>
@dpgeorge dpgeorge merged commit 4212799 into micropython:master Nov 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

py-core Relates to py/ directory in source

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants