2017-02-19 173 views
0

你好我上使用以下命令編譯一個C11程序此警告與gcc(5.4.0版本):分配,而不是鏗鏘

$ gcc -g -Wall -std=c11 main.c -o minishell 
main.c: In function ‘process_new’: 
main.c:184:10: error: assignment of read-only variable ‘s’ 
     s = slice_next(s, ':')) { 

但沒有與clang(版本3.8.0):

$ clang -g -Wall -std=c11 main.c -o minishell # Compile without warning. 

我在Ubuntu 16.04上。

下面是代碼

// The loop that generate the warning with gcc. 
for (str_slice s = slice_at(paths, ':'); 
     !slice_empty(s); 
     s = slice_next(s, ':')) { 
//  ^Gcc complains here. 
    const char *full_path = build_full_path(progname, s); 
    /* I use with full_path but nothing with s after this point. */ 
    // There is no aliasing on full_path at this point. 
    free((void *)full_path); . 
    } 

這裏的str_slice定義:

typedef struct _str_slice { 
    const char* data; 
    const uint32_t len; // end - data len of slice. 
//^^^^^ Source of gcc warning. 
} str_slice; 

,並使用它的功能:

inline 
uint32_t slice_len(const str_slice slice) { 
    return slice.len; 
} 

inline 
const char* slice_data(const str_slice s) { 
    return s.data; 
} 

inline 
str_slice slice_new(const char* data, uint32_t len) { 
    return (str_slice) { data, len }; 
} 

inline 
str_slice slice_at(const char* data, const char c) { 
    const char* end = strchr(data, c); 
    return slice_new(data, end - data); 
} 

inline 
str_slice slice_next(const str_slice s, const char c) { 
    const char* data = slice_data(s) + slice_len(s) + 1; // skip c 
    const char* end = strchr(data, c); 
    if (end != NULL) { 
    return slice_new(data, end - data); 
    } else { 
    return slice_new(NULL, 0); 
    } 
} 

inline 
bool slice_empty(const str_slice s) { 
    return s.len == 0; 
} 

,並在必要的代碼約build_full_path

const char* build_full_path(const char* progname, const str_slice slice) { 
    size_t len_progname = strlen(progname); 
    // Save additional 2 bytes for adding '/' and '\0'. 
    size_t full_path_size = len_progname + slice.len + 2; 
    size_t malloc_size = sizeof(char) * full_path_size; 
    char *full_path = malloc(malloc_size); 

    full_path[full_path_size - 1] = '\0'; 
    memcpy(full_path, slice.data, slice.len); 
    full_path[slice.len] = '/'; 
    memcpy(full_path + slice.len + 1, progname, len_progname); 

    return (const char *) full_path; 
} 

當用clang編譯時,我得到了一個具有良好行爲的可執行文件。 所以我犯了錯誤?或者我發現了一個錯誤?

這裏我的程序(過時)的全碼:https://gist.github.com/darnuria/12af88c509310c2b40e0031522882720

編輯:的memcpy而不是使用strncpy。刪除標量類型上的常量。

+0

'str_slice'的解釋是什麼? –

+0

@AndySchweig爲了清晰起見,我將分隔代碼塊中的定義移動了一遍。 :) – Darnuria

+0

另一個明顯濫用'strncpy'。使用'memcpy'或'snprintf'代替 – chqrlie

回答

1

在該結構中,數據成員len被聲明爲常數數據成員。

typedef struct _str_slice { 
    const char* data; 
    const uint32_t len; // end - data len of slice. 
    ^^^^^^  
} str_slice; 

這意味着它可以被改變,並且作爲結果可能不是結構的一個對象指定給結構的另一個目的。

+0

好的,謝謝!但是如果我想保持這個領域的穩定性。我能怎麼做?我想保留傳遞這個結構的可能性,因爲它只是一個指針和一個32位無符號整數。 – Darnuria

+0

@Darnuria您可以通過複製傳遞,因爲在這種情況下會創建一個新的臨時對象。但是您不可以將結構的一個對象分配給結構的另一個對象。如果你打算使用賦值操作符,那麼數據成員len不應該是const。 –

+0

好吧,非常感謝!我明白。我應該製作一個可重複使用的小例子並填寫一個bug來發出叮噹聲嗎?看起來很奇怪,不會在叮噹中發出警告。 – Darnuria