This paper studies the problem of enforcing safetyof a stochastic dynamical system over a finite time horizon.We use stochastic barrier functions as a means to quantify the probability that a system exits a given safe region of thestate space in finite time. A barrier certificate condition thatbounds the infinitesimal generator of the system, and hencebounds the expected value of the barrier function over the time horizon, is recast as a sum-of-squares optimization problemfor efficient numerical computation. Unlike prior works, theproposed certificate condition includes a state-dependent boundon the infinitesimal generator, allowing for tighter probabilitybounds. Moreover, for stochastic systems for which the driftdynamics are affine-in-control, we propose a method for syn-thesizing polynomial state feedback controllers that achieve aspecified probability of safety. Two case studies are presentedthat benchmark and illustrate the performance of our method.