envp_get_key: fix buffer overflow if line contains no '='

This commit is contained in:
Khaïs COLIN 2025-02-18 15:28:09 +01:00
parent 9ac8588518
commit af90bec318
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 7 additions and 4 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/14 18:43:38 by jguelen #+# #+# */
/* Updated: 2025/02/18 15:16:58 by khais ### ########.fr */
/* Updated: 2025/02/18 15:41:54 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,9 @@
** given that line is of the form key=value, return a string containing key,
** which is allocated and must be freed.
**
** if line is null or is an empty string, return NULL
** if line is null or is empty, return NULL
**
** if line contains no '=', return an copy of line
**
** if allocation fail, return NULL
*/
@ -31,7 +33,7 @@ char *envp_get_key(char *line)
key_len = 0;
if (line == NULL || line[0] == '\0')
return (NULL);
while (line[key_len] != '=')
while (line[key_len] != '\0' && line[key_len] != '=')
key_len++;
key = malloc((key_len + 1) * sizeof(char));
if (key == NULL)

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/18 15:11:14 by khais #+# #+# */
/* Updated: 2025/02/18 15:25:35 by khais ### ########.fr */
/* Updated: 2025/02/18 15:33:36 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,5 +27,6 @@ int main(void) {
test_envp_get_key("=/bin/fish", "");
test_envp_get_key(NULL, NULL);
test_envp_get_key("", NULL);
test_envp_get_key("VARNAME", "VARNAME");
return (0);
}