Fix invalid read in ft_split
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 1 Aug 2024 10:03:40 +0000 (12:03 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 1 Aug 2024 10:03:40 +0000 (12:03 +0200)
This read happens when the last word ends with the terminating '\0'.
After advancing the index to the '\0' it is then increased once more to
regions not accessible. This is fixed by simple skipping the ++i after a
word has been processed.

ft_str/ft_split.c

index a9bd121255d073b466a273a4c382fedd03b83d6c..641ca2584b755ec1b348129bb0fcd77c9fe473dd 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/06/20 11:51:05 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/07/21 18:53:26 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/01 11:56:05 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -75,7 +75,8 @@ char  **ft_split(const char *str, const char *seps)
                        res[str_num++] = extract_substr(&str[i], seps);
                        i += substr_len(&str[i], seps);
                }
-               ++i;
+               else
+                       ++i;
        }
        res[str_num] = NULL;
        return (res);