Summary: The model for `getcwd` assumes the first argument should be non-null when in fact a NULL pointer is legitimate and results in allocation: > As an extension to the POSIX.1-2001 standard, glibc's getcwd() allocates the buffer dynamically using mal‐ > loc(3) if buf is NULL. In this case, the allocated buffer has the length size unless size is zero, when buf > is allocated as big as necessary. The caller should free(3) the returned buffer. I suggest this glibc extension be used for the getcwd model to reduce false positives. Pull Request resolved: https://github.com/facebook/infer/pull/925 Reviewed By: mbouaziz Differential Revision: D9830450 Pulled By: jvillard fbshipit-source-id: 95c4862b1master
parent
5478f3be64
commit
75e4226ea3
@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright (c) 2018-present, Facebook, Inc.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
#include <unistd.h>
|
||||
|
||||
#define BUFFER_SIZE 16
|
||||
|
||||
char getcwd_ok() {
|
||||
char* cwd = getcwd(NULL, 0);
|
||||
if (cwd != NULL) {
|
||||
char result = cwd[0];
|
||||
free(cwd);
|
||||
return result;
|
||||
}
|
||||
char buf[BUFFER_SIZE];
|
||||
cwd = getcwd(&buf, BUFFER_SIZE);
|
||||
if (cwd != NULL) {
|
||||
return cwd[0];
|
||||
}
|
||||
return 'a';
|
||||
}
|
||||
|
||||
char getcwd_no_buf_no_check_bad() {
|
||||
char* cwd = getcwd(NULL, 0);
|
||||
char result = cwd[0];
|
||||
free(cwd);
|
||||
return result;
|
||||
}
|
||||
|
||||
char getcwd_no_buf_no_free_bad() {
|
||||
char* cwd = getcwd(NULL, 0);
|
||||
if (cwd != NULL) {
|
||||
return cwd[0];
|
||||
}
|
||||
return 'a';
|
||||
}
|
||||
|
||||
char getcwd_no_check_bad() {
|
||||
char buf[BUFFER_SIZE];
|
||||
char* cwd = getcwd(&buf, BUFFER_SIZE);
|
||||
return cwd[0];
|
||||
}
|
Loading…
Reference in new issue