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

[CN] Array example fails when using size_t index (but passes with unsigned long) #815

Open
septract opened this issue Jan 6, 2025 · 0 comments
Labels
base typing bug Something isn't working cn

Comments

@septract
Copy link
Collaborator

septract commented Jan 6, 2025

CN fails to verify an array access when using an index of type size_t, but passes the same example when the index has type unsigned long (size_t is an alias for unsigned long, at least on my system).

Replication

The following example fails in CN:

#include <stddef.h>
#define MAX_LEN 32
static int eg1[MAX_LEN];
static size_t eg1_len = 0; // <-- Use `size_t` here 

void mem_access_eg1_fail() 
/*$
accesses eg1; 
accesses eg1_len; 
$*/
{
    if (eg1_len >= MAX_LEN)
        return;
    int* p = &eg1[eg1_len];
}

The example fails with the following error:

  $ cn verify --magic-comment-char-dollar global_mem_2.c --output=debug
  [1/2]: mem_access_eg1_fail -- fail
  [2/2]: mem_access_eg2_pass -- pass
  global_mem_2.c:20:14: error: `&(&&eg1[(u64)0'i32])[O_eg1_len0]` out of bounds
      int* p = &eg1[eg1_len];
              ^~~~~~~~~~~~~ 
  (UB missing short message): UB_CERB004_unspecified__pointer_add

However, the same example works when the type of the index is changed unsigned long:

// Example passes w `unsigned long` 
#include <stddef.h>
#define MAX_LEN 32
static int eg2[MAX_LEN];
static unsigned long eg2_len = 0; // <-- use `unsigned long` here 

void mem_access_eg2_pass() 
/*$
accesses eg2; 
accesses eg2_len; 
$*/
{
    if (eg2_len >= MAX_LEN)
        return;
    int* p = &eg2[eg2_len];
}

Analysis

@septract septract added bug Something isn't working cn base typing labels Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
base typing bug Something isn't working cn
Projects
None yet
Development

No branches or pull requests

1 participant