mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
envp_get_key: fix buffer overflow if line contains no '='
This commit is contained in:
parent
9ac8588518
commit
af90bec318
2 changed files with 7 additions and 4 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue