Yes heartworm medicine can be dangerous for dogs if they do not have heartworm.
Some people give the dog heartworm medicine as a preventative measure but that really is not a good idea as it can lead to heart issues and other health issues.
When you give the dog heartworm medicine when they do not need it then the dogs lifespan may be shortened so it's best to only give the dog heartworm medicine if they already have heartworm.
Dogs get heartworm through a mosquito bite so if the dog does not get bitten by a mosquito then they will not get heartworm.
Also heartworm in dogs is not contagious to either dogs or humans.
So a dog cannot get heartworms from being around another dog who does have heartworms and a human cannot get heartworms from being around the dog who has heartworms.
Heartworm infects a dog through a mosquito bite so if a dog does not get bitten by a mosquito they will not get heartworm.
You should only give your dog heartworm medicine if the dog actually has heartworms as giving the dog heartworm medicine as a preventative measure may actually cause the dog more harm than it does good.
Heartworm medicine can sometimes cause heart issues with a dog and shorten their already short enough lifespan.