If you think of pathfinding being applied to graphs, the problem does not come up at all. Each vertex in the graph (in your case a black square) has edges only to adjacent vertices it can reach. So as a preprocess you would only add diagonal edges (connections) if the neighbouring squares can be walked form the current.
Alternatively you could set infinite edge weights for obstacles, but with a grid i guess you do not use weights. (I'm not that familiar with pathfinding on the special case of grids myself. Guess it's more efficient and common to calculate edges and weights on the fly with grids, but the concepts of general graphs still apply.)
However, i'm curious about squares 3-6 from your result. It looks like it misses the shortest path because it goes towards the goal until it hits an obstacle.
Is this expected from A*? Or could it be a bug?